feat add spdlog
Some checks failed
sm-rpc / build (Debug, aarch64-linux-gnu) (push) Failing after 29s
sm-rpc / build (Debug, arm-linux-gnueabihf) (push) Failing after 16s
sm-rpc / build (Debug, host.gcc) (push) Failing after 11s
sm-rpc / build (Debug, mipsel-linux-gnu) (push) Failing after 12s
sm-rpc / build (Release, aarch64-linux-gnu) (push) Failing after 11s
sm-rpc / build (Release, arm-linux-gnueabihf) (push) Failing after 11s
sm-rpc / build (Release, host.gcc) (push) Failing after 12s
sm-rpc / build (Release, mipsel-linux-gnu) (push) Failing after 16s

This commit is contained in:
tqcq
2025-08-22 18:22:57 +08:00
parent 3d395b45f0
commit c68893bace
2331 changed files with 1306786 additions and 103 deletions

View File

@@ -0,0 +1,12 @@
cc_binary(
name = "benchmarks",
srcs = glob([
"*.cc",
"*.h",
]),
linkstatic = True,
deps = [
"//core",
"@com_github_google_benchmark//:benchmark",
],
)

View File

@@ -0,0 +1,28 @@
add_executable(benchmarks
main.cc
benchmark_helpers.cc
benchmark_helpers.h
counter_bench.cc
gauge_bench.cc
histogram_bench.cc
info_bench.cc
registry_bench.cc
summary_bench.cc
)
target_link_libraries(benchmarks
PRIVATE
${PROJECT_NAME}::core
benchmark::benchmark
)
add_test(
NAME benchmarks
COMMAND benchmarks
)
set_property(
TEST benchmarks
APPEND PROPERTY LABELS Benchmark
)

View File

