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

Side by Side Diff: components/metrics/metrics_utils_unittest_chromeos.cc

Issue 227873002: Create a histogram serialization mechanism in components/metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving to components instead of base. Adding sanity checks. Fixing lint Created 6 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2014 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 "components/metrics/metrics_utils_chromeos.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/metrics/crash_sample_chromeos.h"
11 #include "components/metrics/histogram_sample_chromeos.h"
12 #include "components/metrics/linearhistogram_sample_chromeos.h"
13 #include "components/metrics/metric_sample_chromeos.h"
14 #include "components/metrics/sparsehistogram_sample_chromeos.h"
15 #include "components/metrics/useraction_sample_chromeos.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace metrics {
19 namespace {
20
21 using base::DeleteFile;
22 using base::File;
23 using base::FilePath;
24 using base::GetFileSize;
25 using base::StringPrintf;
26 using MetricsUtils::ReadSample;
27 using std::string;
28
29 void AreEqHistogram(HistogramSample* a, HistogramSample* b) {
30 ASSERT_EQ(a->name(), b->name());
Ben Chan 2014/04/18 20:27:08 ASSERT_ stops the test immediately, do you want EX
bsimonnet 2014/04/18 23:53:49 yes ! :)
31 ASSERT_EQ(a->sample(), b->sample());
32 ASSERT_EQ(a->min(), b->min());
33 ASSERT_EQ(a->max(), b->max());
34 ASSERT_EQ(a->nbucket(), b->nbucket());
35 }
36
37 void AreEqCrash(CrashSample* a, CrashSample* b) {
38 ASSERT_EQ(a->name(), b->name());
39 }
40
41 void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) {
42 ASSERT_EQ(a->name(), b->name());
43 ASSERT_EQ(a->sample(), b->sample());
44 ASSERT_EQ(a->max(), b->max());
45 }
46
47 void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) {
48 ASSERT_EQ(a->name(), b->name());
49 ASSERT_EQ(a->sample(), b->sample());
50 }
51
52 void AreEqAction(UserActionSample* a, UserActionSample* b) {
53 ASSERT_EQ(a->name(), b->name());
54 }
55
56 void AreEq(MetricSample* a, MetricSample* b) {
57 ASSERT_TRUE(a);
58 ASSERT_TRUE(b);
59 MetricSample::SampleType type = a->type();
60 ASSERT_EQ(a->type(), b->type());
61 if (type == MetricSample::CRASH) {
62 AreEqCrash(reinterpret_cast<CrashSample*>(a),
63 reinterpret_cast<CrashSample*>(b));
64 } else if (type == MetricSample::HISTOGRAM) {
65 AreEqHistogram(reinterpret_cast<HistogramSample*>(a),
66 reinterpret_cast<HistogramSample*>(b));
67 } else if (type == MetricSample::SPARSE_HISTOGRAM) {
68 AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a),
69 reinterpret_cast<SparseHistogramSample*>(b));
70 } else if (type == MetricSample::LINEAR_HISTOGRAM) {
71 AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a),
72 reinterpret_cast<LinearHistogramSample*>(b));
73 } else if (type == MetricSample::USER_ACTION) {
74 AreEqAction(reinterpret_cast<UserActionSample*>(a),
75 reinterpret_cast<UserActionSample*>(b));
76 } else {
77 DLOG(ERROR) << "could not recognize type";
78 FAIL();
79 }
80 }
81
82 class MetricsUtilsChromeOSTest : public testing::Test {
83 protected:
84 static const int buff_size = 1024;
Ben Chan 2014/04/18 20:27:08 http://google-styleguide.googlecode.com/svn/trunk/
85 char* buffer;
Ben Chan 2014/04/18 20:27:08 scoped_ptr<char[]> buffer;
86 std::string filename;
Ben Chan 2014/04/18 20:27:08 you can drop std:: as you already have 'using std:
87 FilePath filepath;
88 MetricsUtilsChromeOSTest() {
89 filename = "/tmp/chromeossampletest";
Ben Chan 2014/04/18 20:27:08 avoid hardcoding a temp file for test (and without
90 filepath = FilePath(filename);
91 }
92 virtual void SetUp() {
93 DeleteFile(filepath, false);
94 buffer = new char[buff_size];
95 }
96
97 void TestSerialization(MetricSample* sample) {
98 std::string serialized(sample->ToString());
Ben Chan 2014/04/18 20:27:08 you can drop std:: as you already have 'using std:
99 ASSERT_EQ('\0', serialized[serialized.length() - 1]);
100 AreEq(sample, MetricsUtils::ReadSample(serialized));
101 }
102 };
103
104 TEST_F(MetricsUtilsChromeOSTest, CrashSerializeTest) {
105 CrashSample crash("test");
106 TestSerialization(&crash);
107 }
108
109 TEST_F(MetricsUtilsChromeOSTest, HistogramSerializeTest) {
110 HistogramSample hist("myhist", 13, 1, 100, 10);
111 TestSerialization(&hist);
112 }
113
114 TEST_F(MetricsUtilsChromeOSTest, LinearSerializeTest) {
115 LinearHistogramSample lhist("linearhist", 12, 30);
116 TestSerialization(&lhist);
117 }
118
119 TEST_F(MetricsUtilsChromeOSTest, SparseSerializeTest) {
120 SparseHistogramSample shist("mysparse", 30);
121 TestSerialization(&shist);
122 }
123
124 TEST_F(MetricsUtilsChromeOSTest, UserActionSerializeTest) {
125 TestSerialization(new UserActionSample("myaction"));
126 }
127
128 TEST_F(MetricsUtilsChromeOSTest, IllegalNameAreFilteredTest) {
129 SparseHistogramSample sample1("no space", 10);
130 LinearHistogramSample sample2(StringPrintf("here%cbhe", '\0'), 1, 3);
131 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1, filename));
132 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2, filename));
133 int64 size = GetFileSize(filepath, &size);
134
135 EXPECT_EQ(0, size);
136 }
137
138 TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) {
139 std::string input(StringPrintf("sparsehistogram%cname foo%c", '\0', '\0'));
Ben Chan 2014/04/18 20:27:08 you can drop std:: as you already have 'using std:
140 EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input));
141 }
142
143 TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) {
144 CrashSample crash("crash");
145
146 MetricsUtils::WriteMetricToFile(crash, filename);
147 int64 size;
Ben Chan 2014/04/18 20:27:08 if the compiler yells, you may need to initialize
148 GetFileSize(filepath, &size);
Ben Chan 2014/04/18 20:27:08 ASSERT_TRUE(GetFileSize(filepath, &size));
149 ASSERT_EQ(size, 16);
150 }
151
152 TEST_F(MetricsUtilsChromeOSTest, MessagesTooLongAreDiscardedTest) {
153 // Creates a message that is bigger than the maximum allowed size.
154 // As we are adding extra character (crash, \0s, etc), if the name is
155 // kMessageMaxLength long, it will be too long.
156 char c = 'c';
157 string name = "";
158 for (int i = 0; i < MetricsUtils::kMessageMaxLength; i++) {
159 name = name + c;
160 }
Ben Chan 2014/04/18 20:27:08 you can simply do: string name(MetricsUtils::kM
161
162 CrashSample crash(name);
163 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(crash, filename));
164 int64 size;
165 GetFileSize(filepath, &size);
Ben Chan 2014/04/18 20:27:08 ditto
166 EXPECT_EQ(0, size);
167 }
168
169 TEST_F(MetricsUtilsChromeOSTest, ReadLongMessageTest) {
170 File f(filepath, File::FLAG_OPEN_ALWAYS | File::FLAG_APPEND);
Ben Chan 2014/04/18 20:27:08 nit: |f| feels like a bad variable
171 char c = 'c';
172 string message = "";
173 for (int i = 0; i < MetricsUtils::kMessageMaxLength + 1; i++) {
174 message = message + c;
Ben Chan 2014/04/18 20:27:08 ditto
175 }
176
177 int32_t message_size = message.length() + sizeof(int32_t);
178 f.WriteAtCurrentPos(reinterpret_cast<char*>(&message_size),
Ben Chan 2014/04/18 20:27:08 const char*
179 sizeof(message_size));
180 f.WriteAtCurrentPos(message.c_str(), message.length());
181 f.Close();
182
183 CrashSample crash("test");
184 MetricsUtils::WriteMetricToFile(crash, filename);
185
186 ScopedVector<MetricSample> samples;
187 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &samples);
188 ASSERT_EQ(size_t(1), samples.size());
189 AreEq(&crash, samples[0]);
190 }
191
192 TEST_F(MetricsUtilsChromeOSTest, WriteReadTest) {
193 HistogramSample hist("myhist", 1, 2, 3, 4);
194 CrashSample crash("crash");
195 LinearHistogramSample lhist("linear", 1, 10);
196 SparseHistogramSample shist("mysparse", 30);
197 UserActionSample action("myaction");
198
199 MetricsUtils::WriteMetricToFile(hist, filename);
200 MetricsUtils::WriteMetricToFile(crash, filename);
201 MetricsUtils::WriteMetricToFile(lhist, filename);
202 MetricsUtils::WriteMetricToFile(shist, filename);
203 MetricsUtils::WriteMetricToFile(action, filename);
204 ScopedVector<MetricSample> vect;
205 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect);
206 ASSERT_EQ(vect.size(), size_t(5));
207 AreEq(&hist, vect[0]);
208 AreEq(&crash, vect[1]);
209 AreEq(&lhist, vect[2]);
210 AreEq(&shist, vect[3]);
211 AreEq(&action, vect[4]);
212
213 int64 size;
214 GetFileSize(filepath, &size);
Ben Chan 2014/04/18 20:27:08 ditto
215 ASSERT_EQ(0, size);
216 }
217
218 } // namespace
219 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698