This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate-scaling.sh
executable file
·353 lines (301 loc) · 8.44 KB
/
validate-scaling.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/sh
# Copyright 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
set -e
SEPARATOR="------------------------------------------------"
#TESTHOST="127.0.0.1"
TESTHOST="localhost"
# deployment scale wait
SCALE_SECS=120
# REQ_SECS multiplier
WAIT_COUNT=20
# parallel requests
PARALLEL=-1
TYPE="plain"
#TYPE="json"
usage ()
{
name=${0##*/}
echo "Validation script for device scalability in Kubernetes"
echo
echo "Usage: $name DEPLOYMENT DEP_SPACE SECS SERVICE SVC_SPACE LIMIT SCALE"
echo
echo "Controls backend DEPLOYMENT scaling in DEP_SPACE, and number of parallel requests"
echo "done to it through client SERVICE in SVC_SPACE. Verifies that given deployment can"
echo "be scaled up by doubling its replica count from 1 to LIMIT, with each step improving"
echo "request throughput (at least) by SCALE (according to requesting SERVICE)."
echo
echo "DEPLOYMENT scale-up is waited for ${SCALE_SECS}s (before script fails), and request throughput"
echo "change data is collected for $WAIT_COUNT*SECS (-> ~$((100/WAIT_COUNT))% error margin). SECS should be equal"
echo "or longer than the max run time for a single backend workload request."
echo
prefix=scalability-tester
echo "If '$TESTHOST' is given as k8s namespace, default $prefix localhost addresses"
echo "are used instead of querying Kubernetes."
echo
echo "Example(s) for workloads completing in <2 seconds:"
echo " $name $prefix-backend-sleep monitoring 2 $prefix-client-sleep validation 16 1.5"
echo " $name $prefix-backend-sleep $TESTHOST 2 $prefix-client-sleep $TESTHOST 16 1.5"
echo
echo "ERROR: $1!"
exit 1
}
if [ $# -ne 7 ]; then
usage "incorrect number of arguments"
fi
DEPLOYMENT=$1
DEP_SPACE=$2
# max workload run request completion time
REQ_SECS=$3
SERVICE=$4
SVC_SPACE=$5
LIMIT=$6
SCALE=$7
# check for helpers
if [ -z "$(which curl)" ]; then
echo "ERROR: 'curl' missing"
exit 1
fi
CURL="curl --no-progress-meter"
if [ $TYPE = "json" ] && [ -z "$(which jq)" ]; then
echo "ERROR: 'jq' missing"
exit 1
fi
echo "Current contents of the client service namespace:"
echo $SEPARATOR
kubectl -n "$SVC_SPACE" get all -o wide
echo $SEPARATOR
# service URLs
if [ "$SVC_SPACE" = "$TESTHOST" ]; then
SVC_URL="$TESTHOST:9996"
else
SVC_URL=$(kubectl -n "$SVC_SPACE" get svc "$SERVICE" --no-headers=true |\
sed 's%/TCP%%' | awk '{printf("%s:%s", $3, $5)}')
fi
if [ -z "$SVC_URL" ]; then
echo "ERROR: failed to get '$SERVICE' service endpoint"
exit 1
fi
echo "=> client service URL = $SVC_URL"
URL_PARALLEL="$SVC_URL/parallel?type=$TYPE&value"
URL_THROUGHPUT="$SVC_URL/reqs-per-sec?type=$TYPE"
URL_FAILS="$SVC_URL/fails?type=$TYPE"
URL_STATS="$SVC_URL/stats?type=$TYPE"
URL_RESET="$SVC_URL/reset?type=$TYPE"
URL_NODES="$SVC_URL/nodes"
cleanup ()
{
echo
echo $SEPARATOR
echo "Tempfile content: '$(cat "$TMPFILE")'"
echo $SEPARATOR
rm -r "$TMPDIR"
}
TMPDIR=$(mktemp --directory --suff -scale-validation)
echo "Directory for caching client responses:"
ls -ld "$TMPDIR"
echo
TMPFILE=$TMPDIR/output
trap cleanup EXIT
get_typed ()
{
if [ $TYPE = "plain" ]; then
cat "$1"
elif [ $TYPE = "json" ]; then
jq .result "$1"
else
echo "ERROR: unrecognized type '$TYPE'"
exit 1
fi
}
set_parallel ()
{
if [ "$PARALLEL" -eq "$1" ]; then
return
fi
PARALLEL=$1
if [ "$PARALLEL" -eq 0 ]; then
echo "Disabling client requests..."
else
echo "Setting number of parallel client requests to $PARALLEL..."
fi
echo "$CURL \"$URL_PARALLEL=$PARALLEL\" > $TMPFILE"
$CURL "$URL_PARALLEL=$PARALLEL" > "$TMPFILE"
ret=$(get_typed "$TMPFILE")
if [ "$ret" != "$1" ]; then
echo "ERROR: $ret != $1"
exit 1
fi
}
# wait upto given timeout until specified number of pods with given prefix are in given state
# in given namespace, and none of those pods are in any other state
wait_pod_state ()
{
SECS=$1
COUNT=$2
PREFIX=$3
SPACE=$4
STATE=$5
echo "Waiting ${SECS}s for $COUNT $SPACE/${PREFIX}* deployment(s) to be in '$STATE' state..."
SLEEP=2
while [ "$SECS" -gt 0 ]; do
# start with sleep to make sure terminated pods are not anymore in running state
SECS=$((SECS-SLEEP))
sleep $SLEEP
# show current deployment status, with node names
kubectl -n "$SPACE" get pods -o wide | grep -F "^$PREFIX" || true
# requested number of deployments not yet in given state?
LINES=$(kubectl -n "$SPACE" get pods | grep -c "^${PREFIX}.* $STATE " || true)
echo "= $LINES/$COUNT pods (${SECS}s remaining)"
if [ "$LINES" -ne "$COUNT" ]; then
continue
fi
# none in any other state?
LINES=$(kubectl -n "$SPACE" get pods | grep -F "^$PREFIX" | grep -vcF " $STATE " || true)
if [ "$LINES" -eq 0 ]; then
# => wait done
return
fi
done
echo "ERROR: deployment wait timed out"
# on timeout, show log for first erroring pod
CRASHER=$(kubectl -n "$SPACE" get pods | awk "/^${PREFIX}.* (Crash|Error)/"'{print $1; exit}')
if [ "$CRASHER" != "" ]; then
echo $SEPARATOR
echo "kubectl -n $SPACE logs $CRASHER"
kubectl -n "$SPACE" logs "$CRASHER"
echo $SEPARATOR
fi
exit 1
}
scale_backends_up ()
{
set_parallel 0
COUNT=$1
cmd="kubectl scale -n $DEP_SPACE deployment/$DEPLOYMENT --replicas=$COUNT"
if [ "$DEP_SPACE" != "$TESTHOST" ]; then
echo "Scaling backend deployments up to $COUNT..."
echo "$cmd"
$cmd
fi
# remaining scale wait: wait_pod_state() -> SECS
wait_pod_state "$SCALE_SECS" "$COUNT" "${DEPLOYMENT}-" "$DEP_SPACE" "Running"
# remaining request wait = REQ_SECS - (SCALE_SECS - SECS)
SECS=$((REQ_SECS-SCALE_SECS+SECS))
if [ $SECS -gt 0 ]; then
echo "Waiting extra ${SECS}s to make sure previous requests have finished..."
sleep $SECS
fi
}
scale_backends_down ()
{
set_parallel 0
COUNT=$1
cmd="kubectl scale -n $DEP_SPACE deployment/$DEPLOYMENT --replicas=$COUNT"
if [ "$DEP_SPACE" != "$TESTHOST" ]; then
echo "Scaling backend deployments down to $COUNT..."
echo "$cmd"
$cmd
fi
wait_pod_state "$SCALE_SECS" "$COUNT" "${DEPLOYMENT}-" "$DEP_SPACE" "Running"
}
echo "Verifying that backend deployment can be scaled to maximum..."
scale_backends_up "$LIMIT"
echo $SEPARATOR
echo "Discarding all backend deployments..."
scale_backends_down 0
OLD="0"
FAIL=0
COUNT=1
WAIT_SECS=$((WAIT_COUNT*REQ_SECS))
while [ $COUNT -le "$LIMIT" ]; do
echo $SEPARATOR
HISTOGRAM="false"
echo "Scaling deployments up to $COUNT..."
scale_backends_up "$COUNT"
echo "Reseting client metrics..."
echo "$CURL \"$URL_RESET\" > $TMPFILE"
$CURL "$URL_RESET" > "$TMPFILE"
ret=$(get_typed "$TMPFILE")
if [ "$ret" != "Reseted" ]; then
echo "ERROR: metrics reset failed"
exit 1
fi
echo "Setting client request parallelization also to $COUNT..."
set_parallel "$COUNT"
CUR_COUNT=$COUNT
COUNT=$((2*COUNT))
echo "Waiting ${WAIT_SECS}s for throughput metrics collection..."
sleep "$WAIT_SECS"
echo "Fetching throughput..."
echo "$CURL \"$URL_THROUGHPUT\" > $TMPFILE"
$CURL "$URL_THROUGHPUT" > "$TMPFILE"
RPS=$(get_typed "$TMPFILE")
if [ -z "$RPS" ]; then
echo "ERROR: request-per-second fetch failed"
exit 1
fi
echo "Fetching failure count..."
echo "$CURL \"$URL_FAILS\" > $TMPFILE"
$CURL "$URL_FAILS" > "$TMPFILE"
FAILED=$(get_typed "$TMPFILE")
if [ -z "$FAILED" ]; then
echo "ERROR: failure count fetch failed"
exit 1
fi
echo $SEPARATOR
echo "$CURL \"$URL_STATS\""
$CURL "$URL_STATS"
if [ "$FAILED" -gt 0 ]; then
echo "FAIL: $FAILED request failure(s)"
FAIL=$((FAIL+1))
fi
if [ -z "$(echo "$RPS" | tr -d .0)" ]; then
# new result was zero
echo "FAIL: throughput was zero!"
FAIL=$((FAIL+1))
OLD=$RPS
continue
fi
if [ -z "$(echo "$OLD" | tr -d .0)" ]; then
# old result was zero
echo "Throughput: $RPS reqs-per-sec (pods: $CUR_COUNT)"
OLD=$RPS
continue
fi
echo $SEPARATOR
CHANGE=$(echo "scale=2; $RPS/$OLD" | bc)
echo "Throughput: $OLD -> $RPS = $CHANGE increase on reqs-per-sec (pods: $CUR_COUNT)"
OLD=$RPS
if [ "$(echo "$CHANGE / $SCALE" | bc)" -lt 1 ]; then
echo $SEPARATOR
echo "FAIL: change $CHANGE < $SCALE"
FAIL=$((FAIL+1))
echo $SEPARATOR
# show node/device spread
echo "$CURL \"$URL_NODES\""
$CURL "$URL_NODES"
HISTOGRAM="true"
fi
done
if [ "$HISTOGRAM" != "true" ]; then
echo $SEPARATOR
# show node/device spread
echo "$CURL \"$URL_NODES\""
$CURL "$URL_NODES"
echo $SEPARATOR
fi
# disable load & extra log generation
set_parallel 0
# success -> disable trap
rm -r "$TMPDIR"
trap - EXIT
if [ $FAIL -gt 0 ]; then
echo "*** $FAIL FAILs ***"
echo "After debugging the issues, scale backends down with:"
echo "kubectl scale -n $DEP_SPACE deployment/$DEPLOYMENT --replicas=1"
exit $FAIL
fi
# scale backends down on success
scale_backends_down 1
echo "*** PASS ***"