Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k6 v0.42.0 updates - helper scripts (3/5) #23

Merged
merged 4 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions k6bench.gnuplot
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set datafile separator ','
set terminal pngcairo background rgb 'white' linewidth 4 size 1200,600 enhanced font 'Arial,16'
set bmargin at screen 130.0/600
set rmargin at screen 1080.0/1200
set output sprintf('%s-%s.png', ec2instance, script)

set title sprintf('k6 %s / EC2 %s / %s', k6version, ec2instance, script) font 'Arial Bold,20'
set key at graph 0.6, 0.3 autotitle columnhead

set border 31 lw 0.5
set style data lines
set style line 100 lt 1 lc rgb "grey" lw 0.5 # linestyle for the grid
set grid ls 100

set xtics 30
set xlabel 'Time (M:S)'
set xdata time
set format x '%M:%S'
set xtics rotate
set y2tics # enable second axis
set ytics nomirror # dont show the tics on that side
set y2range [0:] # start from 0
set y2label "CPU (%)"

plot sprintf('%s-%s.csv', ec2instance, script) using ($1):2 axis x1y2 lc rgb '#00d8bfd8', \
'' using ($1):($3 / 1000) title 'RAM (MB)', \
'' using ($1):4, \
'' using ($1):5
50 changes: 50 additions & 0 deletions k6bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# External dependencies:
# - https://www.selenic.com/smem/
# - https://stedolan.github.io/jq/
set -eEuo pipefail

trap 'cleanup; exit 0' EXIT
trap 'trap - INT; cleanup; kill -INT $$' INT
trap 'trap - TERM; cleanup; kill -TERM $$' TERM

# Create temp files to store the output of each collection command.
cpuf=$(mktemp)
memf=$(mktemp)
vusf=$(mktemp)
rpsf=$(mktemp)

cleanup() {
rm -f "$cpuf" "$memf" "$vusf" "$rpsf"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I prefer to wrap variables in quotes and in ${} not only prefix them with $.

IIRC that also prevents you from potentially matching a shorter variable that is just prefix of the longer one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I do that as well where it's useful (e.g. part of an interpolated string), but in this case there's no reason to do so, since they're separate arguments, and there's no risk of mistakenly using a different variable.

}

# 5s is the default interval between samples.
# Note that this might be greater if either smem or the k6 API takes more time
# than this to return a response.
sint="${K6_BENCH_SAMPLE_INTERVAL:-5}"

k6 run "$@" >k6out.txt 2>&1 &
k6pid="$!"

# Run the collection processes in parallel to avoid blocking.
# For details see https://stackoverflow.com/a/68316571

echo '"Time (s)","CPU (%)","RAM (kB)","VUs","RPS"'
while true; do
etimes=$(ps -p "$k6pid" --no-headers -o etimes | awk '{ print $1 }')
pids=()
{ exec >"$cpuf"; top -b -n 2 -d "$sint" -p "$k6pid" | {
grep "$k6pid" || echo; } | tail -1 | awk '{print (NF>0 ? $9 : "0")}'; } &
pids+=($!)
{ exec >"$memf"; smem -H -U "$USER" -c 'pid pss' -P 'k6 run' | {
grep "$k6pid" || echo 0; } | awk '{ print $NF }'; } &
pids+=($!)
{ exec >"$vusf"; { curl -fsSL http://localhost:6565/v1/metrics/vus 2>/dev/null || echo '{}'
} | jq '.data.attributes.sample.value // 0'; } &
pids+=($!)
{ exec >"$rpsf"; { curl -fsSL http://localhost:6565/v1/metrics/http_reqs 2>/dev/null || echo '{}'
} | jq '.data.attributes.sample.rate // 0'; } &
pids+=($!)
wait "${pids[@]}"
echo "${etimes},$(cat "$cpuf"),$(cat "$memf"),$(cat "$vusf"),$(cat "$rpsf")"
done