aboutsummaryrefslogtreecommitdiffstats
path: root/tools/enum_parser/parse_enum/benchmark/main.cpp
blob: 9f2694f236ac19d4d213dfb9c47f09fe7eb71f22 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <tools/enum_parser/parse_enum/benchmark/enum.h_serialized.h>
#include <library/cpp/testing/benchmark/bench.h>
#include <util/generic/algorithm.h>
#include <util/generic/vector.h>
#include <util/generic/xrange.h>
#include <util/stream/null.h>
#include <util/string/cast.h>

namespace {

    template <class TEnum>
    TVector<TEnum> SelectValues(size_t count) {
        auto values = GetEnumAllValues<TEnum>().Materialize();
        SortBy(values, [](const TEnum& v) { return IntHash(static_cast<ui64>(v)); });
        values.crop(count);
        return values;
    }

    template <class TEnum>
    TVector<TStringBuf> SelectStrings(size_t count) {
        TVector<TStringBuf> strings(Reserve(GetEnumItemsCount<TEnum>()));
        for (const auto& [_, s] : GetEnumNames<TEnum>()) {
            strings.push_back(s);
        }
        SortBy(strings, [](const TStringBuf& s) { return THash<TStringBuf>()(s); });
        strings.crop(count);
        return strings;
    }

    template <class TEnum, class TContext>
    void BMToString(TContext& iface) {
        const auto values = SelectValues<TEnum>(5u);
        for (const auto iter : xrange(iface.Iterations())) {
            Y_UNUSED(iter);
            for (const auto value : values) {
                Y_DO_NOT_OPTIMIZE_AWAY(ToString(value).size());
            }
        }
    }

    template <class TEnum, class TContext>
    void BMOut(TContext& iface) {
        const auto values = SelectValues<TEnum>(5u);
        TNullOutput null;
        for (const auto iter : xrange(iface.Iterations())) {
            Y_UNUSED(iter);
            for (const auto value : values) {
                Y_DO_NOT_OPTIMIZE_AWAY(null << value);
            }
        }
    }

    template <class TEnum, class TContext>
    void BMFromString(TContext& iface) {
        const auto strings = SelectStrings<TEnum>(5u);
        for (const auto iter : xrange(iface.Iterations())) {
            Y_UNUSED(iter);
            for (const auto s : strings) {
                Y_DO_NOT_OPTIMIZE_AWAY(FromString<TEnum>(s));
            }
        }
    }

    template <class TEnum, class TContext>
    void BMTryFromString(TContext& iface) {
        auto strings = SelectStrings<TEnum>(5u);
        strings.back() = "fake";

        TEnum value;
        for (const auto iter : xrange(iface.Iterations())) {
            Y_UNUSED(iter);
            for (const auto s : strings) {
                Y_DO_NOT_OPTIMIZE_AWAY(TryFromString<TEnum>(s, value));
            }
        }
    }
}

#define DEFINE_BENCHMARK(name)                     \
    Y_CPU_BENCHMARK(ToString_##name, iface) {      \
        BMToString<name>(iface);                   \
    }                                              \
    Y_CPU_BENCHMARK(Out_##name, iface) {           \
        BMOut<name>(iface);                        \
    }                                              \
    Y_CPU_BENCHMARK(FromString_##name, iface) {    \
        BMFromString<name>(iface);                 \
    }                                              \
    Y_CPU_BENCHMARK(TryFromString_##name, iface) { \
        BMTryFromString<name>(iface);              \
    }

DEFINE_BENCHMARK(ESmallSortedEnum);
DEFINE_BENCHMARK(ESmalUnsortedEnum);
DEFINE_BENCHMARK(EBigSortedEnum);
DEFINE_BENCHMARK(EBigUnsortedEnum);
DEFINE_BENCHMARK(EBigUnsortedDenseEnum);