xref: /aosp_15_r20/external/cronet/base/json/json_perftest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #include "base/json/json_reader.h"
6*6777b538SAndroid Build Coastguard Worker #include "base/json/json_writer.h"
7*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
8*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_number_conversions.h"
9*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/values.h"
11*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
12*6777b538SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
13*6777b538SAndroid Build Coastguard Worker #include "testing/perf/perf_result_reporter.h"
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker namespace base {
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker namespace {
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker constexpr char kMetricPrefixJSON[] = "JSON.";
20*6777b538SAndroid Build Coastguard Worker constexpr char kMetricReadTime[] = "read_time";
21*6777b538SAndroid Build Coastguard Worker constexpr char kMetricWriteTime[] = "write_time";
22*6777b538SAndroid Build Coastguard Worker 
SetUpReporter(const std::string & story_name)23*6777b538SAndroid Build Coastguard Worker perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) {
24*6777b538SAndroid Build Coastguard Worker   perf_test::PerfResultReporter reporter(kMetricPrefixJSON, story_name);
25*6777b538SAndroid Build Coastguard Worker   reporter.RegisterImportantMetric(kMetricReadTime, "ms");
26*6777b538SAndroid Build Coastguard Worker   reporter.RegisterImportantMetric(kMetricWriteTime, "ms");
27*6777b538SAndroid Build Coastguard Worker   return reporter;
28*6777b538SAndroid Build Coastguard Worker }
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker // Generates a simple dictionary value with simple data types, a string and a
31*6777b538SAndroid Build Coastguard Worker // list.
GenerateDict()32*6777b538SAndroid Build Coastguard Worker Value::Dict GenerateDict() {
33*6777b538SAndroid Build Coastguard Worker   Value::Dict root;
34*6777b538SAndroid Build Coastguard Worker   root.Set("Double", 3.141);
35*6777b538SAndroid Build Coastguard Worker   root.Set("Bool", true);
36*6777b538SAndroid Build Coastguard Worker   root.Set("Int", 42);
37*6777b538SAndroid Build Coastguard Worker   root.Set("String", "Foo");
38*6777b538SAndroid Build Coastguard Worker 
39*6777b538SAndroid Build Coastguard Worker   Value::List list;
40*6777b538SAndroid Build Coastguard Worker   list.Append(2.718);
41*6777b538SAndroid Build Coastguard Worker   list.Append(false);
42*6777b538SAndroid Build Coastguard Worker   list.Append(123);
43*6777b538SAndroid Build Coastguard Worker   list.Append("Bar");
44*6777b538SAndroid Build Coastguard Worker   root.Set("List", std::move(list));
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker   return root;
47*6777b538SAndroid Build Coastguard Worker }
48*6777b538SAndroid Build Coastguard Worker 
49*6777b538SAndroid Build Coastguard Worker // Generates a tree-like dictionary value with a size of O(breadth ** depth).
GenerateLayeredDict(int breadth,int depth)50*6777b538SAndroid Build Coastguard Worker Value::Dict GenerateLayeredDict(int breadth, int depth) {
51*6777b538SAndroid Build Coastguard Worker   if (depth == 1)
52*6777b538SAndroid Build Coastguard Worker     return GenerateDict();
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker   Value::Dict root = GenerateDict();
55*6777b538SAndroid Build Coastguard Worker   Value::Dict next = GenerateLayeredDict(breadth, depth - 1);
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker   for (int i = 0; i < breadth; ++i) {
58*6777b538SAndroid Build Coastguard Worker     root.Set("Dict" + base::NumberToString(i), next.Clone());
59*6777b538SAndroid Build Coastguard Worker   }
60*6777b538SAndroid Build Coastguard Worker 
61*6777b538SAndroid Build Coastguard Worker   return root;
62*6777b538SAndroid Build Coastguard Worker }
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker }  // namespace
65*6777b538SAndroid Build Coastguard Worker 
66*6777b538SAndroid Build Coastguard Worker class JSONPerfTest : public testing::Test {
67*6777b538SAndroid Build Coastguard Worker  public:
TestWriteAndRead(int breadth,int depth)68*6777b538SAndroid Build Coastguard Worker   void TestWriteAndRead(int breadth, int depth) {
69*6777b538SAndroid Build Coastguard Worker     std::string description = "Breadth: " + base::NumberToString(breadth) +
70*6777b538SAndroid Build Coastguard Worker                               ", Depth: " + base::NumberToString(depth);
71*6777b538SAndroid Build Coastguard Worker     Value::Dict dict = GenerateLayeredDict(breadth, depth);
72*6777b538SAndroid Build Coastguard Worker     std::string json;
73*6777b538SAndroid Build Coastguard Worker 
74*6777b538SAndroid Build Coastguard Worker     TimeTicks start_write = TimeTicks::Now();
75*6777b538SAndroid Build Coastguard Worker     JSONWriter::Write(dict, &json);
76*6777b538SAndroid Build Coastguard Worker     TimeTicks end_write = TimeTicks::Now();
77*6777b538SAndroid Build Coastguard Worker     auto reporter = SetUpReporter("breadth_" + base::NumberToString(breadth) +
78*6777b538SAndroid Build Coastguard Worker                                   "_depth_" + base::NumberToString(depth));
79*6777b538SAndroid Build Coastguard Worker     reporter.AddResult(kMetricWriteTime, end_write - start_write);
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker     TimeTicks start_read = TimeTicks::Now();
82*6777b538SAndroid Build Coastguard Worker     JSONReader::Read(json);
83*6777b538SAndroid Build Coastguard Worker     TimeTicks end_read = TimeTicks::Now();
84*6777b538SAndroid Build Coastguard Worker     reporter.AddResult(kMetricReadTime, end_read - start_read);
85*6777b538SAndroid Build Coastguard Worker   }
86*6777b538SAndroid Build Coastguard Worker };
87*6777b538SAndroid Build Coastguard Worker 
TEST_F(JSONPerfTest,StressTest)88*6777b538SAndroid Build Coastguard Worker TEST_F(JSONPerfTest, StressTest) {
89*6777b538SAndroid Build Coastguard Worker   // These loop ranges are chosen such that this test will complete in a
90*6777b538SAndroid Build Coastguard Worker   // reasonable amount of time and will work on a 32-bit build without hitting
91*6777b538SAndroid Build Coastguard Worker   // an out-of-memory failure. Having j go to 10 uses over 2 GiB of memory and
92*6777b538SAndroid Build Coastguard Worker   // might hit Android timeouts so be wary of going that high.
93*6777b538SAndroid Build Coastguard Worker   for (int i = 0; i < 4; ++i) {
94*6777b538SAndroid Build Coastguard Worker     for (int j = 0; j < 10; ++j) {
95*6777b538SAndroid Build Coastguard Worker       TestWriteAndRead(i + 1, j + 1);
96*6777b538SAndroid Build Coastguard Worker     }
97*6777b538SAndroid Build Coastguard Worker   }
98*6777b538SAndroid Build Coastguard Worker }
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker }  // namespace base
101