-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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 |