Index: base/prefs/json_pref_store_unittest.cc |
diff --git a/base/prefs/json_pref_store_unittest.cc b/base/prefs/json_pref_store_unittest.cc |
index ff68836014f65bcb780e8aac035224e79a61d655..37406670139734600057d849162d0e535a2e4e29 100644 |
--- a/base/prefs/json_pref_store_unittest.cc |
+++ b/base/prefs/json_pref_store_unittest.cc |
@@ -10,6 +10,8 @@ |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/metrics/histogram_samples.h" |
+#include "base/metrics/statistics_recorder.h" |
#include "base/path_service.h" |
#include "base/prefs/pref_filter.h" |
#include "base/run_loop.h" |
@@ -85,6 +87,14 @@ class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
} // namespace |
class JsonPrefStoreTest : public testing::Test { |
+ public: |
+ base::Time current_time() { return current_time_; } |
+ |
+ void SetCurrentTimeInMinutes(double minutes) { |
+ const int32_t kBaseTimeMins = 100; |
+ current_time_ = base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60); |
+ } |
+ |
protected: |
void SetUp() override { |
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
@@ -92,6 +102,9 @@ class JsonPrefStoreTest : public testing::Test { |
ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
data_dir_ = data_dir_.AppendASCII("prefs"); |
ASSERT_TRUE(PathExists(data_dir_)); |
+ |
+ base::StatisticsRecorder::InitializeForTest(); |
+ SetCurrentTimeInMinutes(0); |
} |
void TearDown() override { |
@@ -101,6 +114,8 @@ class JsonPrefStoreTest : public testing::Test { |
message_loop_.Run(); |
} |
+ base::Time current_time_; |
+ |
// The path to temporary directory used to contain the test operations. |
base::ScopedTempDir temp_dir_; |
// The path to the directory where the test data is stored. |
@@ -671,4 +686,144 @@ TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { |
pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
} |
+TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
+ JsonPrefStore::WriteCountHistogram histogram( |
+ base::TimeDelta::FromSeconds(10), |
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
+ base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this))); |
+ int32 report_interval = |
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
+ |
+ SetCurrentTimeInMinutes(0); |
+ histogram.RecordWriteOccured(); |
+ |
+ SetCurrentTimeInMinutes(1.5 * report_interval); |
+ histogram.ReportOutstandingWrites(); |
+ scoped_ptr<HistogramSamples> samples = |
+ histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(1, samples->GetCount(1)); |
+ ASSERT_EQ(1, samples->TotalCount()); |
+ |
+ ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
+ histogram.GetHistogram()->histogram_name()); |
+ ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
+} |
+ |
+TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
+ JsonPrefStore::WriteCountHistogram histogram( |
+ base::TimeDelta::FromSeconds(10), |
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
+ base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this))); |
+ int32 report_interval = |
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
+ |
+ SetCurrentTimeInMinutes(0); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(0.5 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(0.7 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ |
+ // Nothing should be recorded until the report period has elapsed. |
+ scoped_ptr<HistogramSamples> samples = |
+ histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(0, samples->TotalCount()); |
+ |
+ SetCurrentTimeInMinutes(1.3 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ |
+ // Now the report period has elapsed. |
+ samples = histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(1, samples->GetCount(3)); |
+ ASSERT_EQ(1, samples->TotalCount()); |
+ |
+ // The last write won't be recorded because the second count period hasn't |
+ // fully elapsed. |
+ SetCurrentTimeInMinutes(1.5 * report_interval); |
+ histogram.ReportOutstandingWrites(); |
+ |
+ samples = histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(1, samples->GetCount(3)); |
+ ASSERT_EQ(1, samples->TotalCount()); |
+} |
+ |
+TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
+ JsonPrefStore::WriteCountHistogram histogram( |
+ base::TimeDelta::FromSeconds(10), |
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
+ base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this))); |
+ int32 report_interval = |
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
+ |
+ SetCurrentTimeInMinutes(0); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(0.5 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(0.7 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(1.3 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(1.5 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(2.1 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(2.5 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(2.7 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(3.3 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ |
+ // The last write won't be recorded because the second count period hasn't |
+ // fully elapsed |
+ SetCurrentTimeInMinutes(3.5 * report_interval); |
+ histogram.ReportOutstandingWrites(); |
+ scoped_ptr<HistogramSamples> samples = |
+ histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(2, samples->GetCount(3)); |
+ ASSERT_EQ(1, samples->GetCount(2)); |
+ ASSERT_EQ(3, samples->TotalCount()); |
+} |
+ |
+TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
+ JsonPrefStore::WriteCountHistogram histogram( |
+ base::TimeDelta::FromSeconds(10), |
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
+ base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this))); |
+ int32 report_interval = |
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
+ |
+ // 1 write in the first period. |
+ SetCurrentTimeInMinutes(0); |
+ histogram.RecordWriteOccured(); |
+ |
+ // No writes in the second and third periods. |
+ |
+ // 2 writes in the fourth period. |
+ SetCurrentTimeInMinutes(3.1 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(3.3 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ |
+ // No writes in the fifth period. |
+ |
+ // 3 writes in the sixth period. |
+ SetCurrentTimeInMinutes(5.1 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(5.3 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ SetCurrentTimeInMinutes(5.5 * report_interval); |
+ histogram.RecordWriteOccured(); |
+ |
+ SetCurrentTimeInMinutes(6.1 * report_interval); |
+ histogram.ReportOutstandingWrites(); |
+ scoped_ptr<HistogramSamples> samples = |
+ histogram.GetHistogram()->SnapshotSamples(); |
+ ASSERT_EQ(3, samples->GetCount(0)); |
+ ASSERT_EQ(1, samples->GetCount(1)); |
+ ASSERT_EQ(1, samples->GetCount(2)); |
+ ASSERT_EQ(1, samples->GetCount(3)); |
+ ASSERT_EQ(6, samples->TotalCount()); |
+} |
+ |
} // namespace base |