Here's the code
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
done
This 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.
1 Answer 1
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:
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:$seconds
Don't use seq
seq
is not portable, I suggest to avoid it. Instead of this:
for i in `seq 1 $max`; do ...; done
You can write:
for ((i = 1; i < max; i++)); do ...; done
Don'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.
-
\$\begingroup\$
seq
is very common in Linux these days. \$\endgroup\$chicks– chicks2016年03月04日 15:40:42 +00:00Commented Mar 4, 2016 at 15:40 -
\$\begingroup\$ I prefer my scripts to run everywhere \$\endgroup\$janos– janos2016年03月04日 16:05:30 +00:00Commented Mar 4, 2016 at 16:05
-
\$\begingroup\$ Every comment you made is valid, I over-looked them. Any word on the loooong
curl
command. It itches me but i couldn't do much. \$\endgroup\$bigOmega ツ– bigOmega ツ2016年03月06日 19:36:58 +00:00Commented Mar 6, 2016 at 19:36 -
\$\begingroup\$ @bigΩmega well, I'm wondering if you need the
tr
at all. I think you can simply drop that from the pipeline but I'm not sure. You can give that a try. \$\endgroup\$janos– janos2016年03月06日 20:05:05 +00:00Commented Mar 6, 2016 at 20:05 -
\$\begingroup\$ @janos So without that
tr
i only get the first line out of the pipe \$\endgroup\$bigOmega ツ– bigOmega ツ2016年03月07日 06:31:41 +00:00Commented Mar 7, 2016 at 6:31