2b9f6cf296c873e4dcf9fb8eece42ae0d81ff7f4
[csit.git] / resources / tools / wrk / wrk_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -x
16
17 WRK_VERSION="4.0.2"
18 WRK_TAR=${WRK_VERSION}".tar.gz"
19 WRK_DWNLD_PATH="https://github.com/wg/wrk/archive"
20 WRK_TARGET="/opt"
21 WRK_INSTALL_DIR=${WRK_TARGET}/wrk-${WRK_VERSION}
22
23 function wrk_utils.install {
24     # Install wrk
25
26     # Directory for wrk:
27     dir=${1}
28     # Force the installation:
29     force=${2:-false}
30
31     # Check if wrk is installed:
32     if [ "${force}" = true ]; then
33         wrk_utils.destroy
34     else
35         which wrk
36         if [ $? -eq 0 ]; then
37             test -d ${dir}/${WRK_INSTALL_DIR} && echo "WRK already installed: ${dir}/${WRK_INSTALL_DIR}" && exit 0
38         fi
39     fi
40
41     # Install pre-requisites:
42     apt-get update
43     apt-get install build-essential libssl-dev -y
44
45     # Remove previous installation:
46     wrk_utils.destroy
47
48     # Change the directory:
49     cd ${WRK_TARGET}
50
51     # Get the specified version:
52     wget ${WRK_DWNLD_PATH}/${WRK_TAR}
53     tar xzf ${WRK_TAR}
54     rm ${WRK_TAR}
55     cd ${WRK_INSTALL_DIR}
56
57     # Build the wrk:
58     make
59
60     # Move the executable to somewhere in the PATH:
61     cp wrk /usr/local/bin
62 }
63
64 function wrk_utils.destroy {
65     # Remove wrk
66
67     sudo rm /usr/local/bin/wrk || true
68     sudo rm -rf ${WRK_INSTALL_DIR} || true
69 }
70
71 function wrk_utils.traffic_1_url_1_core {
72     # Send traffic
73     # - to n URL (NIC)
74     # - using n instances of wrk, each on separate core.
75
76     # The CPU used for wrk
77     cpu=${1}
78     # Total number of threads to use by one instance of wrk to send traffic.
79     threads=${2}
80     # Total number of HTTP connections to keep open with each thread handling
81     # N = connections / threads.
82     connections=${3}
83     # Duration of the test.
84     duration=${4}
85     # HTTP header to add to request.
86     header=${5}
87     # Record a timeout if a response is not received within this amount of time.
88     timeout=${6}
89     # Path to LuaJIT script.
90     script=${7}
91     # Print detailed latency statistics.
92     latency=${8}
93     # URL to send the traffic to.
94     url=${9}
95
96     if [ "${timeout}" != "None" ]; then
97         timeout="--timeout ${timeout}"
98     else
99         timeout=""
100     fi
101
102     if [ "${latency}" = "True" ]; then
103         latency="--latency"
104     else
105         latency=""
106     fi
107
108     if [ "${script}" != "None" ]; then
109         script="--script '${script}'"
110     else
111         script=""
112     fi
113
114     if [ "${header}" != "None" ]; then
115         header="${header}"
116     else
117         header=""
118     fi
119
120     taskset --cpu-list ${cpu} \
121         wrk --threads ${threads} \
122             --connections ${connections} \
123             --duration ${duration} \
124             --header "${header}" \
125             ${timeout} \
126             ${script} \
127             ${latency} \
128             ${url}
129 }
130
131 function wrk_utils.traffic_n_urls_n_cores {
132     # Send traffic
133     # - to n URL (NIC)
134     # - using n instances of wrk, each on separate core.
135
136     # The first CPU used for wrk
137     first_cpu=${1}
138     # Total number of threads to use by one instance of wrk to send traffic.
139     threads=${2}
140     # Total number of HTTP connections to keep open with each thread handling
141     # N = connections / threads.
142     connections=${3}
143     # Duration of the test.
144     duration=${4}
145     # HTTP header to add to request.
146     header=${5}
147     # Record a timeout if a response is not received within this amount of time.
148     timeout=${6}
149     # Path to LuaJIT script.
150     script=${7}
151     # Print detailed latency statistics.
152     latency=${8}
153     # URL to send the traffic to.
154     urls=${9}
155
156     if [ "${timeout}" != "None" ]; then
157         timeout="--timeout ${timeout}"
158     else
159         timeout=""
160     fi
161
162     if [ "${latency}" = "True" ]; then
163         latency="--latency"
164     else
165         latency=""
166     fi
167
168     if [ "${script}" != "None" ]; then
169         script="--script '${script}'"
170     else
171         script=""
172     fi
173
174     if [ "${header}" != "None" ]; then
175         header="${header}"
176     else
177         header=""
178     fi
179
180     urls=$(echo ${urls} | tr ";" "\n")
181     cpu=${first_cpu}
182     for url in ${urls}; do
183         taskset --cpu-list ${cpu} \
184             wrk --threads ${threads} \
185                 --connections ${connections} \
186                 --duration ${duration} \
187                 --header "${header}" \
188                 ${timeout} \
189                 ${script} \
190                 ${latency} \
191                 ${url} &
192         cpu=$((cpu+1))
193     done
194
195     sleep ${duration}
196     sleep 2
197 }
198
199 function wrk_utils.traffic_n_urls_m_cores {
200     # Send traffic
201     # - to n URL (NIC)
202     # - using m instances of wrk, each on separate core.
203
204     # The first CPU used for wrk
205     first_cpu=${1}
206     # The last CPU used for wrk
207     cpus_per_url=${2}
208     # Total number of threads to use by one instance of wrk to send traffic.
209     threads=${3}
210     # Total number of HTTP connections to keep open with each thread handling
211     # N = connections / threads.
212     connections=${4}
213     # Duration of the test.
214     duration=${5}
215     # HTTP header to add to request.
216     header=${6}
217     # Record a timeout if a response is not received within this amount of time.
218     timeout=${7}
219     # Path to LuaJIT script.
220     script=${8}
221     # Print detailed latency statistics.
222     latency=${9}
223     # URL to send the traffic to.
224     urls=${10}
225
226     if [ "${timeout}" != "None" ]; then
227         timeout="--timeout ${timeout}"
228     else
229         timeout=""
230     fi
231
232     if [ "${latency}" = "True" ]; then
233         latency="--latency"
234     else
235         latency=""
236     fi
237
238     if [ "${script}" != "None" ]; then
239         script="--script '${script}'"
240     else
241         script=""
242     fi
243
244     if [ "${header}" != "None" ]; then
245         header="${header}"
246     else
247         header=""
248     fi
249
250     urls=$(echo ${urls} | tr ";" "\n")
251
252     cpu=${first_cpu}
253     for i in `seq 1 ${cpus_per_url}`; do
254         for url in ${urls}; do
255             taskset --cpu-list ${cpu} \
256                 wrk --threads ${threads} \
257                     --connections ${connections} \
258                     --duration ${duration} \
259                     --header "${header}" \
260                     ${timeout} \
261                     ${script} \
262                     ${latency} \
263                     ${url} &
264             cpu=$((cpu+1))
265         done
266     done
267
268     sleep ${duration}
269     sleep 2
270 }
271
272 args=("$@")
273 case ${1} in
274     install)
275         force=${2}
276         wrk_utils.install ${force}
277         ;;
278     destroy)
279         wrk_utils.destroy
280         ;;
281     traffic_1_url_1_core)
282         wrk_utils.traffic_1_url_1_core  "${args[@]:1}"
283         ;;
284     traffic_n_urls_n_cores)
285         wrk_utils.traffic_n_urls_n_cores "${args[@]:1}"
286         ;;
287     traffic_n_urls_m_cores)
288         wrk_utils.traffic_n_urls_m_cores "${args[@]:1}"
289         ;;
290 esac