aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime/benchmark/format/main.cpp
blob: 9c930d47140eeaea06eb4915e20430453bc90d56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);