patternbashMinor
Simple load testing script in bash
Viewed 0 times
scriptsimpletestingloadbash
Problem
Here's the code
This can be run as
Please suggest if you think there's a way to take in multiple
max="$1"
date
echo "url: $2
rate: $max calls / second"
START=$(date +%s);
get () {
curl -s -v "$1" 2>&1 | tr '\r\n' '\\n' | awk -v date="$(date +'%r')" '{print $0"\n-----", date}' >> /tmp/perf-test.log
}
while true
do
echo $(($(date +%s) - START)) | awk '{print int($1/60)":"int($1%60)}'
sleep 1
for i in `seq 1 $max`
do
get $2 &
done
doneThis can be run as
sh load-test.sh 20 "http://api.myserver.com/get_info"Please suggest if you think there's a way to take in multiple
curl options.Solution
Arithmetics in Bash
You're doing some math in Bash, some in Awk, some in a combination of both. You can do all of that in Bash alone. Most notably, instead of this:
This would be equivalent, but all in Bash, without additional processes:
Don't use
You can write:
Don't use `
You're doing some math in Bash, some in Awk, some in a combination of both. You can do all of that in Bash alone. Most notably, instead of this:
echo $(($(date +%s) - START)) | awk '{print int($1/60)":"int($1%60)}'This would be equivalent, but all in Bash, without additional processes:
((delta = $(date +%s) - START))
((minutes = delta / 60))
((seconds = delta % 60))
echo $minutes:$secondsDon't use
seqseq is not portable, I suggest to avoid it. Instead of this:for i in `seq 1 $max`; do ...; doneYou can write:
for ((i = 1; i < max; i++)); do ...; doneDon't use `
...
This syntax is obsolete today, always use $(...) instead.
Other minor things
This would be clearer as two echo lines, or a here-document with cat <<EOF:
echo "url: $2
rate: $max calls / second"
The trailing semicolon is unnecessary:
START=$(date +%s);
The quoting is unnecessary in date +'%r'`, but it does no harm.Code Snippets
echo $(($(date +%s) - START)) | awk '{print int($1/60)":"int($1%60)}'((delta = $(date +%s) - START))
((minutes = delta / 60))
((seconds = delta % 60))
echo $minutes:$secondsfor i in `seq 1 $max`; do ...; donefor ((i = 1; i < max; i++)); do ...; doneecho "url: $2
rate: $max calls / second"Context
StackExchange Code Review Q#121868, answer score: 5
Revisions (0)
No revisions yet.