json2yson_ut.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "library/cpp/json/yson/json2yson.h"
  2. #include <library/cpp/blockcodecs/codecs.h>
  3. #include <library/cpp/histogram/simple/histogram.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <library/cpp/testing/unittest/tests_data.h>
  6. #include <util/datetime/cputimer.h>
  7. #include <util/stream/file.h>
  8. template <typename TCallBack>
  9. ui64 Run(TCallBack&& callBack) {
  10. TSimpleTimer timer;
  11. callBack();
  12. return timer.Get().MicroSeconds();
  13. }
  14. static TString GetRequestsWithDecoding(const TString& inputPath, const NBlockCodecs::ICodec* codec) {
  15. TIFStream inputFileStream(inputPath);
  16. TString encodedRequests = inputFileStream.ReadAll();
  17. TString requests;
  18. codec->Decode(encodedRequests, requests);
  19. return requests;
  20. }
  21. Y_UNIT_TEST_SUITE(Json2Yson) {
  22. Y_UNIT_TEST(NOAPACHE_REQUESTS) {
  23. const ui32 warmUpRetries = 5;
  24. const TVector<double> percentiles = {0.25, 0.5, 0.6, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 0.97, 0.99, 1.0};
  25. NSimpleHistogram::TMultiHistogramCalcer<ui64> calcer;
  26. TString requests = GetRequestsWithDecoding(GetWorkPath() + "/noapache_requests_sample_lz4", NBlockCodecs::Codec("lz4"));
  27. TStringInput inputStream(requests);
  28. for (TString jsonRequest, jsonString, ysonString; inputStream.ReadLine(jsonRequest);) {
  29. TStringInput jsonInput(jsonRequest);
  30. NJson::TJsonValue readedJson;
  31. NJson::ReadJsonTree(&jsonInput, &readedJson, true);
  32. jsonRequest.clear();
  33. ui64 writeTime = Max<ui64>();
  34. ui64 readTime = Max<ui64>();
  35. for (ui32 i = 0; i < warmUpRetries; ++i) {
  36. NJson::TJsonValue Json2Json;
  37. TStringOutput jsonWriteOutput(jsonString);
  38. NJsonWriter::TBuf jsonBuf(NJsonWriter::HEM_UNSAFE, &jsonWriteOutput);
  39. writeTime = Min(writeTime, Run([&]() {
  40. jsonBuf.WriteJsonValue(&readedJson);
  41. }));
  42. TStringInput jsonInput(jsonString);
  43. NJson::TJsonReaderConfig config;
  44. config.DontValidateUtf8 = true;
  45. readTime = Min(readTime, Run([&]() {
  46. NJson::ReadJsonTree(&jsonInput, &config, &Json2Json, true);
  47. }));
  48. UNIT_ASSERT_VALUES_EQUAL(
  49. NJsonWriter::TBuf().WriteJsonValue(&readedJson, true).Str(),
  50. NJsonWriter::TBuf().WriteJsonValue(&Json2Json, true).Str());
  51. jsonString.clear();
  52. }
  53. calcer.RecordValue("read_json", readTime);
  54. calcer.RecordValue("write_json", writeTime);
  55. calcer.RecordValue("read_and_write_json", readTime + writeTime);
  56. writeTime = Max<ui64>();
  57. readTime = Max<ui64>();
  58. for (ui32 i = 0; i < warmUpRetries; ++i) {
  59. NJson::TJsonValue convertedJson;
  60. TStringOutput ysonOutput(ysonString);
  61. writeTime = Min(writeTime, Run([&]() {
  62. NJson2Yson::SerializeJsonValueAsYson(readedJson, &ysonOutput);
  63. }));
  64. TStringInput ysonInput(ysonString);
  65. readTime = Min(readTime, Run([&]() {
  66. NJson2Yson::DeserializeYsonAsJsonValue(&ysonInput, &convertedJson);
  67. }));
  68. UNIT_ASSERT_VALUES_EQUAL(
  69. NJsonWriter::TBuf().WriteJsonValue(&convertedJson, true).Str(),
  70. NJsonWriter::TBuf().WriteJsonValue(&readedJson, true).Str());
  71. ysonString.clear();
  72. }
  73. calcer.RecordValue("read_yson", readTime);
  74. calcer.RecordValue("write_yson", writeTime);
  75. calcer.RecordValue("read_and_write_yson", readTime + writeTime);
  76. }
  77. NJson::TJsonValue histogramJson = NSimpleHistogram::ToJson(calcer.Calc(), percentiles);
  78. for (const auto& it : histogramJson.GetMap()) {
  79. for (const auto& percentileValue : it.second.GetMap()) {
  80. UNIT_ADD_METRIC(it.first + "_" + percentileValue.first, percentileValue.second.GetUInteger() / 1000.0);
  81. }
  82. }
  83. }
  84. }