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

Commit 75e7e2f

Browse files
authored
Add context_benchmark. (#213)
1 parent 1a184af commit 75e7e2f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

opencensus/context/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,19 @@ cc_test(
6161
"@com_google_googletest//:gtest_main",
6262
],
6363
)
64+
65+
# Benchmarks
66+
# ========================================================================= #
67+
68+
cc_binary(
69+
name = "context_benchmark",
70+
testonly = 1,
71+
srcs = ["internal/context_benchmark.cc"],
72+
copts = TEST_COPTS,
73+
linkopts = ["-pthread"], # Required for absl/synchronization bits.
74+
linkstatic = 1,
75+
deps = [
76+
":context",
77+
"@com_github_google_benchmark//:benchmark",
78+
],
79+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018, 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 "opencensus/context/context.h"
16+
17+
#include <functional>
18+
19+
#include "benchmark/benchmark.h"
20+
21+
namespace opencensus {
22+
namespace context {
23+
namespace {
24+
25+
void BM_ContextCurrent(benchmark::State& state) {
26+
benchmark::DoNotOptimize(Context::Current()); // Force TLS initialization.
27+
for (auto _ : state) {
28+
benchmark::DoNotOptimize(Context::Current());
29+
}
30+
}
31+
BENCHMARK(BM_ContextCurrent);
32+
33+
void BM_CopyDefaultContext(benchmark::State& state) {
34+
Context ctx = Context::Current();
35+
for (auto _ : state) {
36+
Context copy = ctx;
37+
benchmark::DoNotOptimize(copy);
38+
}
39+
}
40+
BENCHMARK(BM_CopyDefaultContext);
41+
42+
void BM_WrapDefaultContext(benchmark::State& state) {
43+
Context ctx = Context::Current();
44+
std::function<void()> fn = []() {};
45+
for (auto _ : state) {
46+
benchmark::DoNotOptimize(ctx.Wrap(fn));
47+
}
48+
}
49+
BENCHMARK(BM_WrapDefaultContext);
50+
51+
} // namespace
52+
} // namespace context
53+
} // namespace opencensus
54+
55+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)