diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-03-22 18:57:39 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-03-22 19:07:02 +0300 |
commit | 3440878efbe2d889da7070f8f8c1f09c7d162a68 (patch) | |
tree | 78d4a3e0677bf6c35ec3997d176502f00b7ef862 | |
parent | e20d8d7107fe83f612b832be904fac0257138f30 (diff) | |
download | ydb-3440878efbe2d889da7070f8f8c1f09c7d162a68.tar.gz |
Intermediate changes
-rw-r--r-- | util/datetime/benchmark/format/main.cpp | 59 | ||||
-rw-r--r-- | util/datetime/benchmark/format/ya.make | 12 | ||||
-rw-r--r-- | util/datetime/benchmark/gmtime_r/main.cpp | 35 | ||||
-rw-r--r-- | util/datetime/benchmark/gmtime_r/ya.make | 2 | ||||
-rw-r--r-- | util/datetime/benchmark/ya.make | 1 |
5 files changed, 102 insertions, 7 deletions
diff --git a/util/datetime/benchmark/format/main.cpp b/util/datetime/benchmark/format/main.cpp new file mode 100644 index 0000000000..9c930d4714 --- /dev/null +++ b/util/datetime/benchmark/format/main.cpp @@ -0,0 +1,59 @@ +#include <library/cpp/testing/gbenchmark/benchmark.h> + +#include <util/datetime/base.h> +#include <util/stream/str.h> + +class TTimestampGenerator { +public: + TInstant operator()() { + TInstant result = TInstant::MicroSeconds(Base_ + Current_); + Current_ = (Current_ + Step_) % Range_; + return result; + } + +private: + static constexpr ui64 Step_ = TDuration::MicroSeconds(1234567891011).MicroSeconds(); + static constexpr ui64 Range_ = 1ull << 45; // ~ year + static constexpr ui64 Base_ = TInstant::Seconds(1605320321).MicroSeconds(); + ui64 Current_ = 0; +}; + +Y_FORCE_INLINE static void BenchFormatStream(auto&& formatFn, benchmark::State& state) { + TTimestampGenerator gen; + TStringStream ss; + for (auto _ : state) { + ss << formatFn(gen()); + Y_DO_NOT_OPTIMIZE_AWAY(ss.Str()); + ss.clear(); + } +} + +Y_FORCE_INLINE static void BenchToString(auto&& toStringFn, benchmark::State& state) { + TTimestampGenerator gen; + TString s; + for (auto _ : state) { + s = toStringFn(gen()); + Y_DO_NOT_OPTIMIZE_AWAY(s); + } +} + +void BM_FormatIsoLocal(benchmark::State& state) { + BenchFormatStream(FormatIsoLocal, state); +} + +void BM_FormatLocal(benchmark::State& state) { + BenchFormatStream(FormatLocal, state); +} + +void BM_ToStringIsoLocal(benchmark::State& state) { + BenchToString(std::mem_fn(&TInstant::ToIsoStringLocal), state); +} + +void BM_ToStringLocal(benchmark::State& state) { + BenchToString(std::mem_fn(&TInstant::ToIsoStringLocal), state); +} + +BENCHMARK(BM_FormatIsoLocal); +BENCHMARK(BM_FormatLocal); +BENCHMARK(BM_ToStringIsoLocal); +BENCHMARK(BM_ToStringLocal); diff --git a/util/datetime/benchmark/format/ya.make b/util/datetime/benchmark/format/ya.make new file mode 100644 index 0000000000..ef5e7b0c93 --- /dev/null +++ b/util/datetime/benchmark/format/ya.make @@ -0,0 +1,12 @@ +SUBSCRIBER(g:util-subscribers) + +G_BENCHMARK() + +PEERDIR( +) + +SRCS( + main.cpp +) + +END() diff --git a/util/datetime/benchmark/gmtime_r/main.cpp b/util/datetime/benchmark/gmtime_r/main.cpp index a649ab3ccf..a3356d780c 100644 --- a/util/datetime/benchmark/gmtime_r/main.cpp +++ b/util/datetime/benchmark/gmtime_r/main.cpp @@ -1,21 +1,44 @@ -#include <library/cpp/testing/benchmark/bench.h> +#include <library/cpp/testing/gbenchmark/benchmark.h> -#include <util/draft/datetime.h> +#include <util/datetime/base.h> +#include <util/random/fast.h> -Y_CPU_BENCHMARK(GmTimeR, iface) { +void BM_GmTimeR(benchmark::State& state) { time_t now = TInstant::Now().TimeT(); struct tm buf {}; - for (size_t i = 0; i < iface.Iterations(); ++i) { + for (auto _ : state) { Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&now, &buf)); } } -Y_CPU_BENCHMARK(gmtime_r, iface) { +void BM_gmtime_r(benchmark::State& state) { time_t now = TInstant::Now().TimeT(); struct tm buf {}; - for (size_t i = 0; i < iface.Iterations(); ++i) { + for (auto _ : state) { Y_DO_NOT_OPTIMIZE_AWAY(gmtime_r(&now, &buf)); } } + +void BM_GmTimeRRandom(benchmark::State& state, TDuration window) { + time_t now = TInstant::Now().TimeT(); + struct tm buf {}; + + TFastRng<ui32> rng(2); + const size_t range = window.Seconds(); + for (auto _ : state) { + size_t offset = rng.GenRand() % range; + time_t v = now - offset; + Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&v, &buf)); + } +} + +BENCHMARK(BM_GmTimeR); +BENCHMARK(BM_gmtime_r); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_hour, TDuration::Hours(1)); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_day, TDuration::Days(1)); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_mount, TDuration::Days(31)); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_year, TDuration::Days(365)); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_decade, TDuration::Days(3653)); +BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_half_centry, TDuration::Days(18262)); diff --git a/util/datetime/benchmark/gmtime_r/ya.make b/util/datetime/benchmark/gmtime_r/ya.make index 32acd7c1c0..5c86c85db0 100644 --- a/util/datetime/benchmark/gmtime_r/ya.make +++ b/util/datetime/benchmark/gmtime_r/ya.make @@ -1,6 +1,6 @@ SUBSCRIBER(g:util-subscribers) -Y_BENCHMARK() +G_BENCHMARK() PEERDIR( util/draft diff --git a/util/datetime/benchmark/ya.make b/util/datetime/benchmark/ya.make index 6c9ed6cfd4..a94bc6f0b6 100644 --- a/util/datetime/benchmark/ya.make +++ b/util/datetime/benchmark/ya.make @@ -1,5 +1,6 @@ SUBSCRIBER(g:util-subscribers) RECURSE( + format gmtime_r ) |