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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 "base/prefs/json_pref_store.h" 5 #include "base/prefs/json_pref_store.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/statistics_recorder.h"
13 #include "base/path_service.h" 15 #include "base/path_service.h"
14 #include "base/prefs/pref_filter.h" 16 #include "base/prefs/pref_filter.h"
15 #include "base/run_loop.h" 17 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/sequenced_worker_pool.h" 21 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
21 #include "base/values.h" 23 #include "base/values.h"
22 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 }; 80 };
79 81
80 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { 82 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
81 public: 83 public:
82 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); 84 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
83 }; 85 };
84 86
85 } // namespace 87 } // namespace
86 88
87 class JsonPrefStoreTest : public testing::Test { 89 class JsonPrefStoreTest : public testing::Test {
90 public:
91 base::Time current_time() { return current_time_; }
92
93 void SetCurrentTimeInMinutes(double minutes) {
94 const int32_t kBaseTimeMins = 100;
95 current_time_ = base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60);
96 }
97
88 protected: 98 protected:
89 void SetUp() override { 99 void SetUp() override {
90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
91 101
92 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); 102 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
93 data_dir_ = data_dir_.AppendASCII("prefs"); 103 data_dir_ = data_dir_.AppendASCII("prefs");
94 ASSERT_TRUE(PathExists(data_dir_)); 104 ASSERT_TRUE(PathExists(data_dir_));
105
106 base::StatisticsRecorder::InitializeForTest();
107 SetCurrentTimeInMinutes(0);
95 } 108 }
96 109
97 void TearDown() override { 110 void TearDown() override {
98 // Make sure all pending tasks have been processed (e.g., deleting the 111 // Make sure all pending tasks have been processed (e.g., deleting the
99 // JsonPrefStore may post write tasks). 112 // JsonPrefStore may post write tasks).
100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); 113 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
101 message_loop_.Run(); 114 message_loop_.Run();
102 } 115 }
103 116
117 base::Time current_time_;
118
104 // The path to temporary directory used to contain the test operations. 119 // The path to temporary directory used to contain the test operations.
105 base::ScopedTempDir temp_dir_; 120 base::ScopedTempDir temp_dir_;
106 // The path to the directory where the test data is stored. 121 // The path to the directory where the test data is stored.
107 base::FilePath data_dir_; 122 base::FilePath data_dir_;
108 // A message loop that we can use as the file thread message loop. 123 // A message loop that we can use as the file thread message loop.
109 MessageLoop message_loop_; 124 MessageLoop message_loop_;
110 }; 125 };
111 126
112 // Test fallback behavior for a nonexistent file. 127 // Test fallback behavior for a nonexistent file.
113 TEST_F(JsonPrefStoreTest, NonExistentFile) { 128 TEST_F(JsonPrefStoreTest, NonExistentFile) {
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 // "tabs": { 679 // "tabs": {
665 // "new_windows_in_tabs": true, 680 // "new_windows_in_tabs": true,
666 // "max_tabs": 20 681 // "max_tabs": 20
667 // } 682 // }
668 // } 683 // }
669 684
670 RunBasicJsonPrefStoreTest( 685 RunBasicJsonPrefStoreTest(
671 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); 686 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
672 } 687 }
673 688
689 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) {
690 JsonPrefStore::WriteCountHistogram histogram(
691 base::TimeDelta::FromSeconds(10),
692 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
693 base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this)));
694 int32 report_interval =
695 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
696
697 SetCurrentTimeInMinutes(0);
698 histogram.RecordWriteOccured();
699
700 SetCurrentTimeInMinutes(1.5 * report_interval);
701 histogram.ReportOutstandingWrites();
702 scoped_ptr<HistogramSamples> samples =
703 histogram.GetHistogram()->SnapshotSamples();
704 ASSERT_EQ(1, samples->GetCount(1));
705 ASSERT_EQ(1, samples->TotalCount());
706
707 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State",
708 histogram.GetHistogram()->histogram_name());
709 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31));
710 }
711
712 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) {
713 JsonPrefStore::WriteCountHistogram histogram(
714 base::TimeDelta::FromSeconds(10),
715 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
716 base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this)));
717 int32 report_interval =
718 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
719
720 SetCurrentTimeInMinutes(0);
721 histogram.RecordWriteOccured();
722 SetCurrentTimeInMinutes(0.5 * report_interval);
723 histogram.RecordWriteOccured();
724 SetCurrentTimeInMinutes(0.7 * report_interval);
725 histogram.RecordWriteOccured();
726
727 // Nothing should be recorded until the report period has elapsed.
728 scoped_ptr<HistogramSamples> samples =
729 histogram.GetHistogram()->SnapshotSamples();
730 ASSERT_EQ(0, samples->TotalCount());
731
732 SetCurrentTimeInMinutes(1.3 * report_interval);
733 histogram.RecordWriteOccured();
734
735 // Now the report period has elapsed.
736 samples = histogram.GetHistogram()->SnapshotSamples();
737 ASSERT_EQ(1, samples->GetCount(3));
738 ASSERT_EQ(1, samples->TotalCount());
739
740 // The last write won't be recorded because the second count period hasn't
741 // fully elapsed.
742 SetCurrentTimeInMinutes(1.5 * report_interval);
743 histogram.ReportOutstandingWrites();
744
745 samples = histogram.GetHistogram()->SnapshotSamples();
746 ASSERT_EQ(1, samples->GetCount(3));
747 ASSERT_EQ(1, samples->TotalCount());
748 }
749
750 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) {
751 JsonPrefStore::WriteCountHistogram histogram(
752 base::TimeDelta::FromSeconds(10),
753 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
754 base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this)));
755 int32 report_interval =
756 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
757
758 SetCurrentTimeInMinutes(0);
759 histogram.RecordWriteOccured();
760 SetCurrentTimeInMinutes(0.5 * report_interval);
761 histogram.RecordWriteOccured();
762 SetCurrentTimeInMinutes(0.7 * report_interval);
763 histogram.RecordWriteOccured();
764 SetCurrentTimeInMinutes(1.3 * report_interval);
765 histogram.RecordWriteOccured();
766 SetCurrentTimeInMinutes(1.5 * report_interval);
767 histogram.RecordWriteOccured();
768 SetCurrentTimeInMinutes(2.1 * report_interval);
769 histogram.RecordWriteOccured();
770 SetCurrentTimeInMinutes(2.5 * report_interval);
771 histogram.RecordWriteOccured();
772 SetCurrentTimeInMinutes(2.7 * report_interval);
773 histogram.RecordWriteOccured();
774 SetCurrentTimeInMinutes(3.3 * report_interval);
775 histogram.RecordWriteOccured();
776
777 // The last write won't be recorded because the second count period hasn't
778 // fully elapsed
779 SetCurrentTimeInMinutes(3.5 * report_interval);
780 histogram.ReportOutstandingWrites();
781 scoped_ptr<HistogramSamples> samples =
782 histogram.GetHistogram()->SnapshotSamples();
783 ASSERT_EQ(2, samples->GetCount(3));
784 ASSERT_EQ(1, samples->GetCount(2));
785 ASSERT_EQ(3, samples->TotalCount());
786 }
787
788 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) {
789 JsonPrefStore::WriteCountHistogram histogram(
790 base::TimeDelta::FromSeconds(10),
791 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")),
792 base::Bind(&JsonPrefStoreTest::current_time, base::Unretained(this)));
793 int32 report_interval =
794 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins;
795
796 // 1 write in the first period.
797 SetCurrentTimeInMinutes(0);
798 histogram.RecordWriteOccured();
799
800 // No writes in the second and third periods.
801
802 // 2 writes in the fourth period.
803 SetCurrentTimeInMinutes(3.1 * report_interval);
804 histogram.RecordWriteOccured();
805 SetCurrentTimeInMinutes(3.3 * report_interval);
806 histogram.RecordWriteOccured();
807
808 // No writes in the fifth period.
809
810 // 3 writes in the sixth period.
811 SetCurrentTimeInMinutes(5.1 * report_interval);
812 histogram.RecordWriteOccured();
813 SetCurrentTimeInMinutes(5.3 * report_interval);
814 histogram.RecordWriteOccured();
815 SetCurrentTimeInMinutes(5.5 * report_interval);
816 histogram.RecordWriteOccured();
817
818 SetCurrentTimeInMinutes(6.1 * report_interval);
819 histogram.ReportOutstandingWrites();
820 scoped_ptr<HistogramSamples> samples =
821 histogram.GetHistogram()->SnapshotSamples();
822 ASSERT_EQ(3, samples->GetCount(0));
823 ASSERT_EQ(1, samples->GetCount(1));
824 ASSERT_EQ(1, samples->GetCount(2));
825 ASSERT_EQ(1, samples->GetCount(3));
826 ASSERT_EQ(6, samples->TotalCount());
827 }
828
674 } // namespace base 829 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698