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

Side by Side Diff: metrics_daemon_test.cc

Issue 6486021: Collect some disk statistics. (Closed) Base URL: http://git.chromium.org/git/metrics.git@master
Patch Set: Change metrix prefix Filesystem back to Platform Created 9 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « metrics_daemon_main.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <utime.h> 5 #include <utime.h>
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include <base/file_util.h> 10 #include <base/file_util.h>
11 #include <base/stringprintf.h>
11 #include <gtest/gtest.h> 12 #include <gtest/gtest.h>
12 13
13 #include "counter_mock.h" 14 #include "counter_mock.h"
14 #include "metrics_daemon.h" 15 #include "metrics_daemon.h"
15 #include "metrics_library_mock.h" 16 #include "metrics_library_mock.h"
16 17
17 using base::Time; 18 using base::Time;
18 using base::TimeTicks; 19 using base::TimeTicks;
19 using chromeos_metrics::FrequencyCounter; 20 using chromeos_metrics::FrequencyCounter;
20 using chromeos_metrics::FrequencyCounterMock; 21 using chromeos_metrics::FrequencyCounterMock;
21 using chromeos_metrics::TaggedCounterMock; 22 using chromeos_metrics::TaggedCounterMock;
22 using chromeos_metrics::TaggedCounterReporter; 23 using chromeos_metrics::TaggedCounterReporter;
23 using chromeos_metrics::TaggedCounterReporterMock; 24 using chromeos_metrics::TaggedCounterReporterMock;
24 using std::string; 25 using std::string;
25 using std::vector; 26 using std::vector;
26 using ::testing::_; 27 using ::testing::_;
27 using ::testing::Return; 28 using ::testing::Return;
28 using ::testing::StrictMock; 29 using ::testing::StrictMock;
29 30
30 static const int kSecondsPerDay = 24 * 60 * 60; 31 static const int kSecondsPerDay = 24 * 60 * 60;
31 32
32 static const char kTestDir[] = "test"; 33 static const char kTestDir[] = "test";
33 static const char kLastFile[] = "test/last"; 34 static const char kLastFile[] = "test/last";
34 static const char kCurrentFile[] = "test/current"; 35 static const char kCurrentFile[] = "test/current";
36 static const char kFakeDiskStatsPath[] = "fake-disk-stats";
37 static const char kFakeDiskStatsFormat[] =
38 " 1793 1788 %d 105580 "
39 " 196 175 %d 30290 "
40 " 0 44060 135850\n";
41 static string kFakeDiskStats[2];
42 static const int kFakeReadSectors[] = {80000, 100000};
43 static const int kFakeWriteSectors[] = {3000, 4000};
35 44
36 // This class allows a TimeTicks object to be initialized with seconds 45 // This class allows a TimeTicks object to be initialized with seconds
37 // (rather than microseconds) through the protected TimeTicks(int64) 46 // (rather than microseconds) through the protected TimeTicks(int64)
38 // constructor. 47 // constructor.
39 class TestTicks : public TimeTicks { 48 class TestTicks : public TimeTicks {
40 public: 49 public:
41 TestTicks(int64 seconds) 50 TestTicks(int64 seconds)
42 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} 51 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {}
43 }; 52 };
44 53
45 // Overloaded for test failure printing purposes. 54 // Overloaded for test failure printing purposes.
46 static std::ostream& operator<<(std::ostream& o, const Time& time) { 55 static std::ostream& operator<<(std::ostream& o, const Time& time) {
47 o << time.ToInternalValue() << "us"; 56 o << time.ToInternalValue() << "us";
48 return o; 57 return o;
49 }; 58 };
50 59
51 class MetricsDaemonTest : public testing::Test { 60 class MetricsDaemonTest : public testing::Test {
52 protected: 61 protected:
53 virtual void SetUp() { 62 virtual void SetUp() {
54 EXPECT_EQ(NULL, daemon_.daily_use_.get()); 63 EXPECT_EQ(NULL, daemon_.daily_use_.get());
55 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); 64 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get());
56 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); 65 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get());
57 daemon_.Init(true, &metrics_lib_); 66 kFakeDiskStats[0] = StringPrintf(kFakeDiskStatsFormat,
67 kFakeReadSectors[0], kFakeWriteSectors[0]);
68 kFakeDiskStats[1] = StringPrintf(kFakeDiskStatsFormat,
69 kFakeReadSectors[1], kFakeWriteSectors[1]);
70 CreateFakeDiskStatsFile(kFakeDiskStats[0].c_str());
71 daemon_.Init(true, &metrics_lib_, kFakeDiskStatsPath);
58 72
59 // Check configuration of a few histograms. 73 // Check configuration of a few histograms.
60 FrequencyCounter* frequency_counter = 74 FrequencyCounter* frequency_counter =
61 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName]; 75 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName];
62 const TaggedCounterReporter* reporter = GetReporter(frequency_counter); 76 const TaggedCounterReporter* reporter = GetReporter(frequency_counter);
63 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName, 77 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName,
64 reporter->histogram_name()); 78 reporter->histogram_name());
65 EXPECT_EQ(chromeos_metrics::kSecondsPerDay, 79 EXPECT_EQ(chromeos_metrics::kSecondsPerDay,
66 frequency_counter->cycle_duration()); 80 frequency_counter->cycle_duration());
67 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min()); 81 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 127
114 EXPECT_FALSE(daemon_.user_active_); 128 EXPECT_FALSE(daemon_.user_active_);
115 EXPECT_TRUE(daemon_.user_active_last_.is_null()); 129 EXPECT_TRUE(daemon_.user_active_last_.is_null());
116 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); 130 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_);
117 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); 131 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_);
118 132
119 file_util::Delete(FilePath(kTestDir), true); 133 file_util::Delete(FilePath(kTestDir), true);
120 file_util::CreateDirectory(FilePath(kTestDir)); 134 file_util::CreateDirectory(FilePath(kTestDir));
121 } 135 }
122 136
123 virtual void TearDown() {} 137 virtual void TearDown() {
138 EXPECT_EQ(unlink(kFakeDiskStatsPath), 0);
139 }
124 140
125 const TaggedCounterReporter* 141 const TaggedCounterReporter*
126 GetReporter(FrequencyCounter* frequency_counter) const { 142 GetReporter(FrequencyCounter* frequency_counter) const {
127 return static_cast<const TaggedCounterReporter*>( 143 return static_cast<const TaggedCounterReporter*>(
128 &frequency_counter->tagged_counter()); 144 &frequency_counter->tagged_counter());
129 } 145 }
130 146
131 void ExpectFrequencyFlushCalls() { 147 void ExpectFrequencyFlushCalls() {
132 MetricsDaemon::FrequencyCounters::iterator i; 148 MetricsDaemon::FrequencyCounters::iterator i;
133 for (i = daemon_.frequency_counters_.begin(); 149 for (i = daemon_.frequency_counters_.begin();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 231 }
216 return msg; 232 return msg;
217 } 233 }
218 234
219 // Deallocates the DBus message |msg| previously allocated through 235 // Deallocates the DBus message |msg| previously allocated through
220 // dbus_message_new*. 236 // dbus_message_new*.
221 void DeleteDBusMessage(DBusMessage* msg) { 237 void DeleteDBusMessage(DBusMessage* msg) {
222 dbus_message_unref(msg); 238 dbus_message_unref(msg);
223 } 239 }
224 240
225 // Get the frequency counter for the given name. 241 // Gets the frequency counter for the given name.
226 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { 242 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) {
227 return *static_cast<FrequencyCounterMock*>( 243 return *static_cast<FrequencyCounterMock*>(
228 daemon_.frequency_counters_[histogram_name]); 244 daemon_.frequency_counters_[histogram_name]);
229 } 245 }
230 246
247 // Creates or overwrites an input file containing fake disk stats.
248 void CreateFakeDiskStatsFile(const char* fake_stats) {
249 if (unlink(kFakeDiskStatsPath) < 0) {
250 EXPECT_EQ(errno, ENOENT);
251 }
252 FILE* f = fopen(kFakeDiskStatsPath, "w");
253 EXPECT_EQ(1, fwrite(fake_stats, strlen(fake_stats), 1, f));
254 EXPECT_EQ(0, fclose(f));
255 }
256
231 // The MetricsDaemon under test. 257 // The MetricsDaemon under test.
232 MetricsDaemon daemon_; 258 MetricsDaemon daemon_;
233 259
234 // Metrics library mock. It's a strict mock so that all unexpected 260 // Metrics library mock. It's a strict mock so that all unexpected
235 // metric generation calls are marked as failures. 261 // metric generation calls are marked as failures.
236 StrictMock<MetricsLibraryMock> metrics_lib_; 262 StrictMock<MetricsLibraryMock> metrics_lib_;
237 263
238 // Counter mocks. They are strict mocks so that all unexpected 264 // Counter mocks. They are strict mocks so that all unexpected
239 // update calls are marked as failures. They are pointers so that 265 // update calls are marked as failures. They are pointers so that
240 // they can replace the scoped_ptr's allocated by the daemon. 266 // they can replace the scoped_ptr's allocated by the daemon.
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 EXPECT_TRUE(daemon_.user_active_); 552 EXPECT_TRUE(daemon_.user_active_);
527 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_); 553 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_);
528 } 554 }
529 555
530 TEST_F(MetricsDaemonTest, GetHistogramPath) { 556 TEST_F(MetricsDaemonTest, GetHistogramPath) {
531 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", 557 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily",
532 daemon_.GetHistogramPath( 558 daemon_.GetHistogramPath(
533 MetricsDaemon::kMetricAnyCrashesDailyName).value()); 559 MetricsDaemon::kMetricAnyCrashesDailyName).value());
534 } 560 }
535 561
562 TEST_F(MetricsDaemonTest, ReportDiskStats) {
563 long int read_sectors_now, write_sectors_now;
564
565 CreateFakeDiskStatsFile(kFakeDiskStats[1].c_str());
566 daemon_.DiskStatsReadStats(&read_sectors_now, &write_sectors_now);
567 EXPECT_EQ(read_sectors_now, kFakeReadSectors[1]);
568 EXPECT_EQ(write_sectors_now, kFakeWriteSectors[1]);
569
570 MetricsDaemon::DiskStatsState ds_state = daemon_.diskstats_state_;
571 EXPECT_CALL(metrics_lib_,
572 SendToUMA(_, (kFakeReadSectors[1] - kFakeReadSectors[0]) / 30,
573 _, _, _));
574 EXPECT_CALL(metrics_lib_,
575 SendToUMA(_, (kFakeWriteSectors[1] - kFakeWriteSectors[0]) / 30,
576 _, _, _));
577 daemon_.DiskStatsCallback();
578 EXPECT_TRUE(ds_state != daemon_.diskstats_state_);
579 }
580
536 int main(int argc, char** argv) { 581 int main(int argc, char** argv) {
537 testing::InitGoogleTest(&argc, argv); 582 testing::InitGoogleTest(&argc, argv);
538 return RUN_ALL_TESTS(); 583 return RUN_ALL_TESTS();
539 } 584 }
OLDNEW
« no previous file with comments | « metrics_daemon_main.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698