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

Side by Side Diff: base/metrics/chromeos_metrics_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: 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 (c) 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 // Test of crash class
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/metrics/chromeos_metrics.h"
10 #include "base/metrics/crash_sample.h"
11 #include "base/metrics/histogram_sample.h"
12 #include "base/metrics/linearhistogram_sample.h"
13 #include "base/metrics/metric_sample.h"
14 #include "base/metrics/sparsehistogram_sample.h"
15 #include "base/metrics/useraction_sample.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19 namespace {
20
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->getAction(), b->getAction());
47 }
48
49 void AreEq(MetricSample* a, MetricSample* b) {
50 MetricSample::SampleType type = a->getType();
51 EXPECT_EQ(a->getType(), b->getType());
52 if (type == MetricSample::CRASH) {
53 AreEqCrash(reinterpret_cast<CrashSample*>(a),
54 reinterpret_cast<CrashSample*>(b));
55 } else if (type == MetricSample::HISTOGRAM) {
56 AreEqHistogram(reinterpret_cast<HistogramSample*>(a),
57 reinterpret_cast<HistogramSample*>(b));
58 } else if (type == MetricSample::SPARSE_HISTOGRAM) {
59 AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a),
60 reinterpret_cast<SparseHistogramSample*>(b));
61 } else if (type == MetricSample::LINEAR_HISTOGRAM) {
62 AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a),
63 reinterpret_cast<LinearHistogramSample*>(b));
64 } else if (type == MetricSample::USER_ACTION) {
65 AreEqAction(reinterpret_cast<UserActionSample*>(a),
66 reinterpret_cast<UserActionSample*>(b));
67 } else {
68 DLOG(ERROR) << "could not recognize type";
69 FAIL();
70 }
71 }
72
73 class ChromeOSMetricTest : public testing::Test {
74 protected:
75 static const int buff_size = 1024;
76 char* buffer;
77 std::string filename;
78 FilePath filepath;
79 ChromeOSMetricTest() {
80 filename = "/tmp/chromeossampletest";
81 filepath = FilePath(filename);
82 }
83 virtual void SetUp() {
84 DeleteFile(filepath, false);
85 buffer = new char[buff_size];
86 }
87
88 void TestSerialization(MetricSample* sample) {
89 int length = sample->Write(buff_size, buffer);
90 AreEq(
91 sample,
92 ChromeOSMetrics::ReadSample(length, reinterpret_cast<uint8*>(buffer)));
93 }
94 };
95
96 TEST_F(ChromeOSMetricTest, CrashSerializeTest) {
97 CrashSample crash("test");
98 TestSerialization(&crash);
99 }
100
101 TEST_F(ChromeOSMetricTest, HistogramSerializeTest) {
102 HistogramSample hist("myhist", 13, 1, 100, 10);
103 TestSerialization(&hist);
104 }
105
106 TEST_F(ChromeOSMetricTest, LinearSerializeTest) {
107 LinearHistogramSample lhist("linearhist", 12, 30);
108 TestSerialization(&lhist);
109 }
110
111 TEST_F(ChromeOSMetricTest, SparseSerializeTest) {
112 SparseHistogramSample shist("mysparse", 30);
113 TestSerialization(&shist);
114 }
115
116 TEST_F(ChromeOSMetricTest, UserActionSerializeTest) {
117 TestSerialization(new UserActionSample("myaction"));
118 }
119
120 TEST_F(ChromeOSMetricTest, WriteReadTest) {
121 HistogramSample hist("myhist", 1, 2, 3, 4);
122 CrashSample crash("crash");
123 LinearHistogramSample lhist("linear", 1, 10);
124 SparseHistogramSample shist("mysparse", 30);
125
126 ChromeOSMetrics::WriteMetricToFile(&hist, filename);
127 ChromeOSMetrics::WriteMetricToFile(&crash, filename);
128 ChromeOSMetrics::WriteMetricToFile(&lhist, filename);
129 ChromeOSMetrics::WriteMetricToFile(&shist, filename);
130 ScopedVector<MetricSample> vect;
131 ChromeOSMetrics::ReadMetricsFromFile(filename, &vect);
132 EXPECT_EQ(vect.size(), size_t(4));
133 AreEq(&hist, vect[0]);
134 AreEq(&crash, vect[1]);
135 AreEq(&lhist, vect[2]);
136 AreEq(&shist, vect[3]);
137
138 int64 size;
139 GetFileSize(filepath, &size);
140 EXPECT_EQ(0, size);
141 }
142 } // namespace
143 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698