Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| 21 #include "base/test/simple_test_clock.h" | |
| 19 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 21 #include "base/values.h" | 24 #include "base/values.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 27 |
| 25 namespace base { | 28 namespace base { |
| 26 namespace { | 29 namespace { |
| 27 | 30 |
| 28 const char kHomePage[] = "homepage"; | 31 const char kHomePage[] = "homepage"; |
| 29 | 32 |
| 33 // Set the time on the given SimpleTestClock to the given time in minutes. | |
| 34 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.
| |
| 35 const int32_t kBaseTimeMins = 100; | |
| 36 clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); | |
| 37 } | |
| 38 | |
| 30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 39 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 31 // to the |prefs| until explicitly asked to release them. | 40 // to the |prefs| until explicitly asked to release them. |
| 32 class InterceptingPrefFilter : public PrefFilter { | 41 class InterceptingPrefFilter : public PrefFilter { |
| 33 public: | 42 public: |
| 34 InterceptingPrefFilter(); | 43 InterceptingPrefFilter(); |
| 35 ~InterceptingPrefFilter() override; | 44 ~InterceptingPrefFilter() override; |
| 36 | 45 |
| 37 // PrefFilter implementation: | 46 // PrefFilter implementation: |
| 38 void FilterOnLoad( | 47 void FilterOnLoad( |
| 39 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 48 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 } // namespace | 94 } // namespace |
| 86 | 95 |
| 87 class JsonPrefStoreTest : public testing::Test { | 96 class JsonPrefStoreTest : public testing::Test { |
| 88 protected: | 97 protected: |
| 89 void SetUp() override { | 98 void SetUp() override { |
| 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 91 | 100 |
| 92 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 101 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
| 93 data_dir_ = data_dir_.AppendASCII("prefs"); | 102 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 94 ASSERT_TRUE(PathExists(data_dir_)); | 103 ASSERT_TRUE(PathExists(data_dir_)); |
| 104 | |
| 105 base::StatisticsRecorder::ResetForTesting(); | |
| 95 } | 106 } |
| 96 | 107 |
| 97 void TearDown() override { | 108 void TearDown() override { |
| 98 // Make sure all pending tasks have been processed (e.g., deleting the | 109 // Make sure all pending tasks have been processed (e.g., deleting the |
| 99 // JsonPrefStore may post write tasks). | 110 // JsonPrefStore may post write tasks). |
| 100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 111 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); |
| 101 message_loop_.Run(); | 112 message_loop_.Run(); |
| 102 } | 113 } |
| 103 | 114 |
| 104 // The path to temporary directory used to contain the test operations. | 115 // The path to temporary directory used to contain the test operations. |
| (...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 664 // "tabs": { | 675 // "tabs": { |
| 665 // "new_windows_in_tabs": true, | 676 // "new_windows_in_tabs": true, |
| 666 // "max_tabs": 20 | 677 // "max_tabs": 20 |
| 667 // } | 678 // } |
| 668 // } | 679 // } |
| 669 | 680 |
| 670 RunBasicJsonPrefStoreTest( | 681 RunBasicJsonPrefStoreTest( |
| 671 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 682 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 672 } | 683 } |
| 673 | 684 |
| 685 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { | |
| 686 SimpleTestClock* test_clock = new SimpleTestClock; | |
| 687 SetCurrentTimeInMinutes(test_clock, 0); | |
| 688 JsonPrefStore::WriteCountHistogram histogram( | |
| 689 base::TimeDelta::FromSeconds(10), | |
| 690 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | |
| 691 scoped_ptr<base::Clock>(test_clock)); | |
| 692 int32 report_interval = | |
| 693 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | |
| 694 | |
| 695 histogram.RecordWriteOccured(); | |
| 696 | |
| 697 SetCurrentTimeInMinutes(test_clock, 1.5 * report_interval); | |
| 698 histogram.ReportOutstandingWrites(); | |
| 699 scoped_ptr<HistogramSamples> samples = | |
| 700 histogram.GetHistogram()->SnapshotSamples(); | |
| 701 ASSERT_EQ(1, samples->GetCount(1)); | |
| 702 ASSERT_EQ(1, samples->TotalCount()); | |
| 703 | |
| 704 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", | |
| 705 histogram.GetHistogram()->histogram_name()); | |
| 706 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); | |
| 707 } | |
| 708 | |
| 709 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { | |
| 710 SimpleTestClock* test_clock = new SimpleTestClock; | |
| 711 SetCurrentTimeInMinutes(test_clock, 0); | |
| 712 JsonPrefStore::WriteCountHistogram histogram( | |
| 713 base::TimeDelta::FromSeconds(10), | |
| 714 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | |
| 715 scoped_ptr<base::Clock>(test_clock)); | |
| 716 int32 report_interval = | |
| 717 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | |
| 718 | |
| 719 histogram.RecordWriteOccured(); | |
| 720 SetCurrentTimeInMinutes(test_clock, 0.5 * report_interval); | |
| 721 histogram.RecordWriteOccured(); | |
| 722 SetCurrentTimeInMinutes(test_clock, 0.7 * report_interval); | |
| 723 histogram.RecordWriteOccured(); | |
| 724 | |
| 725 // Nothing should be recorded until the report period has elapsed. | |
| 726 scoped_ptr<HistogramSamples> samples = | |
| 727 histogram.GetHistogram()->SnapshotSamples(); | |
| 728 ASSERT_EQ(0, samples->TotalCount()); | |
| 729 | |
| 730 SetCurrentTimeInMinutes(test_clock, 1.3 * report_interval); | |
| 731 histogram.RecordWriteOccured(); | |
| 732 | |
| 733 // Now the report period has elapsed. | |
| 734 samples = histogram.GetHistogram()->SnapshotSamples(); | |
| 735 ASSERT_EQ(1, samples->GetCount(3)); | |
| 736 ASSERT_EQ(1, samples->TotalCount()); | |
| 737 | |
| 738 // The last write won't be recorded because the second count period hasn't | |
| 739 // fully elapsed. | |
| 740 SetCurrentTimeInMinutes(test_clock, 1.5 * report_interval); | |
| 741 histogram.ReportOutstandingWrites(); | |
| 742 | |
| 743 samples = histogram.GetHistogram()->SnapshotSamples(); | |
| 744 ASSERT_EQ(1, samples->GetCount(3)); | |
| 745 ASSERT_EQ(1, samples->TotalCount()); | |
| 746 } | |
| 747 | |
| 748 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { | |
| 749 SimpleTestClock* test_clock = new SimpleTestClock; | |
| 750 SetCurrentTimeInMinutes(test_clock, 0); | |
| 751 JsonPrefStore::WriteCountHistogram histogram( | |
| 752 base::TimeDelta::FromSeconds(10), | |
| 753 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | |
| 754 scoped_ptr<base::Clock>(test_clock)); | |
| 755 int32 report_interval = | |
| 756 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | |
| 757 | |
| 758 histogram.RecordWriteOccured(); | |
| 759 SetCurrentTimeInMinutes(test_clock, 0.5 * report_interval); | |
| 760 histogram.RecordWriteOccured(); | |
| 761 SetCurrentTimeInMinutes(test_clock, 0.7 * report_interval); | |
| 762 histogram.RecordWriteOccured(); | |
| 763 SetCurrentTimeInMinutes(test_clock, 1.3 * report_interval); | |
| 764 histogram.RecordWriteOccured(); | |
| 765 SetCurrentTimeInMinutes(test_clock, 1.5 * report_interval); | |
| 766 histogram.RecordWriteOccured(); | |
| 767 SetCurrentTimeInMinutes(test_clock, 2.1 * report_interval); | |
| 768 histogram.RecordWriteOccured(); | |
| 769 SetCurrentTimeInMinutes(test_clock, 2.5 * report_interval); | |
| 770 histogram.RecordWriteOccured(); | |
| 771 SetCurrentTimeInMinutes(test_clock, 2.7 * report_interval); | |
| 772 histogram.RecordWriteOccured(); | |
| 773 SetCurrentTimeInMinutes(test_clock, 3.3 * report_interval); | |
| 774 histogram.RecordWriteOccured(); | |
| 775 | |
| 776 // The last write won't be recorded because the second count period hasn't | |
| 777 // fully elapsed | |
| 778 SetCurrentTimeInMinutes(test_clock, 3.5 * report_interval); | |
| 779 histogram.ReportOutstandingWrites(); | |
| 780 scoped_ptr<HistogramSamples> samples = | |
| 781 histogram.GetHistogram()->SnapshotSamples(); | |
| 782 ASSERT_EQ(2, samples->GetCount(3)); | |
| 783 ASSERT_EQ(1, samples->GetCount(2)); | |
| 784 ASSERT_EQ(3, samples->TotalCount()); | |
| 785 } | |
| 786 | |
| 787 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { | |
| 788 SimpleTestClock* test_clock = new SimpleTestClock; | |
| 789 SetCurrentTimeInMinutes(test_clock, 0); | |
| 790 JsonPrefStore::WriteCountHistogram histogram( | |
| 791 base::TimeDelta::FromSeconds(10), | |
| 792 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | |
| 793 scoped_ptr<base::Clock>(test_clock)); | |
| 794 int32 report_interval = | |
| 795 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | |
| 796 | |
| 797 // 1 write in the first period. | |
| 798 histogram.RecordWriteOccured(); | |
| 799 | |
| 800 // No writes in the second and third periods. | |
| 801 | |
| 802 // 2 writes in the fourth period. | |
| 803 SetCurrentTimeInMinutes(test_clock, 3.1 * report_interval); | |
| 804 histogram.RecordWriteOccured(); | |
| 805 SetCurrentTimeInMinutes(test_clock, 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(test_clock, 5.1 * report_interval); | |
| 812 histogram.RecordWriteOccured(); | |
| 813 SetCurrentTimeInMinutes(test_clock, 5.3 * report_interval); | |
| 814 histogram.RecordWriteOccured(); | |
| 815 SetCurrentTimeInMinutes(test_clock, 5.5 * report_interval); | |
| 816 histogram.RecordWriteOccured(); | |
| 817 | |
| 818 SetCurrentTimeInMinutes(test_clock, 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 |
| OLD | NEW |