main.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <tools/enum_parser/parse_enum/benchmark/enum.h_serialized.h>
  2. #include <library/cpp/testing/benchmark/bench.h>
  3. #include <util/generic/algorithm.h>
  4. #include <util/generic/vector.h>
  5. #include <util/generic/xrange.h>
  6. #include <util/stream/null.h>
  7. #include <util/string/cast.h>
  8. namespace {
  9. template <class TEnum>
  10. TVector<TEnum> SelectValues(size_t count) {
  11. auto values = GetEnumAllValues<TEnum>().Materialize();
  12. SortBy(values, [](const TEnum& v) { return IntHash(static_cast<ui64>(v)); });
  13. values.crop(count);
  14. return values;
  15. }
  16. template <class TEnum>
  17. TVector<TStringBuf> SelectStrings(size_t count) {
  18. TVector<TStringBuf> strings(Reserve(GetEnumItemsCount<TEnum>()));
  19. for (const auto& [_, s] : GetEnumNames<TEnum>()) {
  20. strings.push_back(s);
  21. }
  22. SortBy(strings, [](const TStringBuf& s) { return THash<TStringBuf>()(s); });
  23. strings.crop(count);
  24. return strings;
  25. }
  26. template <class TEnum, class TContext>
  27. void BMToString(TContext& iface) {
  28. const auto values = SelectValues<TEnum>(5u);
  29. for (const auto iter : xrange(iface.Iterations())) {
  30. Y_UNUSED(iter);
  31. for (const auto value : values) {
  32. Y_DO_NOT_OPTIMIZE_AWAY(ToString(value).size());
  33. }
  34. }
  35. }
  36. template <class TEnum, class TContext>
  37. void BMOut(TContext& iface) {
  38. const auto values = SelectValues<TEnum>(5u);
  39. TNullOutput null;
  40. for (const auto iter : xrange(iface.Iterations())) {
  41. Y_UNUSED(iter);
  42. for (const auto value : values) {
  43. Y_DO_NOT_OPTIMIZE_AWAY(null << value);
  44. }
  45. }
  46. }
  47. template <class TEnum, class TContext>
  48. void BMFromString(TContext& iface) {
  49. const auto strings = SelectStrings<TEnum>(5u);
  50. for (const auto iter : xrange(iface.Iterations())) {
  51. Y_UNUSED(iter);
  52. for (const auto s : strings) {
  53. Y_DO_NOT_OPTIMIZE_AWAY(FromString<TEnum>(s));
  54. }
  55. }
  56. }
  57. template <class TEnum, class TContext>
  58. void BMTryFromString(TContext& iface) {
  59. auto strings = SelectStrings<TEnum>(5u);
  60. strings.back() = "fake";
  61. TEnum value;
  62. for (const auto iter : xrange(iface.Iterations())) {
  63. Y_UNUSED(iter);
  64. for (const auto s : strings) {
  65. Y_DO_NOT_OPTIMIZE_AWAY(TryFromString<TEnum>(s, value));
  66. }
  67. }
  68. }
  69. }
  70. #define DEFINE_BENCHMARK(name) \
  71. Y_CPU_BENCHMARK(ToString_##name, iface) { \
  72. BMToString<name>(iface); \
  73. } \
  74. Y_CPU_BENCHMARK(Out_##name, iface) { \
  75. BMOut<name>(iface); \
  76. } \
  77. Y_CPU_BENCHMARK(FromString_##name, iface) { \
  78. BMFromString<name>(iface); \
  79. } \
  80. Y_CPU_BENCHMARK(TryFromString_##name, iface) { \
  81. BMTryFromString<name>(iface); \
  82. }
  83. DEFINE_BENCHMARK(ESmallSortedEnum);
  84. DEFINE_BENCHMARK(ESmalUnsortedEnum);
  85. DEFINE_BENCHMARK(EBigSortedEnum);
  86. DEFINE_BENCHMARK(EBigUnsortedEnum);
  87. DEFINE_BENCHMARK(EBigUnsortedDenseEnum);