| 6 | #include "base/json/json_writer.h" |
| 7 | #include "base/memory/ptr_util.h" |
| 8 | #include "base/strings/string_number_conversions.h" |
| 9 | #include "base/time/time.h" |
| 10 | #include "base/values.h" |
| 11 | #include "build/build_config.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "testing/perf/perf_result_reporter.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | namespace { |
| 18 |
| 19 | constexpr char kMetricPrefixJSON[] = "JSON."; |
| 20 | constexpr char kMetricReadTime[] = "read_time"; |
| 21 | constexpr char kMetricWriteTime[] = "write_time"; |
| 22 |
| 23 | perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) { |
| 24 | perf_test::PerfResultReporter reporter(kMetricPrefixJSON, story_name); |
| 25 | reporter.RegisterImportantMetric(kMetricReadTime, "ms"); |
| 26 | reporter.RegisterImportantMetric(kMetricWriteTime, "ms"); |
| 27 | return reporter; |
| 28 | } |
| 29 |
| 30 | // Generates a simple dictionary value with simple data types, a string and a |
| 31 | // list. |
| 32 | DictionaryValue GenerateDict() { |
| 33 | DictionaryValue root; |
| 34 | root.SetDoubleKey("Double", 3.141); |
| 35 | root.SetBoolKey("Bool", true); |
| 36 | root.SetIntKey("Int", 42); |
| 37 | root.SetStringKey("String", "Foo"); |
| 38 |
| 39 | ListValue list; |
| 40 | list.Append(2.718); |
| 41 | list.Append(false); |
| 42 | list.Append(123); |
| 43 | list.Append("Bar"); |
| 44 | root.SetKey("List", std::move(list)); |
| 45 |
| 46 | return root; |
| 47 | } |
| 48 |
| 49 | // Generates a tree-like dictionary value with a size of O(breadth ** depth). |
| 50 | DictionaryValue GenerateLayeredDict(int breadth, int depth) { |
| 51 | if (depth == 1) |
| 52 | return GenerateDict(); |
| 53 |
| 54 | DictionaryValue root = GenerateDict(); |
| 55 | DictionaryValue next = GenerateLayeredDict(breadth, depth - 1); |
| 56 |
| 57 | for (int i = 0; i < breadth; ++i) { |
| 58 | root.SetKey("Dict" + base::NumberToString(i), next.Clone()); |
| 59 | } |
| 60 |
| 61 | return root; |
| 62 | } |
| 63 |
| 64 | } // namespace |
| 65 |
| 66 | class JSONPerfTest : public testing::Test { |
| 67 | public: |
| 68 | void TestWriteAndRead(int breadth, int depth) { |
| 69 | std::string description = "Breadth: " + base::NumberToString(breadth) + |
| 70 | ", Depth: " + base::NumberToString(depth); |
| 71 | DictionaryValue dict = GenerateLayeredDict(breadth, depth); |
| 72 | std::string json; |
| 73 |
| 74 | TimeTicks start_write = TimeTicks::Now(); |
| 75 | JSONWriter::Write(dict, &json); |
| 76 | TimeTicks end_write = TimeTicks::Now(); |
| 77 | auto reporter = SetUpReporter("breadth_" + base::NumberToString(breadth) + |
| 78 | "_depth_" + base::NumberToString(depth)); |
| 79 | reporter.AddResult(kMetricWriteTime, end_write - start_write); |
| 80 |
| 81 | TimeTicks start_read = TimeTicks::Now(); |
| 82 | JSONReader::Read(json); |
| 83 | TimeTicks end_read = TimeTicks::Now(); |
| 84 | reporter.AddResult(kMetricReadTime, end_read - start_read); |
| 85 | } |
| 86 | }; |
| 87 |
| 88 | // Times out on Android (crbug.com/906686). |
| 89 | #if defined(OS_ANDROID) |
| 90 | #define MAYBE_StressTest DISABLED_StressTest |
| 91 | #else |
| 92 | #define MAYBE_StressTest StressTest |
| 93 | #endif |
| 94 | TEST_F(JSONPerfTest, MAYBE_StressTest) { |
| 95 | for (int i = 0; i < 4; ++i) { |
| 96 | for (int j = 0; j < 12; ++j) { |
| 97 | TestWriteAndRead(i + 1, j + 1); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 |
| 102 | } // namespace base |
| 103 |