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

Unified Diff: base/prefs/json_pref_store_unittest.cc

Issue 1083603002: Add histograms to record the number of writes to the prefs file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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/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..4e21d60ae88458bf038f8814b9f99913b9c8f332 100644
--- a/base/prefs/json_pref_store_unittest.cc
+++ b/base/prefs/json_pref_store_unittest.cc
@@ -10,12 +10,15 @@
#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"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/simple_test_clock.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread.h"
#include "base/values.h"
@@ -27,6 +30,12 @@ namespace {
const char kHomePage[] = "homepage";
+// Set the time on the given SimpleTestClock to the given time in minutes.
+void SetCurrentTimeInMinutes(base::SimpleTestClock* clock, double minutes) {
Alexei Svitkine (slow) 2015/04/27 20:21:20 Nit: Modifiable parameters should be last.
raymes 2015/04/28 00:32:58 Done.
+ const int32_t kBaseTimeMins = 100;
+ clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60));
+}
+
// A PrefFilter that will intercept all calls to FilterOnLoad() and hold on
// to the |prefs| until explicitly asked to release them.
class InterceptingPrefFilter : public PrefFilter {
@@ -92,6 +101,8 @@ 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::ResetForTesting();
}
void TearDown() override {
@@ -671,4 +682,148 @@ TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) {
pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
}
+TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) {
+ SimpleTestClock* test_clock = new SimpleTestClock;
+ SetCurrentTimeInMinutes(test_clock, 0);
+ JsonPrefStore::WriteCountHistogram histogram(
+ base::TimeDelta::FromSeconds(10),
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
+ scoped_ptr<base::Clock>(test_clock));
+ int32 report_interval =
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
+
+ histogram.RecordWriteOccured();
+
+ SetCurrentTimeInMinutes(test_clock, 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) {
+ SimpleTestClock* test_clock = new SimpleTestClock;
+ SetCurrentTimeInMinutes(test_clock, 0);
+ JsonPrefStore::WriteCountHistogram histogram(
+ base::TimeDelta::FromSeconds(10),
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
+ scoped_ptr<base::Clock>(test_clock));
+ int32 report_interval =
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
+
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 0.5 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 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(test_clock, 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(test_clock, 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) {
+ SimpleTestClock* test_clock = new SimpleTestClock;
+ SetCurrentTimeInMinutes(test_clock, 0);
+ JsonPrefStore::WriteCountHistogram histogram(
+ base::TimeDelta::FromSeconds(10),
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
+ scoped_ptr<base::Clock>(test_clock));
+ int32 report_interval =
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
+
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 0.5 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 0.7 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 1.3 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 1.5 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 2.1 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 2.5 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 2.7 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 3.3 * report_interval);
+ histogram.RecordWriteOccured();
+
+ // The last write won't be recorded because the second count period hasn't
+ // fully elapsed
+ SetCurrentTimeInMinutes(test_clock, 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) {
+ SimpleTestClock* test_clock = new SimpleTestClock;
+ SetCurrentTimeInMinutes(test_clock, 0);
+ JsonPrefStore::WriteCountHistogram histogram(
+ base::TimeDelta::FromSeconds(10),
+ base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
+ scoped_ptr<base::Clock>(test_clock));
+ int32 report_interval =
+ JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
+
+ // 1 write in the first period.
+ histogram.RecordWriteOccured();
+
+ // No writes in the second and third periods.
+
+ // 2 writes in the fourth period.
+ SetCurrentTimeInMinutes(test_clock, 3.1 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 3.3 * report_interval);
+ histogram.RecordWriteOccured();
+
+ // No writes in the fifth period.
+
+ // 3 writes in the sixth period.
+ SetCurrentTimeInMinutes(test_clock, 5.1 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 5.3 * report_interval);
+ histogram.RecordWriteOccured();
+ SetCurrentTimeInMinutes(test_clock, 5.5 * report_interval);
+ histogram.RecordWriteOccured();
+
+ SetCurrentTimeInMinutes(test_clock, 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

Powered by Google App Engine
This is Rietveld 408576698