From 6fa563be84a16b49035ed7d54cb13747738f7498 Mon Sep 17 00:00:00 2001 From: Sam Handler Date: Thu, 13 Apr 2023 13:42:39 -0600 Subject: [PATCH 1/4] speed up id generator --- Makefile | 5 ++- README.md | 4 +++ benchmark/id_generator.lua | 8 +++++ benchmark/run.sh | 10 ++++++ lib/opentelemetry/trace/exporter/console.lua | 2 +- lib/opentelemetry/trace/id_generator.lua | 35 +++++--------------- spec/trace/id_generator_spec.lua | 19 +++++++++++ 7 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 benchmark/id_generator.lua create mode 100755 benchmark/run.sh create mode 100644 spec/trace/id_generator_spec.lua diff --git a/Makefile b/Makefile index d995218..dd1efbb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: doc format api-test +.PHONY: doc format api-test benchmark CONTAINER_ORCHESTRATOR ?= docker-compose CONTAINER_ORCHESTRATOR_EXEC_OPTIONS := $(CONTAINER_ORCHESTRATOR_EXEC_OPTIONS) @@ -35,3 +35,6 @@ api-test: generate-semantic-conventions: $(CONTAINER_ORCHESTRATOR) run --no-deps $(CONTAINER_ORCHESTRATOR_EXEC_OPTIONS) -- openresty bash -c 'pushd tmp && rm -rf opentelemetry-specification && git clone --depth=1 https://github.com/open-telemetry/opentelemetry-specification.git && popd && resty ./utils/generate_semantic_conventions.lua && lua-format -i lib/opentelemetry/semantic_conventions/trace/*.lua' + +benchmark: + $(CONTAINER_ORCHESTRATOR) run --no-deps $(CONTAINER_ORCHESTRATOR_EXEC_OPTIONS) -- openresty bash -c 'cd /opt/opentelemetry-lua && benchmark/run.sh' diff --git a/README.md b/README.md index 076297a..c91c8fc 100644 --- a/README.md +++ b/README.md @@ -335,3 +335,7 @@ local my_metrics_reporter = { } otel_global.set_metrics_reporter(metrics_reporter) ``` + +### Benchmarks + +You can run benchmarks using `make benchmark`. diff --git a/benchmark/id_generator.lua b/benchmark/id_generator.lua new file mode 100644 index 0000000..89c8d1a --- /dev/null +++ b/benchmark/id_generator.lua @@ -0,0 +1,8 @@ +local id_generator = require("lib.opentelemetry.trace.id_generator") +local start = os.clock() + +for _ = 1, 5000000 do + id_generator.new_ids() +end + +print('fewer random calls, 5m new ids: ' .. (os.clock() - start) ..' seconds.') diff --git a/benchmark/run.sh b/benchmark/run.sh new file mode 100755 index 0000000..5949bfb --- /dev/null +++ b/benchmark/run.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +FILES="benchmark/*.lua" +for f in $FILES +do + if [ -f "$f" ] + then + resty -I /opt/opentelemetry-lua/lib "$f" + fi +done diff --git a/lib/opentelemetry/trace/exporter/console.lua b/lib/opentelemetry/trace/exporter/console.lua index e633f38..4d53c75 100644 --- a/lib/opentelemetry/trace/exporter/console.lua +++ b/lib/opentelemetry/trace/exporter/console.lua @@ -29,7 +29,7 @@ function _M.export_spans(self, spans) -- Check if ngx variable is not nil; use ngx.log if ngx var is present. if ngx then - ngx.log(ngx.INFO, "Export spans: ", span_string) + ngx.log(ngx.CRIT, "Export spans: ", span_string) else print("Export spans: ", span_string) end diff --git a/lib/opentelemetry/trace/id_generator.lua b/lib/opentelemetry/trace/id_generator.lua index 30bfa9c..1a3956b 100644 --- a/lib/opentelemetry/trace/id_generator.lua +++ b/lib/opentelemetry/trace/id_generator.lua @@ -4,39 +4,22 @@ local bit = require 'bit' local tohex = bit.tohex local fmt = string.format local random = util.random +local FFFFFFFF = 4294967295 -- FFFFFFFF in hexadecimal is 4294967295 in decimal local _M = {} function _M.new_span_id() - return fmt("%s%s%s%s%s%s%s%s", - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2)) + return fmt("%s%s", + tohex(random(0, FFFFFFFF), 8), + tohex(random(0, FFFFFFFF), 8)) end function _M.new_ids() - return fmt("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2), - tohex(random(0, 255), 2)), _M.new_span_id() + return fmt("%s%s%s%s", + tohex(random(0, FFFFFFFF), 8), + tohex(random(0, FFFFFFFF), 8), + tohex(random(0, FFFFFFFF), 8), + tohex(random(0, FFFFFFFF), 8)), _M.new_span_id() end return _M diff --git a/spec/trace/id_generator_spec.lua b/spec/trace/id_generator_spec.lua new file mode 100644 index 0000000..9209ebc --- /dev/null +++ b/spec/trace/id_generator_spec.lua @@ -0,0 +1,19 @@ +local id_generator = require("opentelemetry.trace.id_generator") + +describe("new_span_id", function() + it("generates a 16 character hex string", function() + for _ = 0, 100 do + assert.is_equal(16, #id_generator.new_span_id()) + end + end) +end) + +describe("new_ids", function() + it("generates a 16 character hex string and a 32 character string", function() + for _ = 0, 100 do + local trace_id, span_id = id_generator.new_ids() + assert.is_equal(32, #trace_id) + assert.is_equal(16, #span_id) + end + end) +end) From 30bb2ff9dd63339325d61513a448df4d36482a7b Mon Sep 17 00:00:00 2001 From: Sam Handler Date: Thu, 13 Apr 2023 13:45:18 -0600 Subject: [PATCH 2/4] clean up output --- benchmark/id_generator.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/id_generator.lua b/benchmark/id_generator.lua index 89c8d1a..3faf024 100644 --- a/benchmark/id_generator.lua +++ b/benchmark/id_generator.lua @@ -5,4 +5,4 @@ for _ = 1, 5000000 do id_generator.new_ids() end -print('fewer random calls, 5m new ids: ' .. (os.clock() - start) ..' seconds.') +print('5m new ids: ' .. (os.clock() - start) ..' seconds.') From 9bd4a8ecb9bad2956a1d8000489067299c5a75a7 Mon Sep 17 00:00:00 2001 From: Sam Handler Date: Thu, 13 Apr 2023 13:46:18 -0600 Subject: [PATCH 3/4] woops --- lib/opentelemetry/trace/exporter/console.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/opentelemetry/trace/exporter/console.lua b/lib/opentelemetry/trace/exporter/console.lua index 4d53c75..e633f38 100644 --- a/lib/opentelemetry/trace/exporter/console.lua +++ b/lib/opentelemetry/trace/exporter/console.lua @@ -29,7 +29,7 @@ function _M.export_spans(self, spans) -- Check if ngx variable is not nil; use ngx.log if ngx var is present. if ngx then - ngx.log(ngx.CRIT, "Export spans: ", span_string) + ngx.log(ngx.INFO, "Export spans: ", span_string) else print("Export spans: ", span_string) end From 26c4a365a534d48ecf9b6c9623debaf2150f7ebd Mon Sep 17 00:00:00 2001 From: Sam Handler Date: Fri, 14 Apr 2023 08:30:58 -0600 Subject: [PATCH 4/4] use hexadecimal directly --- lib/opentelemetry/trace/id_generator.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/opentelemetry/trace/id_generator.lua b/lib/opentelemetry/trace/id_generator.lua index 1a3956b..0292101 100644 --- a/lib/opentelemetry/trace/id_generator.lua +++ b/lib/opentelemetry/trace/id_generator.lua @@ -4,22 +4,21 @@ local bit = require 'bit' local tohex = bit.tohex local fmt = string.format local random = util.random -local FFFFFFFF = 4294967295 -- FFFFFFFF in hexadecimal is 4294967295 in decimal local _M = {} function _M.new_span_id() return fmt("%s%s", - tohex(random(0, FFFFFFFF), 8), - tohex(random(0, FFFFFFFF), 8)) + tohex(random(0, 0xFFFFFFFF), 8), + tohex(random(0, 0xFFFFFFFF), 8)) end function _M.new_ids() return fmt("%s%s%s%s", - tohex(random(0, FFFFFFFF), 8), - tohex(random(0, FFFFFFFF), 8), - tohex(random(0, FFFFFFFF), 8), - tohex(random(0, FFFFFFFF), 8)), _M.new_span_id() + tohex(random(0, 0xFFFFFFFF), 8), + tohex(random(0, 0xFFFFFFFF), 8), + tohex(random(0, 0xFFFFFFFF), 8), + tohex(random(0, 0xFFFFFFFF), 8)), _M.new_span_id() end return _M