Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/files/file.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 11 #include "chrome/browser/chromeos/power/cpu_data_collector.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using base::TimeDelta; | |
| 15 | |
| 16 namespace chromeos { | |
| 17 namespace { | |
| 18 | |
| 19 // The suffix path of fake cpu frequency file for cpu0. | |
| 20 const char kTimeInStateSuffixPathCpu0[] = "cpu0/time_in_state"; | |
|
xiyuan
2017/05/04 20:12:01
nit: const -> constexpr, here and other places.
weidongg
2017/05/04 20:32:46
Done.
| |
| 21 | |
| 22 // The suffix path of fake cpu frequency file for cpu1. | |
| 23 const char kTimeInStateSuffixPathCpu1[] = "cpu1/time_in_state"; | |
| 24 | |
| 25 // The suffix path of fake cpu frequency file for all cpus. | |
| 26 const char kAllTimeInStateSuffixPath[] = "all_time_in_state"; | |
| 27 | |
| 28 // The string content of the fake cpu frequency file for cpu0. | |
| 29 const char kTimeInStateContentCpu0[] = | |
| 30 "20000 30000000\n" | |
| 31 "60000 90000\n" | |
| 32 "100000 50000\n"; | |
| 33 | |
| 34 // The string content of the fake cpu frequency file for cpu1. | |
| 35 const char kTimeInStateContentCpu1[] = | |
| 36 "20000 31000000\n" | |
| 37 "60000 91000\n" | |
| 38 "100000 51000\n"; | |
| 39 | |
| 40 // The string content of the fake cpu frequency file for all cpus. | |
| 41 const char kAllTimeInStateContent[] = | |
| 42 "freq\t\tcpu0\t\tcpu1\t\t\n" | |
| 43 "20000\t\t30000000\t\t31000000\t\t\n" | |
| 44 "60000\t\t90000\t\t91000\t\t\n" | |
| 45 "100000\t\t50000\t\t51000\t\t\n"; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 class CpuDataCollectorTest : public testing::Test { | |
| 50 public: | |
| 51 CpuDataCollectorTest() | |
| 52 : kExpectedCpuFreqStateNames({"20", "60", "100"}), | |
| 53 kExpectedTimeInStateCpu0({TimeDelta::FromMilliseconds(300000000), | |
| 54 TimeDelta::FromMilliseconds(900000), | |
| 55 TimeDelta::FromMilliseconds(500000)}), | |
| 56 kExpectedTimeInStateCpu1({TimeDelta::FromMilliseconds(310000000), | |
| 57 TimeDelta::FromMilliseconds(910000), | |
| 58 TimeDelta::FromMilliseconds(510000)}) { | |
| 59 CHECK(temp_dir_.CreateUniqueTempDir()); | |
| 60 | |
| 61 time_in_state_path_cpu0_ = | |
| 62 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu0); | |
| 63 time_in_state_path_cpu1_ = | |
| 64 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu1); | |
| 65 all_time_in_state_path_ = | |
| 66 temp_dir_.GetPath().AppendASCII(kAllTimeInStateSuffixPath); | |
| 67 | |
| 68 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu0_)); | |
| 69 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu1_)); | |
| 70 CHECK(base::CreateTemporaryFile(&all_time_in_state_path_)); | |
| 71 CHECK(base::WriteFile(time_in_state_path_cpu0_, kTimeInStateContentCpu0, | |
| 72 static_cast<int>(strlen(kTimeInStateContentCpu0))) != | |
| 73 -1); | |
| 74 CHECK(base::WriteFile(time_in_state_path_cpu1_, kTimeInStateContentCpu1, | |
| 75 static_cast<int>(strlen(kTimeInStateContentCpu1))) != | |
| 76 -1); | |
| 77 CHECK(base::WriteFile(all_time_in_state_path_, kAllTimeInStateContent, | |
| 78 static_cast<int>(strlen(kAllTimeInStateContent))) != | |
| 79 -1); | |
|
xiyuan
2017/05/04 20:12:01
Any reason why not put all of these in an overidde
weidongg
2017/05/04 20:32:46
There are some constants to be initialized in the
xiyuan
2017/05/04 20:40:02
The const member are initialized in the member ini
weidongg
2017/05/04 21:03:45
Thanks for explanation. Done.
Daniel Erat
2017/05/04 21:12:46
not a huge deal either way, but just to give my ju
xiyuan
2017/05/04 21:25:35
My preference is to make ctor as simple as possibl
| |
| 80 } | |
| 81 | |
| 82 protected: | |
| 83 // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState| | |
| 84 // or |ReadCpuFreqAllTimeInState| | |
| 85 const std::vector<std::string> kExpectedCpuFreqStateNames; | |
| 86 | |
| 87 // Expected time_in_state of sample for cpu0. | |
| 88 const std::vector<TimeDelta> kExpectedTimeInStateCpu0; | |
| 89 | |
| 90 // Expected time_in_state of sample for cpu1. | |
| 91 const std::vector<TimeDelta> kExpectedTimeInStateCpu1; | |
| 92 | |
| 93 base::ScopedTempDir temp_dir_; | |
| 94 base::FilePath time_in_state_path_cpu0_; | |
| 95 base::FilePath time_in_state_path_cpu1_; | |
| 96 base::FilePath all_time_in_state_path_; | |
| 97 | |
| 98 private: | |
| 99 DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest); | |
| 100 }; | |
| 101 | |
| 102 TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) { | |
| 103 std::vector<std::string> cpu_freq_state_names; | |
| 104 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 105 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 106 | |
| 107 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 108 time_in_state_path_cpu0_, &cpu_freq_state_names, &freq_sample_cpu0); | |
| 109 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 110 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_sample_cpu0.time_in_state); | |
| 111 | |
| 112 cpu_freq_state_names.clear(); | |
| 113 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 114 time_in_state_path_cpu1_, &cpu_freq_state_names, &freq_sample_cpu1); | |
| 115 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 116 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_sample_cpu1.time_in_state); | |
| 117 } | |
| 118 | |
| 119 TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) { | |
| 120 std::vector<std::string> cpu_freq_state_names; | |
| 121 std::vector<CpuDataCollector::StateOccupancySample> freq_samples; | |
| 122 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 123 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 124 // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online. | |
| 125 freq_sample_cpu0.cpu_online = true; | |
| 126 freq_sample_cpu1.cpu_online = true; | |
| 127 freq_samples.push_back(freq_sample_cpu0); | |
| 128 freq_samples.push_back(freq_sample_cpu1); | |
| 129 | |
| 130 CpuDataCollector::ReadCpuFreqAllTimeInState( | |
| 131 2, all_time_in_state_path_, &cpu_freq_state_names, &freq_samples); | |
| 132 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 133 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_samples[0].time_in_state); | |
| 134 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_samples[1].time_in_state); | |
| 135 } | |
| 136 | |
| 137 } // namespace chromeos | |
| OLD | NEW |