Skip to content

Commit

Permalink
Improve benchmark for StringPool
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Nov 4, 2024
1 parent 1bf84c1 commit 3bd3197
Showing 1 changed file with 56 additions and 27 deletions.
83 changes: 56 additions & 27 deletions benchmark/src/benchmarks/benchmark.stringpool.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,102 @@
#include <benchmark/benchmark.h>
#include <random>
#include <zasm/core/stringpool.hpp>

namespace zasm::benchmarks
{
static const std::string TestStrings[] = { "hello", "world", "longer string", "even longer string",
"even longer longer string" };
static constexpr auto kTestSize = 500'000;
static constexpr const char kChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

static const std::vector<std::string> kInputStrings = []() {
std::vector<std::string> strings;

std::mt19937 prng(42);
for (int i = 0; i < kTestSize; ++i)
{
std::string str;
for (size_t i = 0; i < 4 + (prng() % 24); ++i)
{
str.push_back(kChars[prng() % (sizeof(kChars) - 1)]);
}
strings.push_back(std::move(str));
}
return strings;
}();

static void BM_StringPool_Aquire(benchmark::State& state)
{
StringPool pool;
for (auto _ : state)
{
const auto& str = TestStrings[state.range(0)];
for (auto i = 0; i < state.range(0); ++i)
{
const auto& str = kInputStrings[i];

auto stringId = pool.aquire(str);
benchmark::DoNotOptimize(stringId);
auto stringId = pool.aquire(str);
benchmark::DoNotOptimize(stringId);
}
}
}
BENCHMARK(BM_StringPool_Aquire)->DenseRange(0, std::size(TestStrings) - 1);
BENCHMARK(BM_StringPool_Aquire)->Range(0, kTestSize)->Unit(benchmark::kMillisecond);

static void BM_StringPool_Release(benchmark::State& state)
{
StringPool pool;
for (auto _ : state)
{
state.PauseTiming();
const auto& str = TestStrings[state.range(0)];
for (auto i = 0; i < state.range(0); ++i)
{
state.PauseTiming();
const auto& str = kInputStrings[i];

auto stringId = pool.aquire(str);
state.ResumeTiming();
auto stringId = pool.aquire(str);
state.ResumeTiming();

auto refCount = pool.release(stringId);
benchmark::DoNotOptimize(refCount);
auto refCount = pool.release(stringId);
benchmark::DoNotOptimize(refCount);
}
}
}
BENCHMARK(BM_StringPool_Release)->DenseRange(0, std::size(TestStrings) - 1);
BENCHMARK(BM_StringPool_Release)->Range(0, kTestSize)->Unit(benchmark::kMillisecond);

static void BM_StringPool_Get(benchmark::State& state)
{
StringPool pool;
for (auto _ : state)
{
state.PauseTiming();
const auto& str = TestStrings[state.range(0)];
for (auto i = 0; i < state.range(0); ++i)
{
state.PauseTiming();
const auto& str = kInputStrings[i];

auto stringId = pool.aquire(str);
state.ResumeTiming();
auto stringId = pool.aquire(str);
state.ResumeTiming();

const char* res = pool.get(stringId);
benchmark::DoNotOptimize(res);
const char* res = pool.get(stringId);
benchmark::DoNotOptimize(res);
}
}
}
BENCHMARK(BM_StringPool_Get)->DenseRange(0, std::size(TestStrings) - 1);
BENCHMARK(BM_StringPool_Get)->Range(0, kTestSize)->Unit(benchmark::kMillisecond);

static void BM_StringPool_GetLength(benchmark::State& state)
{
StringPool pool;
for (auto _ : state)
{
state.PauseTiming();
const auto& str = TestStrings[state.range(0)];
for (auto i = 0; i < state.range(0); ++i)
{
state.PauseTiming();
const auto& str = kInputStrings[i];

auto stringId = pool.aquire(str);
state.ResumeTiming();
auto stringId = pool.aquire(str);
state.ResumeTiming();

auto strLen = pool.getLength(stringId);
benchmark::DoNotOptimize(strLen);
auto strLen = pool.getLength(stringId);
benchmark::DoNotOptimize(strLen);
}
}
}
BENCHMARK(BM_StringPool_GetLength)->DenseRange(0, std::size(TestStrings) - 1);
BENCHMARK(BM_StringPool_GetLength)->Range(0, kTestSize)->Unit(benchmark::kMillisecond);

} // namespace zasm::benchmarks

0 comments on commit 3bd3197

Please sign in to comment.