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

Speed up id generator #74

Merged
merged 4 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,7 @@ local my_metrics_reporter = {
}
otel_global.set_metrics_reporter(metrics_reporter)
```

### Benchmarks

You can run benchmarks using `make benchmark`.
8 changes: 8 additions & 0 deletions benchmark/id_generator.lua
Original file line number Diff line number Diff line change
@@ -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('5m new ids: ' .. (os.clock() - start) ..' seconds.')
10 changes: 10 additions & 0 deletions benchmark/run.sh
Original file line number Diff line number Diff line change
@@ -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
35 changes: 9 additions & 26 deletions lib/opentelemetry/trace/id_generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
yangxikun marked this conversation as resolved.
Show resolved Hide resolved

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
19 changes: 19 additions & 0 deletions spec/trace/id_generator_spec.lua
Original file line number Diff line number Diff line change
@@ -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)