Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_executable(benchmarks
gauge_bench.cc
histogram_bench.cc
info_bench.cc
labels_bench.cc
registry_bench.cc
summary_bench.cc
)
Expand Down
67 changes: 67 additions & 0 deletions core/benchmarks/labels_bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <benchmark/benchmark.h>

#include <functional>
#include <map>
#include <string>
#include <utility>
#include <vector>

#include "benchmark_helpers.h"

#if defined(__cpp_lib_string_view) && defined(__cpp_lib_generic_associative_lookup)
#include <string_view>

using Plain = std::map<std::string, std::string>;
using Transparent = std::map<std::string, std::string, std::less<>>;

template <typename Map>
static Map MakeMap(std::size_t size, std::size_t key_length,
std::vector<std::string>& keys) {
Map map;
keys.reserve(size);
for (std::size_t i = 0; i < size; ++i) {
auto key = GenerateRandomString(key_length);
map.emplace(key, "value");
keys.push_back(std::move(key));
}
return map;
}

template <typename Map, bool Heterogeneous>
static void BM_Labels_LookupView(benchmark::State& state) {
std::vector<std::string> keys;
const auto map = MakeMap<Map>(state.range(0), state.range(1), keys);
std::size_t i = 0;
while (state.KeepRunning()) {
const std::string_view key = keys[i];
if (++i == keys.size()) i = 0;
if constexpr (Heterogeneous) {
benchmark::DoNotOptimize(map.find(key));
} else {
benchmark::DoNotOptimize(map.find(std::string(key)));
}
}
}

template <typename Map>
static void BM_Labels_LookupString(benchmark::State& state) {
std::vector<std::string> keys;
const auto map = MakeMap<Map>(state.range(0), state.range(1), keys);
std::size_t i = 0;
while (state.KeepRunning()) {
const std::string& key = keys[i];
if (++i == keys.size()) i = 0;
benchmark::DoNotOptimize(map.find(key));
}
}

BENCHMARK_TEMPLATE(BM_Labels_LookupView, Plain, false)
->ArgsProduct({{4, 16}, {8, 32}});
BENCHMARK_TEMPLATE(BM_Labels_LookupView, Transparent, true)
->ArgsProduct({{4, 16}, {8, 32}});
BENCHMARK_TEMPLATE(BM_Labels_LookupString, Plain)
->ArgsProduct({{4, 16}, {8, 32}});
BENCHMARK_TEMPLATE(BM_Labels_LookupString, Transparent)
->ArgsProduct({{4, 16}, {8, 32}});

#endif
10 changes: 10 additions & 0 deletions core/include/prometheus/labels.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#pragma once

#if defined(__cpp_lib_generic_associative_lookup)
#include <functional>
#endif
#include <map>
#include <string>

namespace prometheus {

/// \brief Multiple labels and their value.
///
/// Uses the transparent comparator std::less<> on C++14 and newer to allow
/// heterogeneous lookup, falling back to the default comparator on C++11.
#if defined(__cpp_lib_generic_associative_lookup)
using Labels = std::map<std::string, std::string, std::less<>>;
#else
using Labels = std::map<std::string, std::string>;
#endif

/// \brief Single label and its value.
using Label = Labels::value_type;
Expand Down
Loading