Move: WRK installation into ansible
[csit.git] / resources / tools / wrk / wrk_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2019 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 function wrk_utils.installed {
18
19     # Check if the WRK utility is installed. Fail if not installed.
20
21     # Returns:
22     # - 0 - If command is installed.
23     # - 1 - If command is not installed.
24
25     set -exuo pipefail
26
27     command -v wrk
28 }
29
30
31 function wrk_utils.traffic_1_url_1_core {
32     # Send traffic
33     # - to n URL (NIC)
34     # - using n instances of wrk, each on separate core.
35
36     # The CPU used for wrk
37     cpu=${1}
38     # Total number of threads to use by one instance of wrk to send traffic.
39     threads=${2}
40     # Total number of HTTP connections to keep open with each thread handling
41     # N = connections / threads.
42     connections=${3}
43     # Duration of the test.
44     duration=${4}
45     # HTTP header to add to request.
46     header=${5}
47     # Record a timeout if a response is not received within this amount of time.
48     timeout=${6}
49     # Path to LuaJIT script.
50     script=${7}
51     # Print detailed latency statistics.
52     latency=${8}
53     # URL to send the traffic to.
54     url=${9}
55
56     if [ "${timeout}" != "None" ]; then
57         timeout="--timeout ${timeout}"
58     else
59         timeout=""
60     fi
61
62     if [ "${latency}" = "True" ]; then
63         latency="--latency"
64     else
65         latency=""
66     fi
67
68     if [ "${script}" != "None" ]; then
69         script="--script '${script}'"
70     else
71         script=""
72     fi
73
74     if [ "${header}" != "None" ]; then
75         header="${header}"
76     else
77         header=""
78     fi
79
80     taskset --cpu-list ${cpu} \
81         wrk --threads ${threads} \
82             --connections ${connections} \
83             --duration ${duration} \
84             --header "${header}" \
85             ${timeout} \
86             ${script} \
87             ${latency} \
88             ${url}
89 }
90
91 function wrk_utils.traffic_n_urls_n_cores {
92     # Send traffic
93     # - to n URL (NIC)
94     # - using n instances of wrk, each on separate core.
95
96     # The first CPU used for wrk
97     first_cpu=${1}
98     # Total number of threads to use by one instance of wrk to send traffic.
99     threads=${2}
100     # Total number of HTTP connections to keep open with each thread handling
101     # N = connections / threads.
102     connections=${3}
103     # Duration of the test.
104     duration=${4}
105     # HTTP header to add to request.
106     header=${5}
107     # Record a timeout if a response is not received within this amount of time.
108     timeout=${6}
109     # Path to LuaJIT script.
110     script=${7}
111     # Print detailed latency statistics.
112     latency=${8}
113     # URL to send the traffic to.
114     urls=${9}
115
116     if [ "${timeout}" != "None" ]; then
117         timeout="--timeout ${timeout}"
118     else
119         timeout=""
120     fi
121
122     if [ "${latency}" = "True" ]; then
123         latency="--latency"
124     else
125         latency=""
126     fi
127
128     if [ "${script}" != "None" ]; then
129         script="--script '${script}'"
130     else
131         script=""
132     fi
133
134     if [ "${header}" != "None" ]; then
135         header="${header}"
136     else
137         header=""
138     fi
139
140     urls=$(echo ${urls} | tr ";" "\n")
141     cpu=${first_cpu}
142     for url in ${urls}; do
143         taskset --cpu-list ${cpu} \
144             wrk --threads ${threads} \
145                 --connections ${connections} \
146                 --duration ${duration} \
147                 --header "${header}" \
148                 ${timeout} \
149                 ${script} \
150                 ${latency} \
151                 ${url} &
152         cpu=$((cpu+1))
153     done
154
155     sleep ${duration}
156     sleep 2
157 }
158
159 function wrk_utils.traffic_n_urls_m_cores {
160     # Send traffic
161     # - to n URL (NIC)
162     # - using m instances of wrk, each on separate core.
163
164     # The first CPU used for wrk
165     first_cpu=${1}
166     # The last CPU used for wrk
167     cpus_per_url=${2}
168     # Total number of threads to use by one instance of wrk to send traffic.
169     threads=${3}
170     # Total number of HTTP connections to keep open with each thread handling
171     # N = connections / threads.
172     connections=${4}
173     # Duration of the test.
174     duration=${5}
175     # HTTP header to add to request.
176     header=${6}
177     # Record a timeout if a response is not received within this amount of time.
178     timeout=${7}
179     # Path to LuaJIT script.
180     script=${8}
181     # Print detailed latency statistics.
182     latency=${9}
183     # URL to send the traffic to.
184     urls=${10}
185
186     if [ "${timeout}" != "None" ]; then
187         timeout="--timeout ${timeout}"
188     else
189         timeout=""
190     fi
191
192     if [ "${latency}" = "True" ]; then
193         latency="--latency"
194     else
195         latency=""
196     fi
197
198     if [ "${script}" != "None" ]; then
199         script="--script '${script}'"
200     else
201         script=""
202     fi
203
204     if [ "${header}" != "None" ]; then
205         header="${header}"
206     else
207         header=""
208     fi
209
210     urls=$(echo ${urls} | tr ";" "\n")
211
212     cpu=${first_cpu}
213     for i in `seq 1 ${cpus_per_url}`; do
214         for url in ${urls}; do
215             taskset --cpu-list ${cpu} \
216                 wrk --threads ${threads} \
217                     --connections ${connections} \
218                     --duration ${duration} \
219                     --header "${header}" \
220                     ${timeout} \
221                     ${script} \
222                     ${latency} \
223                     ${url} &
224             cpu=$((cpu+1))
225         done
226     done
227
228     sleep ${duration}
229     sleep 2
230 }
231
232 args=("$@")
233 case ${1} in
234     installed)
235         wrk_utils.installed
236         ;;
237     traffic_1_url_1_core)
238         wrk_utils.traffic_1_url_1_core  "${args[@]:1}"
239         ;;
240     traffic_n_urls_n_cores)
241         wrk_utils.traffic_n_urls_n_cores "${args[@]:1}"
242         ;;
243     traffic_n_urls_m_cores)
244         wrk_utils.traffic_n_urls_m_cores "${args[@]:1}"
245         ;;
246 esac