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

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: Convert to use scoped_ptr. Remove using declarations. Fixed linting. 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/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 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 static const int buff_size = 1024;
78
79 MetricsUtilsChromeOSTest() {
80 bool success = temporary_dir.CreateUniqueTempDir();
81 if (success) {
82 base::FilePath dir_path = temporary_dir.path();
83 filename = dir_path.value() + "chromeossampletest";
84 filepath = base::FilePath(filename);
85 }
86 }
Alexei Svitkine (slow) 2014/05/01 15:57:09 Add empty line after this.
87 virtual void SetUp() {
88 base::DeleteFile(filepath, false);
Alexei Svitkine (slow) 2014/05/01 15:57:09 What's this call for?
bsimonnet 2014/05/01 20:34:21 To make sure the file is not here and therefore no
89 buffer.reset(new char[buff_size]);
90 }
91
92 void TestSerialization(MetricSample* sample) {
93 std::string serialized(sample->ToString());
94 ASSERT_EQ('\0', serialized[serialized.length() - 1]);
95 AreEq(sample, MetricsUtils::ReadSample(serialized).get());
96 }
97
98 scoped_ptr<char[]> buffer;
Alexei Svitkine (slow) 2014/05/01 15:57:09 Is this used somewhere?
bsimonnet 2014/05/01 20:34:21 no I forgot to delete it.
99 std::string filename;
100 base::ScopedTempDir temporary_dir;
101 base::FilePath filepath;
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(base::StringPrintf("here%cbhe", '\0'), 1, 3);
131 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1, filename));
132 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2, filename));
133 int64 size = 0;
134
135 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size));
136
137 EXPECT_EQ(0, size);
138 }
139
140 TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) {
141 std::string input(
142 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0'));
143 EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input).get());
144 }
145
146 TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) {
147 CrashSample crash("crash");
148
149 MetricsUtils::WriteMetricToFile(crash, filename);
150 int64 size = 0;
151 ASSERT_TRUE(base::GetFileSize(filepath, &size));
152 EXPECT_EQ(size, 16);
153 }
154
155 TEST_F(MetricsUtilsChromeOSTest, MessagesTooLongAreDiscardedTest) {
156 // Creates a message that is bigger than the maximum allowed size.
157 // As we are adding extra character (crash, \0s, etc), if the name is
158 // kMessageMaxLength long, it will be too long.
159 std::string name(MetricsUtils::kMessageMaxLength, 'c');
160
161 CrashSample crash(name);
162 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(crash, filename));
163 int64 size = 0;
164 ASSERT_TRUE(base::GetFileSize(filepath, &size));
165 EXPECT_EQ(0, size);
166 }
167
168 TEST_F(MetricsUtilsChromeOSTest, ReadLongMessageTest) {
169 base::File test_file(filepath,
170 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND);
171 std::string message(MetricsUtils::kMessageMaxLength + 1, 'c');
172
173 int32_t message_size = message.length() + sizeof(int32_t);
174 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size),
175 sizeof(message_size));
176 test_file.WriteAtCurrentPos(message.c_str(), message.length());
177 test_file.Close();
178
179 CrashSample crash("test");
180 MetricsUtils::WriteMetricToFile(crash, filename);
181
182 ScopedVector<MetricSample> samples;
183 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &samples);
184 ASSERT_EQ(size_t(1), samples.size());
185 AreEq(&crash, samples[0]);
186 }
187
188 TEST_F(MetricsUtilsChromeOSTest, WriteReadTest) {
189 HistogramSample hist("myhist", 1, 2, 3, 4);
190 CrashSample crash("crash");
191 LinearHistogramSample lhist("linear", 1, 10);
192 SparseHistogramSample shist("mysparse", 30);
193 UserActionSample action("myaction");
194
195 MetricsUtils::WriteMetricToFile(hist, filename);
196 MetricsUtils::WriteMetricToFile(crash, filename);
197 MetricsUtils::WriteMetricToFile(lhist, filename);
198 MetricsUtils::WriteMetricToFile(shist, filename);
199 MetricsUtils::WriteMetricToFile(action, filename);
200 ScopedVector<MetricSample> vect;
201 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect);
202 ASSERT_EQ(vect.size(), size_t(5));
203 AreEq(&hist, vect[0]);
204 AreEq(&crash, vect[1]);
205 AreEq(&lhist, vect[2]);
206 AreEq(&shist, vect[3]);
207 AreEq(&action, vect[4]);
208
209 int64 size = 0;
210 ASSERT_TRUE(base::GetFileSize(filepath, &size));
211 ASSERT_EQ(0, size);
212 }
213
214 } // namespace
215 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698