@@ -0,0 +1,26 @@
#include "benchmark_helpers.h"
#include <algorithm>
#include <cstdlib>
#include <map>
std::string GenerateRandomString(std::size_t length) {
auto randchar = []() -> char {
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
const std::size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
prometheus::Labels GenerateRandomLabels(std::size_t number_of_pairs) {
const auto label_character_count = 10;
auto label_pairs = prometheus::Labels{};
for (std::size_t i = 0; i < number_of_pairs; i++) {
label_pairs.insert({GenerateRandomString(label_character_count),
GenerateRandomString(label_character_count)});
}
return label_pairs;
}

View File

@@ -0,0 +1,9 @@
#pragma once
#include <cstddef>
#include <string>
#include "prometheus/labels.h"
std::string GenerateRandomString(std::size_t length);
prometheus::Labels GenerateRandomLabels(std::size_t number_of_labels);

View File

@@ -0,0 +1,33 @@
#include <benchmark/benchmark.h>
#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
static void BM_Counter_Increment(benchmark::State& state) {
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
auto& counter_family =
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
auto& counter = counter_family.Add({});
while (state.KeepRunning()) counter.Increment();
}
BENCHMARK(BM_Counter_Increment);
static void BM_Counter_Collect(benchmark::State& state) {
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
auto& counter_family =
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
auto& counter = counter_family.Add({});
while (state.KeepRunning()) {
benchmark::DoNotOptimize(counter.Collect());
};
}
BENCHMARK(BM_Counter_Collect);

View File

@@ -0,0 +1,59 @@
#include <benchmark/benchmark.h>
#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/registry.h"
static void BM_Gauge_Increment(benchmark::State& state) {
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
auto& gauge = gauge_family.Add({});
while (state.KeepRunning()) gauge.Increment(2);
}
BENCHMARK(BM_Gauge_Increment);
static void BM_Gauge_Decrement(benchmark::State& state) {
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
auto& gauge = gauge_family.Add({});
while (state.KeepRunning()) gauge.Decrement(2);
}
BENCHMARK(BM_Gauge_Decrement);
static void BM_Gauge_SetToCurrentTime(benchmark::State& state) {
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
auto& gauge = gauge_family.Add({});
while (state.KeepRunning()) gauge.SetToCurrentTime();
}
BENCHMARK(BM_Gauge_SetToCurrentTime);
static void BM_Gauge_Collect(benchmark::State& state) {
using prometheus::BuildGauge;
using prometheus::Gauge;
using prometheus::Registry;
Registry registry;
auto& gauge_family =
BuildGauge().Name("benchmark_gauge").Help("").Register(registry);
auto& gauge = gauge_family.Add({});
while (state.KeepRunning()) {
benchmark::DoNotOptimize(gauge.Collect());
};
}
BENCHMARK(BM_Gauge_Collect);

View File

@@ -0,0 +1,70 @@
#include <benchmark/benchmark.h>
#include <chrono>
#include <cstdint>
#include <random>
#include <vector>
#include "prometheus/family.h"
#include "prometheus/histogram.h"
#include "prometheus/registry.h"
using prometheus::Histogram;
static Histogram::BucketBoundaries CreateLinearBuckets(std::int64_t start,
std::int64_t end,
std::int64_t step) {
auto bucket_boundaries = Histogram::BucketBoundaries{};
for (auto i = start; i < end; i += step) {
bucket_boundaries.push_back(i);
}
return bucket_boundaries;
}
static void BM_Histogram_Observe(benchmark::State& state) {
using prometheus::BuildHistogram;
using prometheus::Histogram;
using prometheus::Registry;
const auto number_of_buckets = state.range(0);
Registry registry;
auto& histogram_family =
BuildHistogram().Name("benchmark_histogram").Help("").Register(registry);
auto bucket_boundaries = CreateLinearBuckets(0, number_of_buckets - 1, 1);
auto& histogram = histogram_family.Add({}, bucket_boundaries);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(0, number_of_buckets);
while (state.KeepRunning()) {
auto observation = d(gen);
auto start = std::chrono::high_resolution_clock::now();
histogram.Observe(observation);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
BENCHMARK(BM_Histogram_Observe)->Range(0, 4096)->UseManualTime();
static void BM_Histogram_Collect(benchmark::State& state) {
using prometheus::BuildHistogram;
using prometheus::Histogram;
using prometheus::Registry;
const auto number_of_buckets = state.range(0);
Registry registry;
auto& histogram_family =
BuildHistogram().Name("benchmark_histogram").Help("").Register(registry);
auto bucket_boundaries = CreateLinearBuckets(0, number_of_buckets - 1, 1);
auto& histogram = histogram_family.Add({}, bucket_boundaries);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(histogram.Collect());
}
}
BENCHMARK(BM_Histogram_Collect)->Range(0, 4096);

View File

@@ -0,0 +1,20 @@
#include <benchmark/benchmark.h>
#include "prometheus/family.h"
#include "prometheus/info.h"
#include "prometheus/registry.h"
static void BM_Info_Collect(benchmark::State& state) {
using prometheus::BuildInfo;
using prometheus::Info;
using prometheus::Registry;
Registry registry;
auto& info_family =
BuildInfo().Name("benchmark_info").Help("").Register(registry);
auto& info = info_family.Add({});
while (state.KeepRunning()) {
benchmark::DoNotOptimize(info.Collect());
};
}
BENCHMARK(BM_Info_Collect);

View File

@@ -0,0 +1,3 @@
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();

View File

@@ -0,0 +1,44 @@
#include <benchmark/benchmark.h>
#include <chrono>
#include "benchmark_helpers.h"
#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
static void BM_Registry_CreateFamily(benchmark::State& state) {
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
while (state.KeepRunning())
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
}
BENCHMARK(BM_Registry_CreateFamily);
static void BM_Registry_CreateCounter(benchmark::State& state) {
using prometheus::BuildCounter;
using prometheus::Counter;
using prometheus::Registry;
Registry registry;
auto& counter_family = BuildCounter()
.Labels(GenerateRandomLabels(10))
.Name("benchmark_counter")
.Help("")
.Register(registry);
while (state.KeepRunning()) {
auto labels = GenerateRandomLabels(state.range(0));
auto start = std::chrono::high_resolution_clock::now();
counter_family.Add(labels);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
BENCHMARK(BM_Registry_CreateCounter)->Range(0, 4096);

View File

@@ -0,0 +1,139 @@
#include <benchmark/benchmark.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <random>
#include <vector>
#include "prometheus/family.h"
#include "prometheus/registry.h"
#include "prometheus/summary.h"
using prometheus::Summary;
static const auto ITERATIONS = 262144;
static Summary::Quantiles CreateLinearQuantiles(int count) {
static auto generator = [](double x) {
static auto exp = [](double x) {
static const double A = 2;
return 1 - std::exp(-A * x);
};
return exp(x) / exp(1);
};
auto quantiles = Summary::Quantiles{};
for (auto i = 0; i < count; ++i) {
quantiles.emplace_back(generator(double(i) / count), 0.01);
}
return quantiles;
}
static void BM_Summary_Observe(benchmark::State& state) {
using prometheus::BuildSummary;
using prometheus::Registry;
using prometheus::Summary;
const auto number_of_quantiles = state.range(0);
Registry registry;
auto& summary_family =
BuildSummary().Name("benchmark_summary").Help("").Register(registry);
auto quantiles = CreateLinearQuantiles(number_of_quantiles);
auto& summary = summary_family.Add({}, quantiles);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(0, 100);
while (state.KeepRunning()) {
auto observation = d(gen);
auto start = std::chrono::high_resolution_clock::now();
summary.Observe(observation);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
BENCHMARK(BM_Summary_Observe)->Range(0, 64)->Iterations(ITERATIONS);
static void BM_Summary_Collect(benchmark::State& state) {
using prometheus::BuildSummary;
using prometheus::Registry;
using prometheus::Summary;
const auto number_of_quantiles = state.range(0);
const auto number_of_entries = state.range(1);
Registry registry;
auto& summary_family =
BuildSummary().Name("benchmark_summary").Help("").Register(registry);
auto quantiles = CreateLinearQuantiles(number_of_quantiles);
auto& summary = summary_family.Add({}, quantiles);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(0, 100);
for (auto i = 1; i <= number_of_entries; ++i) summary.Observe(d(gen));
while (state.KeepRunning()) {
benchmark::DoNotOptimize(summary.Collect());
}
}
BENCHMARK(BM_Summary_Collect)->RangePair(0, 64, 0, ITERATIONS);
static void BM_Summary_Observe_Common(benchmark::State& state) {
using prometheus::BuildSummary;
using prometheus::Registry;
using prometheus::Summary;
Registry registry;
auto& summary_family =
BuildSummary().Name("benchmark_summary").Help("").Register(registry);
auto& summary = summary_family.Add(
{}, Summary::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}});
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(0, 100);
while (state.KeepRunning()) {
auto observation = d(gen);
auto start = std::chrono::high_resolution_clock::now();
summary.Observe(observation);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
BENCHMARK(BM_Summary_Observe_Common)->Iterations(ITERATIONS);
static void BM_Summary_Collect_Common(benchmark::State& state) {
using prometheus::BuildSummary;
using prometheus::Registry;
using prometheus::Summary;
const auto number_of_entries = state.range(0);
Registry registry;
auto& summary_family =
BuildSummary().Name("benchmark_summary").Help("").Register(registry);
auto& summary = summary_family.Add(
{}, Summary::Quantiles{
{0.5, 0.05}, {0.9, 0.01}, {0.95, 0.005}, {0.99, 0.001}});
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(0, 100);
for (auto i = 1; i <= number_of_entries; ++i) summary.Observe(d(gen));
while (state.KeepRunning()) {
benchmark::DoNotOptimize(summary.Collect());
}
}
BENCHMARK(BM_Summary_Collect_Common)->Range(0, ITERATIONS);