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 namespace chromeos { | |
| 15 namespace { | |
| 16 // The suffix path of fake cpu frequency file for cpu0. | |
|
Daniel Erat
2017/05/03 22:35:43
nit: please add a blank line above each comment to
weidongg
2017/05/03 23:16:09
Done.
| |
| 17 const char kTimeInStateSuffixPathCpu0[] = "cpu0/time_in_state"; | |
| 18 // The suffix path of fake cpu frequency file for cpu1. | |
| 19 const char kTimeInStateSuffixPathCpu1[] = "cpu1/time_in_state"; | |
| 20 // The suffix path of fake cpu frequency file for all cpus. | |
| 21 const char kAllTimeInStateSuffixPath[] = "all_time_in_state"; | |
| 22 // The string content of the fake cpu frequency file for cpu0. | |
| 23 const char kTimeInStateContentCpu0[] = | |
| 24 "20000 30000000\n" | |
| 25 "60000 90000\n" | |
| 26 "100000 50000\n"; | |
| 27 // The string content of the fake cpu frequency file for cpu1. | |
| 28 const char kTimeInStateContentCpu1[] = | |
| 29 "20000 31000000\n" | |
| 30 "60000 91000\n" | |
| 31 "100000 51000\n"; | |
| 32 // The string content of the fake cpu frequency file for all cpus. | |
| 33 const char kAllTimeInStateContent[] = | |
| 34 "freq\t\tcpu0\t\tcpu1\t\t\n" | |
| 35 "20000\t\t30000000\t\t31000000\t\t\n" | |
| 36 "60000\t\t90000\t\t91000\t\t\n" | |
| 37 "100000\t\t50000\t\t51000\t\t\n"; | |
| 38 } // namespace | |
|
Daniel Erat
2017/05/03 22:35:43
nit: add a blank line above this one
weidongg
2017/05/03 23:16:09
Done.
| |
| 39 | |
| 40 class CpuDataCollectorTest : public testing::Test { | |
| 41 public: | |
| 42 CpuDataCollectorTest() | |
| 43 : kExpectedCpuFreqStateNames({"20", "60", "100"}), | |
| 44 kExpectedTimeInStateCpu0({300000000, 900000, 500000}), | |
| 45 kExpectedTimeInStateCpu1({310000000, 910000, 510000}) { | |
| 46 CHECK(temp_dir_.CreateUniqueTempDir()); | |
| 47 | |
| 48 time_in_state_path_cpu0_ = | |
| 49 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu0); | |
| 50 time_in_state_path_cpu1_ = | |
| 51 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu1); | |
| 52 all_time_in_state_path_ = | |
| 53 temp_dir_.GetPath().AppendASCII(kAllTimeInStateSuffixPath); | |
| 54 | |
| 55 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu0_)); | |
| 56 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu1_)); | |
| 57 CHECK(base::CreateTemporaryFile(&all_time_in_state_path_)); | |
| 58 CHECK(base::WriteFile(time_in_state_path_cpu0_, kTimeInStateContentCpu0, | |
| 59 static_cast<int>(strlen(kTimeInStateContentCpu0))) != | |
| 60 -1); | |
| 61 CHECK(base::WriteFile(time_in_state_path_cpu1_, kTimeInStateContentCpu1, | |
| 62 static_cast<int>(strlen(kTimeInStateContentCpu1))) != | |
| 63 -1); | |
| 64 CHECK(base::WriteFile(all_time_in_state_path_, kAllTimeInStateContent, | |
| 65 static_cast<int>(strlen(kAllTimeInStateContent))) != | |
| 66 -1); | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 const base::FilePath GetTimeInStatePathCpu0() const { | |
| 71 return time_in_state_path_cpu0_; | |
| 72 } | |
| 73 const base::FilePath GetTimeInStatePathCpu1() const { | |
| 74 return time_in_state_path_cpu1_; | |
| 75 } | |
| 76 const base::FilePath GetAllTimeInStatePath() const { | |
| 77 return all_time_in_state_path_; | |
| 78 } | |
| 79 // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState| | |
| 80 // or |ReadCpuFreqAllTimeInState| | |
| 81 const std::vector<std::string> kExpectedCpuFreqStateNames; | |
| 82 // Expected time_in_state of sample for cpu0. | |
| 83 const std::vector<int64_t> kExpectedTimeInStateCpu0; | |
| 84 // Expected time_in_state of sample for cpu1. | |
| 85 const std::vector<int64_t> kExpectedTimeInStateCpu1; | |
| 86 | |
| 87 private: | |
| 88 base::ScopedTempDir temp_dir_; | |
| 89 base::FilePath time_in_state_path_cpu0_; | |
| 90 base::FilePath time_in_state_path_cpu1_; | |
| 91 base::FilePath all_time_in_state_path_; | |
|
Daniel Erat
2017/05/03 22:35:43
you can just make all these members protected and
weidongg
2017/05/03 23:16:09
Done.
| |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest); | |
| 94 }; | |
| 95 | |
| 96 TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) { | |
| 97 std::vector<std::string> cpu_freq_state_names; | |
| 98 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 99 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 100 | |
| 101 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 102 GetTimeInStatePathCpu0(), &cpu_freq_state_names, &freq_sample_cpu0); | |
| 103 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 104 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_sample_cpu0.time_in_state); | |
| 105 | |
| 106 cpu_freq_state_names.clear(); | |
| 107 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 108 GetTimeInStatePathCpu1(), &cpu_freq_state_names, &freq_sample_cpu1); | |
| 109 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 110 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_sample_cpu1.time_in_state); | |
| 111 } | |
| 112 | |
| 113 TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) { | |
| 114 std::vector<std::string> cpu_freq_state_names; | |
| 115 std::vector<CpuDataCollector::StateOccupancySample> freq_samples; | |
| 116 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 117 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 118 // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online. | |
| 119 freq_sample_cpu0.cpu_online = true; | |
| 120 freq_sample_cpu1.cpu_online = true; | |
| 121 freq_samples.push_back(freq_sample_cpu0); | |
| 122 freq_samples.push_back(freq_sample_cpu1); | |
| 123 | |
| 124 CpuDataCollector::ReadCpuFreqAllTimeInState( | |
| 125 2, GetAllTimeInStatePath(), &cpu_freq_state_names, &freq_samples); | |
| 126 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 127 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_samples[0].time_in_state); | |
| 128 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_samples[1].time_in_state); | |
| 129 } | |
| 130 | |
| 131 } // namespace chromeos | |
| OLD | NEW |