main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <yql/essentials/minikql/dom/json.h>
  2. #include <yql/essentials/minikql/jsonpath/jsonpath.h>
  3. #include <yql/essentials/minikql/computation/mkql_value_builder.h>
  4. #include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
  5. #include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
  6. #include <yql/essentials/minikql/mkql_mem_info.h>
  7. #include <yql/essentials/minikql/mkql_function_registry.h>
  8. #include <yql/essentials/minikql/mkql_alloc.h>
  9. #include <yql/essentials/minikql/mkql_node.h>
  10. #include <library/cpp/json/json_value.h>
  11. #include <library/cpp/testing/benchmark/bench.h>
  12. #include <util/random/fast.h>
  13. using namespace NJson;
  14. using namespace NYql;
  15. using namespace NYql::NDom;
  16. using namespace NYql::NUdf;
  17. using namespace NYql::NJsonPath;
  18. using namespace NJson;
  19. using namespace NKikimr::NMiniKQL;
  20. TString RandomString(ui32 min, ui32 max) {
  21. static TReallyFastRng32 rand(0);
  22. TString result;
  23. const ui32 length = rand.Uniform(min, max + 1);
  24. result.reserve(length);
  25. for (ui32 i = 0; i < length; ++i) {
  26. result.push_back(char(rand.Uniform('a', 'z' + 1)));
  27. }
  28. return result;
  29. }
  30. TString RandomString(ui32 length) {
  31. return RandomString(length, length);
  32. }
  33. TString GenerateRandomJson() {
  34. TJsonMap result;
  35. TJsonMap id;
  36. id.InsertValue("id", TJsonValue(RandomString(24)));
  37. id.InsertValue("issueId", TJsonValue(RandomString(24)));
  38. result.InsertValue("_id", std::move(id));
  39. result.InsertValue("@class", TJsonValue(RandomString(60)));
  40. result.InsertValue("author", TJsonValue(RandomString(10)));
  41. result.InsertValue("transitionId", TJsonValue(RandomString(24)));
  42. TJsonArray comments;
  43. for (ui32 i = 0; i < 30; i++) {
  44. TJsonMap comment;
  45. comment.InsertValue("id", TJsonValue(RandomString(24)));
  46. comment.InsertValue("newText", TJsonValue(RandomString(150)));
  47. comments.AppendValue(std::move(comment));
  48. }
  49. TJsonMap changes;
  50. changes.InsertValue("comment", std::move(comments));
  51. result.InsertValue("changes", std::move(changes));
  52. return result.GetStringRobust();
  53. }
  54. const size_t MAX_PARSE_ERRORS = 100;
  55. #define PREPARE() \
  56. TIntrusivePtr<IFunctionRegistry> FunctionRegistry(CreateFunctionRegistry(CreateBuiltinRegistry())); \
  57. TScopedAlloc Alloc(__LOCATION__); \
  58. TTypeEnvironment Env(Alloc); \
  59. TMemoryUsageInfo MemInfo("Memory"); \
  60. THolderFactory HolderFactory(Alloc.Ref(), MemInfo, FunctionRegistry.Get()); \
  61. TDefaultValueBuilder ValueBuilder(HolderFactory); \
  62. Y_CPU_BENCHMARK(JsonPath, iface) {
  63. PREPARE()
  64. const TString json = GenerateRandomJson();
  65. const TUnboxedValue dom = TryParseJsonDom(json, &ValueBuilder);
  66. for (size_t i = 0; i < iface.Iterations(); i++) {
  67. TIssues issues;
  68. const auto jsonPath = ParseJsonPath("$.'_id'.issueId", issues, MAX_PARSE_ERRORS);
  69. const auto result = ExecuteJsonPath(jsonPath, TValue(dom), TVariablesMap(), &ValueBuilder);
  70. Y_ABORT_UNLESS(!result.IsError());
  71. }
  72. }
  73. Y_CPU_BENCHMARK(JsonPathLikeRegexWithCompile, iface) {
  74. PREPARE()
  75. const TString json = GenerateRandomJson();
  76. const TUnboxedValue dom = TryParseJsonDom(json, &ValueBuilder);
  77. for (size_t i = 0; i < iface.Iterations(); i++) {
  78. TIssues issues;
  79. const auto jsonPath = ParseJsonPath("$[*] like_regex \"[0-9]+\"", issues, MAX_PARSE_ERRORS);
  80. const auto result = ExecuteJsonPath(jsonPath, TValue(dom), TVariablesMap(), &ValueBuilder);
  81. Y_ABORT_UNLESS(!result.IsError());
  82. }
  83. }
  84. Y_CPU_BENCHMARK(JsonPathLikeRegex, iface) {
  85. PREPARE()
  86. const TString json = GenerateRandomJson();
  87. const TUnboxedValue dom = TryParseJsonDom(json, &ValueBuilder);
  88. TIssues issues;
  89. const auto jsonPath = ParseJsonPath("$[*] like_regex \"[0-9]+\"", issues, MAX_PARSE_ERRORS);
  90. for (size_t i = 0; i < iface.Iterations(); i++) {
  91. const auto result = ExecuteJsonPath(jsonPath, TValue(dom), TVariablesMap(), &ValueBuilder);
  92. Y_ABORT_UNLESS(!result.IsError());
  93. }
  94. }