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. | |
| 17 const char time_in_state_suffix_path_cpu0[] = "cpu0/time_in_state"; | |
|
Daniel Erat
2017/05/02 23:41:11
please follow the style guide's naming convention
weidongg
2017/05/03 21:32:32
Done.
| |
| 18 // The suffix path of fake cpu frequency file for cpu1. | |
| 19 const char time_in_state_suffix_path_cpu1[] = "cpu1/time_in_state"; | |
| 20 // The suffix path of fake cpu frequency file for all cpus. | |
| 21 const char all_time_in_state_suffix_path[] = "all_time_in_state"; | |
| 22 // The string content of the fake cpu frequency file for cpu0. | |
| 23 const char time_in_state_content_cpu0[] = | |
| 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 time_in_state_content_cpu1[] = | |
| 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 all_time_in_state_content[] = | |
| 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 // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState| or | |
| 39 // |ReadCpuFreqAllTimeInState| | |
| 40 const std::vector<std::string> expected_cpu_freq_state_names = {"20", "60", | |
|
Daniel Erat
2017/05/02 23:41:10
class-type static variables aren't allowed: https:
weidongg
2017/05/03 21:32:32
Done.
| |
| 41 "100"}; | |
| 42 // Expceted time_in_state of sample in |freq_samples| for cpu0. | |
|
Daniel Erat
2017/05/02 23:41:11
"Expected"
weidongg
2017/05/03 21:32:32
Done.
I put these expected variables in the test c
| |
| 43 const std::vector<int64_t> expected_time_in_state_cpu0{300000000, 900000, | |
| 44 500000}; | |
| 45 // Expceted time_in_state of sample in |freq_samples| for cpu1. | |
|
Daniel Erat
2017/05/02 23:41:10
|freq_samples| is a local var defined it tests; re
weidongg
2017/05/03 21:32:32
Removed
| |
| 46 const std::vector<int64_t> expected_time_in_state_cpu1{310000000, 910000, | |
| 47 510000}; | |
| 48 } // namespace | |
| 49 | |
| 50 class CpuDataCollectorTest : public testing::Test { | |
| 51 public: | |
| 52 CpuDataCollectorTest() {} | |
| 53 ~CpuDataCollectorTest() override {} | |
| 54 | |
| 55 void SetUp() override { | |
| 56 CHECK(temp_dir.CreateUniqueTempDir()); | |
|
Daniel Erat
2017/05/02 23:41:10
put code in c'tor and d'tor instead of SetUp/TearD
weidongg
2017/05/03 21:32:32
Done.
| |
| 57 | |
| 58 time_in_state_path_cpu0 = | |
| 59 temp_dir.GetPath().AppendASCII(time_in_state_suffix_path_cpu0); | |
| 60 time_in_state_path_cpu1 = | |
| 61 temp_dir.GetPath().AppendASCII(time_in_state_suffix_path_cpu1); | |
| 62 all_time_in_state_path = | |
| 63 temp_dir.GetPath().AppendASCII(all_time_in_state_suffix_path); | |
| 64 | |
| 65 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu0)); | |
|
Daniel Erat
2017/05/02 23:41:11
remove all of these CreateTemporaryFile calls. see
weidongg
2017/05/03 21:32:32
When I removed these CreateTemporaryFile calls and
| |
| 66 CHECK(base::CreateTemporaryFile(&time_in_state_path_cpu1)); | |
| 67 CHECK(base::CreateTemporaryFile(&all_time_in_state_path)); | |
| 68 CHECK(base::WriteFile( | |
| 69 time_in_state_path_cpu0, time_in_state_content_cpu0, | |
| 70 static_cast<int>(strlen(time_in_state_content_cpu0))) != -1); | |
| 71 CHECK(base::WriteFile( | |
| 72 time_in_state_path_cpu1, time_in_state_content_cpu1, | |
| 73 static_cast<int>(strlen(time_in_state_content_cpu1))) != -1); | |
| 74 CHECK(base::WriteFile( | |
| 75 all_time_in_state_path, all_time_in_state_content, | |
| 76 static_cast<int>(strlen(all_time_in_state_content))) != -1); | |
| 77 } | |
| 78 | |
| 79 void TearDown() override { CHECK(temp_dir.Delete()); } | |
|
Daniel Erat
2017/05/02 23:41:11
you don't need this call. ScopedTempDir's d'tor al
weidongg
2017/05/03 21:32:31
Removed
| |
| 80 | |
| 81 protected: | |
| 82 base::ScopedTempDir temp_dir; | |
|
Daniel Erat
2017/05/02 23:41:10
please read the style guide section on naming: htt
weidongg
2017/05/03 21:32:32
Done.
| |
| 83 base::FilePath time_in_state_path_cpu0; | |
|
Daniel Erat
2017/05/02 23:41:11
make all of these const after you initialize them
weidongg
2017/05/03 21:32:32
The initialization of these variable should happen
| |
| 84 base::FilePath time_in_state_path_cpu1; | |
| 85 base::FilePath all_time_in_state_path; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest); | |
|
Daniel Erat
2017/05/02 23:41:10
put this in the private section
weidongg
2017/05/03 21:32:32
Done.
| |
| 88 }; | |
| 89 | |
| 90 TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) { | |
| 91 std::vector<std::string> cpu_freq_state_names; | |
| 92 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 93 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 94 | |
| 95 CpuDataCollector::ReadCpuFreqTimeInState(time_in_state_path_cpu0.value(), | |
| 96 &cpu_freq_state_names, | |
| 97 &freq_sample_cpu0); | |
| 98 EXPECT_EQ(expected_cpu_freq_state_names, cpu_freq_state_names); | |
| 99 EXPECT_EQ(expected_time_in_state_cpu0, freq_sample_cpu0.time_in_state); | |
| 100 | |
| 101 CpuDataCollector::ReadCpuFreqTimeInState(time_in_state_path_cpu1.value(), | |
| 102 &cpu_freq_state_names, | |
| 103 &freq_sample_cpu1); | |
| 104 EXPECT_EQ(expected_cpu_freq_state_names, cpu_freq_state_names); | |
| 105 EXPECT_EQ(expected_time_in_state_cpu1, freq_sample_cpu1.time_in_state); | |
| 106 } | |
| 107 | |
| 108 TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) { | |
| 109 std::vector<std::string> cpu_freq_state_names; | |
| 110 std::vector<CpuDataCollector::StateOccupancySample> freq_samples; | |
| 111 CpuDataCollector::StateOccupancySample freq_sample_cpu0; | |
| 112 CpuDataCollector::StateOccupancySample freq_sample_cpu1; | |
| 113 // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online. | |
| 114 freq_sample_cpu0.cpu_online = true; | |
| 115 freq_sample_cpu1.cpu_online = true; | |
| 116 freq_samples.push_back(freq_sample_cpu0); | |
| 117 freq_samples.push_back(freq_sample_cpu1); | |
| 118 | |
| 119 CpuDataCollector::ReadCpuFreqAllTimeInState( | |
| 120 2, all_time_in_state_path.value(), &cpu_freq_state_names, &freq_samples); | |
| 121 EXPECT_EQ(expected_cpu_freq_state_names, cpu_freq_state_names); | |
| 122 EXPECT_EQ(expected_time_in_state_cpu0, freq_samples[0].time_in_state); | |
| 123 EXPECT_EQ(expected_time_in_state_cpu1, freq_samples[1].time_in_state); | |
| 124 } | |
| 125 | |
| 126 } // namespace chromeos | |
| OLD | NEW |