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

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

Powered by Google App Engine
This is Rietveld 408576698