From 2600198bd5ca0a6b2ffc2c357bb43dfb98a46b9b Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Fri, 8 Aug 2025 12:21:06 +0100 Subject: [PATCH] perf: Optimize bench logger code for more realistic scenario (#2412) --- benchmarks/util/log/LoggerBenchmark.cpp | 30 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/benchmarks/util/log/LoggerBenchmark.cpp b/benchmarks/util/log/LoggerBenchmark.cpp index 710d88c7..5953910e 100644 --- a/benchmarks/util/log/LoggerBenchmark.cpp +++ b/benchmarks/util/log/LoggerBenchmark.cpp @@ -41,8 +41,6 @@ struct BenchmarkLoggingInitializer { static void initFileLogging(LogService::FileLoggingParams const& params) { - boost::log::add_common_attributes(); - std::filesystem::create_directories(params.logDir); LogService::initFileLogging(params, kLOG_FORMAT); } }; @@ -64,6 +62,8 @@ uniqueLogDir() static void benchmarkConcurrentFileLogging(benchmark::State& state) { + boost::log::add_common_attributes(); + auto const numThreads = static_cast(state.range(0)); auto const messagesPerThread = static_cast(state.range(1)); @@ -72,21 +72,24 @@ benchmarkConcurrentFileLogging(benchmark::State& state) auto const logDir = uniqueLogDir(); for (auto _ : state) { state.PauseTiming(); - std::filesystem::remove_all(logDir); + + std::filesystem::create_directories(logDir); BenchmarkLoggingInitializer::initFileLogging({ .logDir = logDir, - .rotationSizeMB = 1, - .dirMaxSizeMB = 10, + .rotationSizeMB = 5, + .dirMaxSizeMB = 125, .rotationHours = 24, }); - state.ResumeTiming(); std::vector threads; threads.reserve(numThreads); std::chrono::high_resolution_clock::time_point start; - std::barrier barrier(numThreads, [&start]() { start = std::chrono::high_resolution_clock::now(); }); + std::barrier barrier(numThreads, [&state, &start]() { + state.ResumeTiming(); + start = std::chrono::high_resolution_clock::now(); + }); for (size_t threadNum = 0; threadNum < numThreads; ++threadNum) { threads.emplace_back([threadNum, messagesPerThread, &barrier]() { @@ -108,9 +111,9 @@ benchmarkConcurrentFileLogging(benchmark::State& state) auto const end = std::chrono::high_resolution_clock::now(); state.SetIterationTime(std::chrono::duration_cast>(end - start).count()); - } - std::filesystem::remove_all(logDir); + std::filesystem::remove_all(logDir); + } auto const totalMessages = numThreads * messagesPerThread; state.counters["TotalMessagesRate"] = benchmark::Counter(totalMessages, benchmark::Counter::kIsRate); @@ -118,10 +121,15 @@ benchmarkConcurrentFileLogging(benchmark::State& state) state.counters["MessagesPerThread"] = messagesPerThread; } +// One line of log message is around 110 bytes +// So, 100K messages is around 10.5MB + BENCHMARK(benchmarkConcurrentFileLogging) ->ArgsProduct({ - {1, 2, 4, 8}, // Number of threads - {500, 1000, 2000, 100000}, // Messages per thread + // Number of threads + {1, 2, 4, 8}, + // Messages per thread + {10'000, 100'000, 500'000}, }) ->UseManualTime() ->Unit(benchmark::kMillisecond);