-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
56 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |