Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: base/value_perftest.cc

Issue 2745733005: Base Perftest
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <numeric>
7 #include <random>
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_piece.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/perf/perf_test.h"
16
17 namespace base {
18
19 class ValuePerfTest : public testing::Test {
20 public:
21 enum class TestOrder { ASCENDING, DESCENDING, INTERLEAVED, RANDOM };
22
23 std::string to_string(TestOrder order) {
24 switch (order) {
25 case TestOrder::ASCENDING:
26 return "ascending";
27 case TestOrder::DESCENDING:
28 return "descending";
29 case TestOrder::INTERLEAVED:
30 return "interleaved";
31 case TestOrder::RANDOM:
32 return "random";
33 default:
34 return "";
35 }
36 }
37
38 std::vector<int> GetLengths(int max_length, TestOrder order) {
39 std::vector<int> lengths(max_length + 1);
40 std::iota(std::begin(lengths), std::end(lengths), 0);
41
42 switch (order) {
43 case TestOrder::ASCENDING:
44 break;
45 case TestOrder::DESCENDING:
46 std::reverse(std::begin(lengths), std::end(lengths));
47 break;
48 case TestOrder::INTERLEAVED:
49 for (int i = 0; i < max_length / 2; i += 2)
50 std::swap(lengths[i], lengths[max_length - i]);
51 break;
52 case TestOrder::RANDOM: {
53 std::random_device rd;
54 std::mt19937 g(rd());
55 std::shuffle(std::begin(lengths), std::end(lengths), g);
56 break;
57 }
58 }
59
60 return lengths;
61 }
62
63 void TestStringMoveAssign(int max_length, TestOrder order) {
64 std::string description = "Max Length: " + std::to_string(max_length) +
65 ", TestOrder: " + to_string(order);
66
67 double duration = 0.0;
68 for (auto length : GetLengths(max_length, order)) {
69 auto val = Value(std::string(length, 'a'));
70 TimeTicks start = TimeTicks::Now();
71 val_ = std::move(val);
72 TimeTicks end = TimeTicks::Now();
73 duration += (end - start).InMicroseconds();
74 }
75
76 perf_test::PrintResult("StringMoveAssign", "", description, duration, "us",
77 true);
78 }
79
80 void TestListMoveAssign(int max_length, TestOrder order) {
81 std::string description = "Max Length: " + std::to_string(max_length) +
82 ", TestOrder: " + to_string(order);
83
84 double duration = 0.0;
85 for (auto length : GetLengths(max_length, order)) {
86 auto val = ListValue();
87 for (int i = 0; i < length; ++i)
88 val.AppendInteger(i);
89 TimeTicks start = TimeTicks::Now();
90 val_ = std::move(val);
91 TimeTicks end = TimeTicks::Now();
92 duration += (end - start).InMicroseconds();
93 }
94
95 perf_test::PrintResult("ListMoveAssign", "", description, duration, "us",
96 true);
97 }
98
99 void TestDictMoveAssign(int max_length, TestOrder order) {
100 std::string description = "Max Length: " + std::to_string(max_length) +
101 ", TestOrder: " + to_string(order);
102
103 double duration = 0.0;
104 for (auto length : GetLengths(max_length, order)) {
105 auto val = DictionaryValue();
106 for (int i = 0; i < length; ++i)
107 val.Set(std::to_string(i), MakeUnique<Value>(i));
108 TimeTicks start = TimeTicks::Now();
109 val_ = std::move(val);
110 TimeTicks end = TimeTicks::Now();
111 duration += (end - start).InMicroseconds();
112 }
113
114 perf_test::PrintResult("DictMoveAssign", "", description, duration, "us",
115 true);
116 }
117
118 void TestSortStringValues(int max_length, TestOrder order) {
119 std::string description = "Max Length: " + std::to_string(max_length) +
120 ", TestOrder: " + to_string(order);
121
122 std::vector<base::Value> values;
123 values.reserve(max_length);
124 for (auto length : GetLengths(max_length, order))
125 values.emplace_back(std::string(length, 'a'));
126
127 TimeTicks start = TimeTicks::Now();
128 std::sort(std::begin(values), std::end(values),
129 [](const base::Value& lhs, const base::Value& rhs) {
130 return lhs.GetString() < rhs.GetString();
131 });
132 TimeTicks end = TimeTicks::Now();
133 double duration = (end - start).InMicroseconds();
134
135 perf_test::PrintResult("SortStringValues", "", description, duration, "us",
136 true);
137 }
138
139 private:
140 Value val_;
141 };
142
143 TEST_F(ValuePerfTest, StringAssignTest) {
144 for (int l = 0; l < 19; ++l) {
145 for (int o = 0; o < 1; ++o) {
146 TestStringMoveAssign(1 << l, static_cast<ValuePerfTest::TestOrder>(o));
147 }
148 }
149 }
150
151 TEST_F(ValuePerfTest, ListAssignTest) {
152 for (int l = 0; l < 15; ++l) {
153 for (int o = 0; o < 1; ++o) {
154 TestListMoveAssign(1 << l, static_cast<ValuePerfTest::TestOrder>(o));
155 }
156 }
157 }
158
159 TEST_F(ValuePerfTest, DictAssignTest) {
160 for (int l = 0; l < 14; ++l) {
161 for (int o = 0; o < 1; ++o) {
162 TestDictMoveAssign(1 << l, static_cast<ValuePerfTest::TestOrder>(o));
163 }
164 }
165 }
166
167 TEST_F(ValuePerfTest, SortTest) {
168 for (int l = 0; l < 19; ++l) {
169 for (int o = 0; o < 4; ++o) {
170 TestSortStringValues(1 << l, static_cast<ValuePerfTest::TestOrder>(o));
171 }
172 }
173 }
174
175 } // namespace base
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698