Skip to content

Commit 2600198

Browse files
authored
perf: Optimize bench logger code for more realistic scenario (#2412)
1 parent c83be63 commit 2600198

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

benchmarks/util/log/LoggerBenchmark.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ struct BenchmarkLoggingInitializer {
4141
static void
4242
initFileLogging(LogService::FileLoggingParams const& params)
4343
{
44-
boost::log::add_common_attributes();
45-
std::filesystem::create_directories(params.logDir);
4644
LogService::initFileLogging(params, kLOG_FORMAT);
4745
}
4846
};
@@ -64,6 +62,8 @@ uniqueLogDir()
6462
static void
6563
benchmarkConcurrentFileLogging(benchmark::State& state)
6664
{
65+
boost::log::add_common_attributes();
66+
6767
auto const numThreads = static_cast<size_t>(state.range(0));
6868
auto const messagesPerThread = static_cast<size_t>(state.range(1));
6969

@@ -72,21 +72,24 @@ benchmarkConcurrentFileLogging(benchmark::State& state)
7272
auto const logDir = uniqueLogDir();
7373
for (auto _ : state) {
7474
state.PauseTiming();
75-
std::filesystem::remove_all(logDir);
75+
76+
std::filesystem::create_directories(logDir);
7677

7778
BenchmarkLoggingInitializer::initFileLogging({
7879
.logDir = logDir,
79-
.rotationSizeMB = 1,
80-
.dirMaxSizeMB = 10,
80+
.rotationSizeMB = 5,
81+
.dirMaxSizeMB = 125,
8182
.rotationHours = 24,
8283
});
83-
state.ResumeTiming();
8484

8585
std::vector<std::thread> threads;
8686
threads.reserve(numThreads);
8787

8888
std::chrono::high_resolution_clock::time_point start;
89-
std::barrier barrier(numThreads, [&start]() { start = std::chrono::high_resolution_clock::now(); });
89+
std::barrier barrier(numThreads, [&state, &start]() {
90+
state.ResumeTiming();
91+
start = std::chrono::high_resolution_clock::now();
92+
});
9093

9194
for (size_t threadNum = 0; threadNum < numThreads; ++threadNum) {
9295
threads.emplace_back([threadNum, messagesPerThread, &barrier]() {
@@ -108,20 +111,25 @@ benchmarkConcurrentFileLogging(benchmark::State& state)
108111
auto const end = std::chrono::high_resolution_clock::now();
109112

110113
state.SetIterationTime(std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count());
111-
}
112114

113-
std::filesystem::remove_all(logDir);
115+
std::filesystem::remove_all(logDir);
116+
}
114117

115118
auto const totalMessages = numThreads * messagesPerThread;
116119
state.counters["TotalMessagesRate"] = benchmark::Counter(totalMessages, benchmark::Counter::kIsRate);
117120
state.counters["Threads"] = numThreads;
118121
state.counters["MessagesPerThread"] = messagesPerThread;
119122
}
120123

124+
// One line of log message is around 110 bytes
125+
// So, 100K messages is around 10.5MB
126+
121127
BENCHMARK(benchmarkConcurrentFileLogging)
122128
->ArgsProduct({
123-
{1, 2, 4, 8}, // Number of threads
124-
{500, 1000, 2000, 100000}, // Messages per thread
129+
// Number of threads
130+
{1, 2, 4, 8},
131+
// Messages per thread
132+
{10'000, 100'000, 500'000},
125133
})
126134
->UseManualTime()
127135
->Unit(benchmark::kMillisecond);

0 commit comments

Comments
 (0)