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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: base/metrics/chromeos_metrics_unittest.cc
diff --git a/base/metrics/chromeos_metrics_unittest.cc b/base/metrics/chromeos_metrics_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e8e86a78970d65f26f2a7d425477a0446ef893d8
--- /dev/null
+++ b/base/metrics/chromeos_metrics_unittest.cc
@@ -0,0 +1,143 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Test of crash class
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/metrics/chromeos_metrics.h"
+#include "base/metrics/crash_sample.h"
+#include "base/metrics/histogram_sample.h"
+#include "base/metrics/linearhistogram_sample.h"
+#include "base/metrics/metric_sample.h"
+#include "base/metrics/sparsehistogram_sample.h"
+#include "base/metrics/useraction_sample.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace {
+
+
+void AreEqHistogram(HistogramSample* a, HistogramSample* b) {
+ EXPECT_EQ(a->name(), b->name());
+ EXPECT_EQ(a->sample(), b->sample());
+ EXPECT_EQ(a->min(), b->min());
+ EXPECT_EQ(a->max(), b->max());
+ EXPECT_EQ(a->nbucket(), b->nbucket());
+}
+
+void AreEqCrash(CrashSample* a, CrashSample* b) {
+ EXPECT_EQ(a->name(), b->name());
+}
+
+void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) {
+ EXPECT_EQ(a->name(), b->name());
+ EXPECT_EQ(a->sample(), b->sample());
+ EXPECT_EQ(a->max(), b->max());
+}
+
+void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) {
+ EXPECT_EQ(a->name(), b->name());
+ EXPECT_EQ(a->sample(), b->sample());
+}
+
+void AreEqAction(UserActionSample* a, UserActionSample* b) {
+ EXPECT_EQ(a->getAction(), b->getAction());
+}
+
+void AreEq(MetricSample* a, MetricSample* b) {
+ MetricSample::SampleType type = a->getType();
+ EXPECT_EQ(a->getType(), b->getType());
+ if (type == MetricSample::CRASH) {
+ AreEqCrash(reinterpret_cast<CrashSample*>(a),
+ reinterpret_cast<CrashSample*>(b));
+ } else if (type == MetricSample::HISTOGRAM) {
+ AreEqHistogram(reinterpret_cast<HistogramSample*>(a),
+ reinterpret_cast<HistogramSample*>(b));
+ } else if (type == MetricSample::SPARSE_HISTOGRAM) {
+ AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a),
+ reinterpret_cast<SparseHistogramSample*>(b));
+ } else if (type == MetricSample::LINEAR_HISTOGRAM) {
+ AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a),
+ reinterpret_cast<LinearHistogramSample*>(b));
+ } else if (type == MetricSample::USER_ACTION) {
+ AreEqAction(reinterpret_cast<UserActionSample*>(a),
+ reinterpret_cast<UserActionSample*>(b));
+ } else {
+ DLOG(ERROR) << "could not recognize type";
+ FAIL();
+ }
+}
+
+class ChromeOSMetricTest : public testing::Test {
+ protected:
+ static const int buff_size = 1024;
+ char* buffer;
+ std::string filename;
+ FilePath filepath;
+ ChromeOSMetricTest() {
+ filename = "/tmp/chromeossampletest";
+ filepath = FilePath(filename);
+ }
+ virtual void SetUp() {
+ DeleteFile(filepath, false);
+ buffer = new char[buff_size];
+ }
+
+ void TestSerialization(MetricSample* sample) {
+ int length = sample->Write(buff_size, buffer);
+ AreEq(
+ sample,
+ ChromeOSMetrics::ReadSample(length, reinterpret_cast<uint8*>(buffer)));
+ }
+};
+
+TEST_F(ChromeOSMetricTest, CrashSerializeTest) {
+ CrashSample crash("test");
+ TestSerialization(&crash);
+}
+
+TEST_F(ChromeOSMetricTest, HistogramSerializeTest) {
+ HistogramSample hist("myhist", 13, 1, 100, 10);
+ TestSerialization(&hist);
+}
+
+TEST_F(ChromeOSMetricTest, LinearSerializeTest) {
+ LinearHistogramSample lhist("linearhist", 12, 30);
+ TestSerialization(&lhist);
+}
+
+TEST_F(ChromeOSMetricTest, SparseSerializeTest) {
+ SparseHistogramSample shist("mysparse", 30);
+ TestSerialization(&shist);
+}
+
+TEST_F(ChromeOSMetricTest, UserActionSerializeTest) {
+ TestSerialization(new UserActionSample("myaction"));
+}
+
+TEST_F(ChromeOSMetricTest, WriteReadTest) {
+ HistogramSample hist("myhist", 1, 2, 3, 4);
+ CrashSample crash("crash");
+ LinearHistogramSample lhist("linear", 1, 10);
+ SparseHistogramSample shist("mysparse", 30);
+
+ ChromeOSMetrics::WriteMetricToFile(&hist, filename);
+ ChromeOSMetrics::WriteMetricToFile(&crash, filename);
+ ChromeOSMetrics::WriteMetricToFile(&lhist, filename);
+ ChromeOSMetrics::WriteMetricToFile(&shist, filename);
+ ScopedVector<MetricSample> vect;
+ ChromeOSMetrics::ReadMetricsFromFile(filename, &vect);
+ EXPECT_EQ(vect.size(), size_t(4));
+ AreEq(&hist, vect[0]);
+ AreEq(&crash, vect[1]);
+ AreEq(&lhist, vect[2]);
+ AreEq(&shist, vect[3]);
+
+ int64 size;
+ GetFileSize(filepath, &size);
+ EXPECT_EQ(0, size);
+}
+} // namespace
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698