Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
Alexei Svitkine (slow)
2014/04/17 16:33:21
Nit: Remove (c) from copyright strings for new fil
| |
| 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 | |
|
Alexei Svitkine (slow)
2014/04/17 16:33:21
Remove this comment. Make the metrics_utils_chrome
| |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/metrics/crash_sample_chromeos.h" | |
| 10 #include "base/metrics/histogram_sample_chromeos.h" | |
| 11 #include "base/metrics/linearhistogram_sample_chromeos.h" | |
| 12 #include "base/metrics/metric_sample_chromeos.h" | |
| 13 #include "base/metrics/metrics_utils_chromeos.h" | |
| 14 #include "base/metrics/sparsehistogram_sample_chromeos.h" | |
| 15 #include "base/metrics/useraction_sample_chromeos.h" | |
| 16 #include "base/strings/stringprintf.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace base { | |
| 20 namespace { | |
| 21 using MetricsUtils::ReadSample; | |
| 22 | |
| 23 void AreEqHistogram(HistogramSample* a, HistogramSample* b) { | |
| 24 ASSERT_EQ(a->name(), b->name()); | |
| 25 ASSERT_EQ(a->sample(), b->sample()); | |
| 26 ASSERT_EQ(a->min(), b->min()); | |
| 27 ASSERT_EQ(a->max(), b->max()); | |
| 28 ASSERT_EQ(a->nbucket(), b->nbucket()); | |
| 29 } | |
| 30 | |
| 31 void AreEqCrash(CrashSample* a, CrashSample* b) { | |
| 32 ASSERT_EQ(a->name(), b->name()); | |
| 33 } | |
| 34 | |
| 35 void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) { | |
| 36 ASSERT_EQ(a->name(), b->name()); | |
| 37 ASSERT_EQ(a->sample(), b->sample()); | |
| 38 ASSERT_EQ(a->max(), b->max()); | |
| 39 } | |
| 40 | |
| 41 void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) { | |
| 42 ASSERT_EQ(a->name(), b->name()); | |
| 43 ASSERT_EQ(a->sample(), b->sample()); | |
| 44 } | |
| 45 | |
| 46 void AreEqAction(UserActionSample* a, UserActionSample* b) { | |
| 47 ASSERT_EQ(a->name(), b->name()); | |
| 48 } | |
| 49 | |
| 50 void AreEq(MetricSample* a, MetricSample* b) { | |
| 51 ASSERT_TRUE(a); | |
| 52 ASSERT_TRUE(b); | |
| 53 MetricSample::SampleType type = a->type(); | |
| 54 ASSERT_EQ(a->type(), b->type()); | |
| 55 if (type == MetricSample::CRASH) { | |
| 56 AreEqCrash(reinterpret_cast<CrashSample*>(a), | |
| 57 reinterpret_cast<CrashSample*>(b)); | |
| 58 } else if (type == MetricSample::HISTOGRAM) { | |
| 59 AreEqHistogram(reinterpret_cast<HistogramSample*>(a), | |
| 60 reinterpret_cast<HistogramSample*>(b)); | |
| 61 } else if (type == MetricSample::SPARSE_HISTOGRAM) { | |
| 62 AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a), | |
| 63 reinterpret_cast<SparseHistogramSample*>(b)); | |
| 64 } else if (type == MetricSample::LINEAR_HISTOGRAM) { | |
| 65 AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a), | |
| 66 reinterpret_cast<LinearHistogramSample*>(b)); | |
| 67 } else if (type == MetricSample::USER_ACTION) { | |
| 68 AreEqAction(reinterpret_cast<UserActionSample*>(a), | |
| 69 reinterpret_cast<UserActionSample*>(b)); | |
| 70 } else { | |
| 71 DLOG(ERROR) << "could not recognize type"; | |
| 72 FAIL(); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 class MetricsUtilsChromeOSTest : public testing::Test { | |
| 77 protected: | |
| 78 static const int buff_size = 1024; | |
| 79 char* buffer; | |
| 80 std::string filename; | |
| 81 FilePath filepath; | |
| 82 MetricsUtilsChromeOSTest() { | |
| 83 filename = "/tmp/chromeossampletest"; | |
| 84 filepath = FilePath(filename); | |
| 85 } | |
| 86 virtual void SetUp() { | |
| 87 DeleteFile(filepath, false); | |
| 88 buffer = new char[buff_size]; | |
| 89 } | |
| 90 | |
| 91 void TestSerialization(MetricSample* sample) { | |
| 92 string serialized(sample->ToString()); | |
| 93 ASSERT_EQ('\0', serialized[serialized.length() - 1]); | |
| 94 AreEq(sample, MetricsUtils::ReadSample(serialized)); | |
| 95 } | |
| 96 }; | |
| 97 | |
| 98 TEST_F(MetricsUtilsChromeOSTest, CrashSerializeTest) { | |
| 99 CrashSample crash("test"); | |
| 100 TestSerialization(&crash); | |
| 101 } | |
| 102 | |
| 103 TEST_F(MetricsUtilsChromeOSTest, HistogramSerializeTest) { | |
| 104 HistogramSample hist("myhist", 13, 1, 100, 10); | |
| 105 TestSerialization(&hist); | |
| 106 } | |
| 107 | |
| 108 TEST_F(MetricsUtilsChromeOSTest, LinearSerializeTest) { | |
| 109 LinearHistogramSample lhist("linearhist", 12, 30); | |
| 110 TestSerialization(&lhist); | |
| 111 } | |
| 112 | |
| 113 TEST_F(MetricsUtilsChromeOSTest, SparseSerializeTest) { | |
| 114 SparseHistogramSample shist("mysparse", 30); | |
| 115 TestSerialization(&shist); | |
| 116 } | |
| 117 | |
| 118 TEST_F(MetricsUtilsChromeOSTest, UserActionSerializeTest) { | |
| 119 TestSerialization(new UserActionSample("myaction")); | |
| 120 } | |
| 121 | |
| 122 TEST_F(MetricsUtilsChromeOSTest, IllegalNameAreFilteredTest) { | |
| 123 SparseHistogramSample sample1("no space", 10); | |
| 124 LinearHistogramSample sample2(StringPrintf("here%cbhe", '\0'), 1, 3); | |
| 125 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1, filename)); | |
| 126 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2, filename)); | |
| 127 int64 size = GetFileSize(filepath, &size); | |
| 128 | |
| 129 EXPECT_EQ(0, size); | |
| 130 } | |
| 131 | |
| 132 TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) { | |
| 133 string input(StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); | |
| 134 EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input)); | |
| 135 } | |
| 136 | |
| 137 TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) { | |
| 138 CrashSample crash("crash"); | |
| 139 | |
| 140 MetricsUtils::WriteMetricToFile(crash, filename); | |
| 141 int64 size; | |
| 142 GetFileSize(filepath, &size); | |
| 143 ASSERT_EQ(size, 16); | |
| 144 } | |
| 145 | |
| 146 TEST_F(MetricsUtilsChromeOSTest, WriteReadTest) { | |
| 147 HistogramSample hist("myhist", 1, 2, 3, 4); | |
| 148 CrashSample crash("crash"); | |
| 149 LinearHistogramSample lhist("linear", 1, 10); | |
| 150 SparseHistogramSample shist("mysparse", 30); | |
| 151 UserActionSample action("myaction"); | |
| 152 | |
| 153 MetricsUtils::WriteMetricToFile(hist, filename); | |
| 154 MetricsUtils::WriteMetricToFile(crash, filename); | |
| 155 MetricsUtils::WriteMetricToFile(lhist, filename); | |
| 156 MetricsUtils::WriteMetricToFile(shist, filename); | |
| 157 MetricsUtils::WriteMetricToFile(action, filename); | |
| 158 ScopedVector<MetricSample> vect; | |
| 159 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect); | |
| 160 ASSERT_EQ(vect.size(), size_t(5)); | |
| 161 AreEq(&hist, vect[0]); | |
| 162 AreEq(&crash, vect[1]); | |
| 163 AreEq(&lhist, vect[2]); | |
| 164 AreEq(&shist, vect[3]); | |
| 165 AreEq(&action, vect[4]); | |
| 166 | |
| 167 int64 size; | |
| 168 GetFileSize(filepath, &size); | |
| 169 ASSERT_EQ(0, size); | |
| 170 } | |
| 171 } // namespace | |
| 172 } // namespace base | |
| OLD | NEW |