-
Notifications
You must be signed in to change notification settings - Fork 11
/
artifactoryBenchmark.sh
executable file
·300 lines (259 loc) · 9.72 KB
/
artifactoryBenchmark.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
#!/bin/bash
# Benchmark Artifactory uploads and downloads
set -eou pipefail
SCRIPT_NAME=$0
SCRIPT_DIR=$(dirname ${SCRIPT_NAME})
LOGS_DIR="${SCRIPT_DIR}/${SCRIPT_NAME}-logs"
# Set some defaults
ART_URL=http://localhost
REPO="benchmark-tests"
SIZE=1
SIZE_UNIT=MB
USER="admin"
PASS="password"
TEST=all
ITERATIONS=5
SKIP_READINESS=false
ERRORS_FOUND=false
SEED=${RANDOM}
TEST_FILE=
######### Functions #########
errorExit () {
echo -e "\nERROR: $1\n"
echo "Check logs under ${LOGS_DIR}"
exit 1
}
usage () {
cat <<END_USAGE
Usage: ${SCRIPT_NAME} <options>
-l | --url : Artifactory URL (defaults to ${ART_URL})
-u | --user | --username : Artifactory user (defaults to ${USER})
-p | --pass | --password : Artifactory password, API key or token (defaults to ${PASS})
-r | --repo | --repository : Repository (defaults to ${REPO})
-s | --size : Size (defaults to ${SIZE})
-k | --unit : Size unit (KB or MB only) (defaults to ${SIZE_UNIT})
-i | --iterations : Number of test iterations (defaults to ${ITERATIONS})
-t | --test : Test type (all|upload|download) (defaults to ${TEST})
-d | --logs : Logs directory (defaults to ${LOGS_DIR})
-h | --help : Show this usage.
--skip-readiness-test : Skip the Artifactory readiness test
Examples:
========
# Test with all defaults - downloads and uploads
$ ${SCRIPT_NAME}
# Test a 10 MB upload 10 times
$ ${SCRIPT_NAME} --url https://server.company.org \\
--user admin --password password1x \\
--repo generic-tests \\
--test upload \\
--size 10 \\
--iterations 10
END_USAGE
exit 1
}
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-l | --url)
ART_URL="$2"
shift 2
;;
-r | --repo | --repository)
REPO="$2"
shift 2
;;
-u | --user | --username)
USER="$2"
shift 2
;;
-p | --pass | --password)
PASS="$2"
shift 2
;;
-s | --size)
SIZE="$2"
shift 2
;;
-k | --unit)
SIZE_UNIT="$2"
shift 2
;;
-i | --iterations)
ITERATIONS="$2"
shift 2
;;
-t | --test)
TEST="$2"
shift 2
;;
-d | --logs)
LOGS_DIR="$2"
shift 2
;;
--skip-readiness-test)
SKIP_READINESS=true
shift 1
;;
-h | --help)
usage
;;
*)
usage
;;
esac
done
if [[ ! ${TEST} =~ ^(all|download|upload)$ ]]; then
errorExit "Test type can be 'download' or 'upload' only!"
fi
if [[ ! ${SIZE_UNIT} =~ ^(KB|MB)$ ]]; then
errorExit "Size unit can be 'KB' or 'MB' only!"
fi
# Add the artifactory context to the ART_URL if missing
if [[ ! ${ART_URL} =~ \/artifactory\/?$ ]]; then
ART_URL="${ART_URL%/}"
ART_URL="${ART_URL}/artifactory"
fi
# Set a global, unique test file
TEST_FILE="test${SIZE}_${SIZE_UNIT}_${SEED}"
}
testArtifactory () {
[[ ${SKIP_READINESS} =~ true ]] && return
echo -n "Check Artifactory readiness... "
curl --connect-timeout 3 -f -s -k "${ART_URL}/api/v1/system/readiness" -o ${LOGS_DIR}/check-readiness.log || errorExit "Artifactory readiness failed"
echo "success"
}
cleanArtifactory () {
echo -n "Delete repository ${REPO} if exists... "
curl --connect-timeout 3 -f -s -k -u ${USER}:${PASS} "${ART_URL}/api/repositories/${REPO}" -o ${LOGS_DIR}/check-repository.log && deleteTestRepository || echo "repository does not exist"
}
createTestRepository () {
echo -n "Creating test repository ${REPO}... "
curl --connect-timeout 3 -f -s -k -u ${USER}:${PASS} -X PUT "${ART_URL}/api/repositories/${REPO}" -o ${LOGS_DIR}/create-repository.log -H 'Content-Type: application/json' -d "{\"key\":\"${REPO}\",\"rclass\":\"local\",\"packageType\":\"generic\",\"description\":\"Generic local repository for benchmarks\"}" || errorExit "Creating repository ${REPO} failed"
echo "success"
}
deleteTestRepository () {
echo -n "Deleting test repository ${REPO}... "
curl --connect-timeout 3 -f -s -k -u ${USER}:${PASS} -X DELETE "${ART_URL}/api/repositories/${REPO}" || errorExit "Deleting repository ${REPO} failed"
}
createFile () {
local test_file="$1"
local bs=1024
[[ ${SIZE_UNIT} =~ MB ]] && bs=1048576
# Create a unique binary, generic file
dd if=/dev/urandom of="${test_file}" bs=${bs} count="${SIZE}" status=noxfer || errorExit "Creating file ${test_file} failed"
}
createAndUploadTestFile () {
FULL_PATH="${ART_URL}/${REPO}/${TEST_FILE}"
echo -n "Creating a ${SIZE} ${SIZE_UNIT} file ${TEST_FILE}... "
createFile "${TEST_FILE}"
echo "Done"
echo "Uploading ${TEST_FILE} to ${FULL_PATH}"
curl --connect-timeout 3 -f -k -s -u ${USER}:${PASS} -X PUT -T ./${TEST_FILE} "${FULL_PATH}" -o ${LOGS_DIR}/upload-out.log || errorExit "Uploading ${TEST_FILE} to ${FULL_PATH} failed"
# Remove local file
rm -f "${TEST_FILE}" || errorExit "Deleting ${TEST_FILE} failed"
}
deleteTestFile () {
FULL_PATH="${ART_URL}/${REPO}/${TEST_FILE}"
# Delete test file
echo "Deleting ${FULL_PATH}"
curl --connect-timeout 3 -f -k -s -u ${USER}:${PASS} -X DELETE "${FULL_PATH}" -o ${LOGS_DIR}/delete-out.log || errorExit "Deleting ${FULL_PATH} failed"
}
printResults () {
local test=$1
echo "Results from ${LOGS_DIR}/${test}-results.csv"
echo "======================================== CSV START ================================================="
cat "${LOGS_DIR}/${test}-results.csv"
echo "======================================== CSV END ================================================="
}
downloadTest () {
local test=download
local results_line=
local results_array=()
echo -e "\n======== DOWNLOADS TEST ========"
# Create and upload the file to be used for the download tests
createAndUploadTestFile
echo "Running $ITERATIONS serial downloads"
echo "Run #, Test, Download size (bytes), Http response code, Total time (sec), Connect time (sec), Speed (bytes/sec)" > "${LOGS_DIR}/${test}-results.csv"
for ((i=1; i <= ${ITERATIONS}; i++)); do
echo -n "$i, $test, " >> "${LOGS_DIR}/${test}-results.csv"
results_line=$(curl -L -k -s -f -u ${USER}:${PASS} -X GET "${FULL_PATH}" -o /dev/null --write-out '%{size_download}, %{http_code}, %{time_total}, %{time_connect}, %{speed_download}\n')
echo "$results_line" >> "${LOGS_DIR}/${test}-results.csv"
OLD_IFS=$IFS
IFS=', '; read -a results_array <<< "$results_line"
if [[ ! ${results_array[1]} =~ 20. ]]; then
echo -e "\nERROR: A non 20x http response code detected (${results_array[1]})"
ERRORS_FOUND=true
fi
IFS=$OLD_IFS
echo -n "."
done
echo
echo "Done"
deleteTestFile
printResults ${test}
}
uploadTest () {
local test=upload
local results_line=
local results_array=()
FULL_PATH="${ART_URL}/${REPO}/${TEST_FILE}"
echo -e "\n========= UPLOADS TEST ========="
echo "Creating a ${SIZE} ${SIZE_UNIT} test files and uploading"
createFile "${TEST_FILE}"
echo "Test file ${TEST_FILE} ready"
echo "Run #, Test, Upload size (bytes), Http response code, Total time (sec), Connect time (sec), Speed (bytes/sec)" > "${LOGS_DIR}/${test}-results.csv"
echo "Running $ITERATIONS serial uploads"
for ((i=1; i <= ITERATIONS; i++)); do
echo -n "$i, $test, " >> "${LOGS_DIR}/${test}-results.csv"
results_line=$(curl --connect-timeout 3 -f -k -s -u ${USER}:${PASS} -X PUT -T ./${TEST_FILE} "${FULL_PATH}" -o /dev/null --write-out '%{size_upload}, %{http_code}, %{time_total}, %{time_connect}, %{speed_upload}\n')
OLD_IFS=$IFS
IFS=', '; read -a results_array <<< "$results_line"
if [[ ! ${results_array[1]} =~ 20. ]]; then
echo -e "\nERROR: A non 20x http response code detected (${results_array[1]})"
ERRORS_FOUND=true
fi
IFS=$OLD_IFS
echo "$results_line" >> "${LOGS_DIR}/${test}-results.csv"
echo -n "."
done
echo
echo "Done"
rm -f "${TEST_FILE}"
deleteTestFile
printResults ${test}
}
main () {
processOptions "$@"
rm -rf "${LOGS_DIR}"
mkdir -p "${LOGS_DIR}"
echo -e "\n================================"
echo "Server ${ART_URL}"
echo "Tests ${TEST}"
echo "User ${USER}"
echo "Repository ${REPO}"
echo "File size ${SIZE} ${SIZE_UNIT}"
echo "Iterations ${ITERATIONS}"
echo "================================"
testArtifactory
cleanArtifactory
createTestRepository
echo "================================"
case ${TEST} in
download)
downloadTest
;;
upload)
uploadTest
;;
all)
downloadTest
uploadTest
;;
esac
deleteTestRepository
if [[ ${ERRORS_FOUND} =~ true ]]; then
echo -e "\nERROR: Errors found in some of the tests. Review http response codes in the results"
fi
echo
}
main "$@"