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 constexpr char kTimeInStateSuffixPathCpu0[] = "cpu0/time_in_state"; | |
| 21 | |
| 22 // The suffix path of fake cpu frequency file for cpu1. | |
| 23 constexpr char kTimeInStateSuffixPathCpu1[] = "cpu1/time_in_state"; | |
| 24 | |
| 25 // The suffix path of fake cpu frequency file for all cpus. | |
| 26 constexpr char kAllTimeInStateSuffixPath[] = "all_time_in_state"; | |
| 27 | |
| 28 // The string content of the fake cpu frequency file for cpu0. | |
| 29 constexpr 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 constexpr 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 constexpr 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 | |
| 60 void SetUp() override { | |
|
xiyuan
2017/05/04 21:25:35
nit: // testing::Test:
weidongg
2017/05/04 21:38:50
Done.
| |
| 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 62 | |
| 63 time_in_state_path_cpu0_ = | |
| 64 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu0); | |
| 65 time_in_state_path_cpu1_ = | |
| 66 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu1); | |
| 67 all_time_in_state_path_ = | |
| 68 temp_dir_.GetPath().AppendASCII(kAllTimeInStateSuffixPath); | |
| 69 | |
| 70 ASSERT_TRUE(base::CreateTemporaryFile(&time_in_state_path_cpu0_)); | |
| 71 ASSERT_TRUE(base::CreateTemporaryFile(&time_in_state_path_cpu1_)); | |
| 72 ASSERT_TRUE(base::CreateTemporaryFile(&all_time_in_state_path_)); | |
| 73 ASSERT_TRUE(base::WriteFile( | |
| 74 time_in_state_path_cpu0_, kTimeInStateContentCpu0, | |
| 75 static_cast<int>(strlen(kTimeInStateContentCpu0))) != -1); | |
| 76 ASSERT_TRUE(base::WriteFile( | |
| 77 time_in_state_path_cpu1_, kTimeInStateContentCpu1, | |
| 78 static_cast<int>(strlen(kTimeInStateContentCpu1))) != -1); | |
| 79 ASSERT_TRUE(base::WriteFile( | |
| 80 all_time_in_state_path_, kAllTimeInStateContent, | |
| 81 static_cast<int>(strlen(kAllTimeInStateContent))) != -1); | |
| 82 } | |
| 83 | |
| 84 protected: | |
| 85 // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState| | |
| 86 // or |ReadCpuFreqAllTimeInState| | |
| 87 const std::vector<std::string> kExpectedCpuFreqStateNames; | |
| 88 | |
| 89 // Expected time_in_state of sample for cpu0. | |
| 90 const std::vector<TimeDelta> kExpectedTimeInStateCpu0; | |
| 91 | |
| 92 // Expected time_in_state of sample for cpu1. | |
| 93 const std::vector<TimeDelta> kExpectedTimeInStateCpu1; | |
| 94 | |
| 95 base::ScopedTempDir temp_dir_; | |
| 96 base::FilePath time_in_state_path_cpu0_; | |
| 97 base::FilePath time_in_state_path_cpu1_; | |
| 98 base::FilePath all_time_in_state_path_; | |
| 99 | |
| 100 private: | |
| 101 DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest); | |
| 102 }; | |
| 103 | |
| 104 TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) { | |
| 105 std::vector<std::string> cpu_freq_state_names; | |
| 106 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 107 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 108 | |
| 109 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 110 time_in_state_path_cpu0_, &cpu_freq_state_names, &freq_sample_cpu0); | |
| 111 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 112 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_sample_cpu0.time_in_state); | |
| 113 | |
| 114 cpu_freq_state_names.clear(); | |
| 115 CpuDataCollector::ReadCpuFreqTimeInState( | |
| 116 time_in_state_path_cpu1_, &cpu_freq_state_names, &freq_sample_cpu1); | |
| 117 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 118 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_sample_cpu1.time_in_state); | |
| 119 } | |
| 120 | |
| 121 TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) { | |
| 122 std::vector<std::string> cpu_freq_state_names; | |
| 123 std::vector<CpuDataCollector::StateOccupancySample> freq_samples; | |
| 124 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 125 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 126 // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online. | |
| 127 freq_sample_cpu0.cpu_online = true; | |
| 128 freq_sample_cpu1.cpu_online = true; | |
| 129 freq_samples.push_back(freq_sample_cpu0); | |
| 130 freq_samples.push_back(freq_sample_cpu1); | |
| 131 | |
| 132 CpuDataCollector::ReadCpuFreqAllTimeInState( | |
| 133 2, all_time_in_state_path_, &cpu_freq_state_names, &freq_samples); | |
| 134 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names); | |
| 135 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_samples[0].time_in_state); | |
| 136 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_samples[1].time_in_state); | |
| 137 } | |
| 138 | |
| 139 } // namespace chromeos | |
| OLD | NEW |