Skip to content

Instantly share code, notes, and snippets.

@dolik-rce
Last active May 4, 2020 11:44
Show Gist options
  • Save dolik-rce/58adb967288206aec7c0065dc8c8ed17 to your computer and use it in GitHub Desktop.
Save dolik-rce/58adb967288206aec7c0065dc8c8ed17 to your computer and use it in GitHub Desktop.
Nginx-lua-prometheus benchmark #2
daemon off;
user www-data;
pid /var/run/nginx.pid;
worker_processes 4;
include /etc/nginx/modules-enabled/*.conf;
events {}
http {
sendfile on;
tcp_nodelay on;
lua_shared_dict prometheus_metrics 100M;
lua_package_path "/nginx-lua-prometheus/?.lua;;";
log_format short_log "$time_local [$pid] $request_uri ($status) $request_time";
access_log /dev/stdout short_log buffer=1k flush=1s ;
error_log stderr info;
init_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
my_counter = prometheus:counter("counter1", "Benchmark counter", {"n"})
my_hist = prometheus:histogram("hist1", "Benchmark histogram", {"latency"},
{0.005, 0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75,
1, 1.5, 2, 3, 4, 5, 10, 15, 30, 45, 60, 90, 120, 180, 300})
}
init_worker_by_lua_block {
if prometheus.init_worker then
prometheus:init_worker(1)
end
local metric_count = 100000
function prepare()
ngx.log(ngx.INFO, "Preparing data...")
for i = 0, metric_count do
my_counter:inc(1, {i})
if i % 10000 == 0 then
ngx.update_time()
ngx.log(ngx.INFO, " ", (100.0*i/metric_count),"%")
end
end
ngx.timer.at(1, function() ngx.log(ngx.INFO, "Done!") end)
end
prepare()
}
log_by_lua_block {
my_hist:observe(tonumber(ngx.var.request_time), {ngx.var.uri})
}
server {
listen 18003;
server_name benchmark;
location / {
content_by_lua_block {
ngx.print("Hello world!\n")
}
}
location /metrics {
content_by_lua_block {
prometheus:collect()
}
}
}
}
#!/bin/bash
set -x -e -u -o pipefail
base_dir="$(cd "$(dirname "$0")"; pwd -P)"
bench_dir="$(basename ${base_dir})"
container_name="nginx_lua_prometheus_benchmark"
image_name="${container_name}_image"
cat > ${base_dir}/Dockerfile <<EOF
FROM debian:stable-slim
RUN apt-get update
RUN apt-get install --no-install-recommends -y nginx-light libnginx-mod-http-lua
EOF
docker build -t ${image_name} ${base_dir}
function cleanup {
docker rm -f ${container_name} || true
}
trap cleanup EXIT
run_metrics() {
set +x
case "$2" in
parallel)
CMD="curl -sS -o /dev/null --max-time 5 -w 'metrics %{time_starttransfer}\n' http://localhost:18003/metrics & sleep 10" ;;
serial)
CMD="curl -sS -o /dev/null -w 'metrics %{time_starttransfer}\n' http://localhost:18003/metrics" ;;
none)
CMD="sleep 10" ;;
esac
while true; do
eval "${CMD}"
done
set -x
}
run_benchmark() {
cleanup
docker run -d --name ${container_name} -p 18003:18003 \
-v ${base_dir}/../:/nginx-lua-prometheus ${image_name} \
nginx -c /nginx-lua-prometheus/${bench_dir}/nginx.conf
# wait for metrics to be prepared
docker logs -f ${container_name} 2>&1 | sed '/Done!/q;' || true
run_metrics "$1" "$2" &> metrics_${1}_${2}.log &
METRICS_PID=$!
ab -n 100000 -c 4 -s 120 "http://localhost:18003/test" | tee ${base_dir}/ab_${1}_${2}.log || true
kill $METRICS_PID || true
docker logs ${container_name} &> ${base_dir}/docker_${1}_${2}.log
docker kill ${container_name}
}
for COMMIT in "1e13e22c" "0.20200420" "avoid-get_keys"; do
git checkout "$COMMIT"
for METRICS in "parallel" "serial" "none"; do
run_benchmark "$COMMIT" "$METRICS"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment