Skip to content

Instantly share code, notes, and snippets.

@mayocream
Created June 6, 2022 08:17
Show Gist options
  • Save mayocream/8c48b5a27bedb3409434a118bff2120a to your computer and use it in GitHub Desktop.
Save mayocream/8c48b5a27bedb3409434a118bff2120a to your computer and use it in GitHub Desktop.
local to_hex = require "resty.string".to_hex
local get_rand_bytes = require "kong.tools.utils".get_rand_bytes
local concat = table.concat
local proxy_span = {
trace_id = get_rand_bytes(16),
span_id = get_rand_bytes(8),
parent_id = get_rand_bytes(8),
should_sample = true,
}
local function target1()
return to_hex(proxy_span.trace_id) ..
"-" .. to_hex(proxy_span.span_id) ..
"-" .. (proxy_span.should_sample and "1" or "0") ..
(proxy_span.parent_id and "-" .. to_hex(proxy_span.parent_id) or "")
end
local function target2()
return concat({
to_hex(proxy_span.trace_id),
to_hex(proxy_span.span_id),
(proxy_span.should_sample and "1" or "0"),
-- fine if parent_id is nil
proxy_span.parent_id,
}, "-")
end
local target = target2
for i = 1, 100 do
target()
end
ngx.update_time()
local begin = ngx.now()
local N = 1e7
for i = 1, N do
target()
end
ngx.update_time()
ngx.say("elapsed: ", (ngx.now() - begin) / N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment