-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Optimize span and trace ID generation (#1521)
- Loading branch information
Showing
2 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'benchmark/ipsa' | ||
|
||
INVALID_SPAN_ID = ("\0" * 8).b | ||
INVALID_TRACE_ID = ("\0" * 16).b | ||
|
||
def generate_trace_id | ||
loop do | ||
id = Random.bytes(16) | ||
return id unless id == INVALID_TRACE_ID | ||
end | ||
end | ||
|
||
def generate_trace_id_while | ||
id = Random.bytes(16) | ||
id = Random.bytes(16) while id == INVALID_TRACE_ID | ||
id | ||
end | ||
|
||
def generate_span_id | ||
loop do | ||
id = Random.bytes(8) | ||
return id unless id == INVALID_SPAN_ID | ||
end | ||
end | ||
|
||
def generate_span_id_while | ||
id = Random.bytes(8) | ||
id = Random.bytes(8) while id == INVALID_SPAN_ID | ||
id | ||
end | ||
|
||
Benchmark.ipsa do |x| | ||
x.report('generate_trace_id') { generate_trace_id } | ||
x.report('generate_trace_id_while') { generate_trace_id_while } | ||
x.compare! | ||
end | ||
|
||
Benchmark.ipsa do |x| | ||
x.report('generate_span_id') { generate_span_id } | ||
x.report('generate_span_id_while') { generate_span_id_while } | ||
x.compare! | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters