Skip to content

Instantly share code, notes, and snippets.

@dolik-rce
Last active April 22, 2020 18:21
Show Gist options
  • Save dolik-rce/1b1a6d844fe51654d9bd8589db85bcff to your computer and use it in GitHub Desktop.
Save dolik-rce/1b1a6d844fe51654d9bd8589db85bcff to your computer and use it in GitHub Desktop.
Benchmark for https://github.com/knyar/nginx-lua-prometheus/pull/82, just place both files in benchmark/ directory, then execute run.sh.
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 20M;
lua_package_path "/nginx-lua-prometheus/?.lua;;";
error_log stderr info;
init_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
my_counter = prometheus:counter("counter1", "Benchmark counter", {"n"})
}
init_worker_by_lua_block {
prometheus:init_worker(1)
local get_keys_samples = 100
local metric_data_samples = 10
local max_metric_count = 50000
local step = 10000
local start_time
local get_keys_time
local metric_data_time
function measure(i)
ngx.update_time()
start_time = ngx.now()
for j = 1, get_keys_samples do
-- original: prometheus.dict:get_keys(0)
-- key_index: prometheus.key_index:get()
end
ngx.update_time()
get_keys_time = (ngx.now() - start_time) / get_keys_samples
ngx.update_time()
start_time = ngx.now()
for j = 1, metric_data_samples do
prometheus:metric_data()
end
ngx.update_time()
metric_data_time = (ngx.now() - start_time) / metric_data_samples
ngx.log(ngx.INFO, i, "\t", get_keys_time, "\t", metric_data_time)
end
function benchmark()
-- original: ngx.log(ngx.INFO, "Metric count\tavg. get_keys()\t avg. metric_data()")
-- key_index: ngx.log(ngx.INFO, "Metric count\tavg. key_index:get()\t avg. metric_data()")
for i = 0, max_metric_count do
if i % step == 0 then
measure(i)
end
my_counter:inc(1, {i})
end
ngx.log(ngx.INFO, "Done, press Ctrl+C to exit.")
end
-- only run this on one of the workers
if ngx.worker.id() == 0 then
-- launch from timer, so other workers can initialize first
ngx.timer.at(1, benchmark)
end
}
server {
listen 18001;
server_name benchmark;
location / {
return 200;
}
}
}
#!/bin/bash
set -x -e -u -o pipefail
base_dir="$(cd "$(dirname "$0")"; pwd -P)"
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
}
cleanup
trap cleanup EXIT
if [ -f ${base_dir}/../key_index.lua ]; then
version="key_index"
else
version=original
fi
conf="nginx-${version}.conf"
sed "s/-- $version: //;" ${base_dir}/nginx.conf > ${base_dir}/${conf}
exec docker run -it --name ${container_name} \
-v ${base_dir}/../:/nginx-lua-prometheus ${image_name} \
nginx -c /nginx-lua-prometheus/benchmark/${conf}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment