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

Side by Side Diff: components/metrics/export/metrics_utils_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698