summaryrefslogtreecommitdiffstats
path: root/chromium/base/json/json_perftest.cc
blob: 1e0ea33d31071bd3ea571fd5cd5e8efa89f754fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_test.h"

namespace base {

namespace {
// Generates a simple dictionary value with simple data types, a string and a
// list.
DictionaryValue GenerateDict() {
  DictionaryValue root;
  root.SetDoubleKey("Double", 3.141);
  root.SetBoolKey("Bool", true);
  root.SetIntKey("Int", 42);
  root.SetStringKey("String", "Foo");

  ListValue list;
  list.GetList().emplace_back(2.718);
  list.GetList().emplace_back(false);
  list.GetList().emplace_back(123);
  list.GetList().emplace_back("Bar");
  root.SetKey("List", std::move(list));

  return root;
}

// Generates a tree-like dictionary value with a size of O(breadth ** depth).
DictionaryValue GenerateLayeredDict(int breadth, int depth) {
  if (depth == 1)
    return GenerateDict();

  DictionaryValue root = GenerateDict();
  DictionaryValue next = GenerateLayeredDict(breadth, depth - 1);

  for (int i = 0; i < breadth; ++i) {
    root.SetKey("Dict" + base::NumberToString(i), next.Clone());
  }

  return root;
}

}  // namespace

class JSONPerfTest : public testing::Test {
 public:
  void TestWriteAndRead(int breadth, int depth) {
    std::string description = "Breadth: " + base::NumberToString(breadth) +
                              ", Depth: " + base::NumberToString(depth);
    DictionaryValue dict = GenerateLayeredDict(breadth, depth);
    std::string json;

    TimeTicks start_write = TimeTicks::Now();
    JSONWriter::Write(dict, &json);
    TimeTicks end_write = TimeTicks::Now();
    perf_test::PrintResult("Write", "", description,
                           (end_write - start_write).InMillisecondsF(), "ms",
                           true);

    TimeTicks start_read = TimeTicks::Now();
    JSONReader::Read(json);
    TimeTicks end_read = TimeTicks::Now();
    perf_test::PrintResult("Read", "", description,
                           (end_read - start_read).InMillisecondsF(), "ms",
                           true);
  }
};

// Times out on Android (crbug.com/906686).
#if defined(OS_ANDROID)
#define MAYBE_StressTest DISABLED_StressTest
#else
#define MAYBE_StressTest StressTest
#endif
TEST_F(JSONPerfTest, MAYBE_StressTest) {
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 12; ++j) {
      TestWriteAndRead(i + 1, j + 1);
    }
  }
}

}  // namespace base