File: baggage_benchmark.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (122 lines) | stat: -rw-r--r-- 3,372 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <benchmark/benchmark.h>
#include <stddef.h>
#include <string>

#include "opentelemetry/baggage/baggage.h"
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"

using namespace opentelemetry::baggage;
namespace nostd = opentelemetry::nostd;

namespace
{

const size_t kNumEntries = 10;

std::string header_with_custom_entries(size_t num_entries)
{
  std::string header;
  for (size_t i = 0; i < num_entries; i++)
  {
    std::string key   = "ADecentlyLargekey" + std::to_string(i);
    std::string value = "ADecentlyLargeValue" + std::to_string(i);
    header.append(key).append("=").append(value);
    if (i != num_entries - 1)
    {
      header += ",";
    }
  }
  return header;
}

void BM_CreateBaggageFromTenEntries(benchmark::State &state)
{
  std::string header = header_with_custom_entries(kNumEntries);
  while (state.KeepRunning())
  {
    auto baggage = Baggage::FromHeader(header);
  }
}
BENCHMARK(BM_CreateBaggageFromTenEntries);

void BM_ExtractBaggageHavingTenEntries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(header_with_custom_entries(kNumEntries));
  while (state.KeepRunning())
  {
    baggage->GetAllEntries(
        [](nostd::string_view /* key */, nostd::string_view /* value */) { return true; });
  }
}
BENCHMARK(BM_ExtractBaggageHavingTenEntries);

void BM_CreateBaggageFrom180Entries(benchmark::State &state)
{
  std::string header = header_with_custom_entries(Baggage::kMaxKeyValuePairs);
  while (state.KeepRunning())
  {
    auto baggage = Baggage::FromHeader(header);
  }
}
BENCHMARK(BM_CreateBaggageFrom180Entries);

void BM_ExtractBaggageWith180Entries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(header_with_custom_entries(Baggage::kMaxKeyValuePairs));
  while (state.KeepRunning())
  {
    baggage->GetAllEntries(
        [](nostd::string_view /* key */, nostd::string_view /* value */) { return true; });
  }
}
BENCHMARK(BM_ExtractBaggageWith180Entries);

void BM_SetValueBaggageWithTenEntries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(
      header_with_custom_entries(kNumEntries - 1));  // 9 entries, and add one new
  while (state.KeepRunning())
  {
    auto new_baggage = baggage->Set("new_key", "new_value");
  }
}
BENCHMARK(BM_SetValueBaggageWithTenEntries);

void BM_SetValueBaggageWith180Entries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(header_with_custom_entries(
      Baggage::kMaxKeyValuePairs - 1));  // keep 179 entries, and add one new
  while (state.KeepRunning())
  {
    auto new_baggage = baggage->Set("new_key", "new_value");
  }
}
BENCHMARK(BM_SetValueBaggageWith180Entries);

void BM_BaggageToHeaderTenEntries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(header_with_custom_entries(kNumEntries));
  while (state.KeepRunning())
  {
    auto new_baggage = baggage->ToHeader();
  }
}
BENCHMARK(BM_BaggageToHeaderTenEntries);

void BM_BaggageToHeader180Entries(benchmark::State &state)
{
  auto baggage = Baggage::FromHeader(header_with_custom_entries(Baggage::kMaxKeyValuePairs));
  while (state.KeepRunning())
  {
    auto new_baggage = baggage->ToHeader();
  }
}
BENCHMARK(BM_BaggageToHeader180Entries);
}  // namespace

BENCHMARK_MAIN();