Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 40c8361

Browse files
authored
Add benchmark for ProbabilitySampler. (#394)
1 parent f6151ba commit 40c8361

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

opencensus/trace/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,19 @@ cc_binary(
491491
],
492492
)
493493

494+
cc_binary(
495+
name = "sampler_benchmark",
496+
testonly = 1,
497+
srcs = ["internal/sampler_benchmark.cc"],
498+
copts = TEST_COPTS,
499+
linkstatic = 1,
500+
deps = [
501+
":span_context",
502+
":trace",
503+
"@com_github_google_benchmark//:benchmark",
504+
],
505+
)
506+
494507
cc_binary(
495508
name = "span_benchmark",
496509
testonly = 1,

opencensus/trace/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ opencensus_benchmark(trace_cloud_trace_context_benchmark
219219
opencensus_benchmark(trace_grpc_trace_bin_benchmark
220220
internal/grpc_trace_bin_benchmark.cc trace_grpc_trace_bin)
221221

222+
opencensus_benchmark(trace_sampler_benchmark
223+
internal/sampler_benchmark.cc
224+
trace_span_context
225+
trace)
226+
222227
opencensus_benchmark(trace_span_benchmark
223228
internal/span_benchmark.cc
224229
trace_span_context
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string>
16+
#include <vector>
17+
18+
#include "benchmark/benchmark.h"
19+
#include "opencensus/trace/sampler.h"
20+
#include "opencensus/trace/span.h"
21+
#include "opencensus/trace/span_context.h"
22+
#include "opencensus/trace/span_id.h"
23+
#include "opencensus/trace/trace_id.h"
24+
25+
namespace opencensus {
26+
namespace trace {
27+
namespace {
28+
29+
void BM_ProbabilitySampler(benchmark::State& state) {
30+
// Unused:
31+
SpanContext parent_context;
32+
bool has_remote_parent = true;
33+
SpanId span_id;
34+
std::string name = "MyName";
35+
std::vector<Span*> parent_links;
36+
// Used:
37+
constexpr uint8_t trace_id_buf[TraceId::kSize] = {
38+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
39+
TraceId trace_id(trace_id_buf);
40+
static ProbabilitySampler sampler(.5);
41+
42+
while (state.KeepRunning()) {
43+
sampler.ShouldSample(&parent_context, has_remote_parent, trace_id, span_id,
44+
name, parent_links);
45+
}
46+
}
47+
BENCHMARK(BM_ProbabilitySampler);
48+
49+
} // namespace
50+
} // namespace trace
51+
} // namespace opencensus
52+
53+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)