Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: chrome/browser/chromeos/power/cpu_data_collector_unittest.cc

Issue 2853863002: Add parser for new cpu freq file (Closed)
Patch Set: Add '// testing::Test:' Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // testing::Test:
61 void SetUp() override {
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
63
64 time_in_state_path_cpu0_ =
65 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu0);
66 time_in_state_path_cpu1_ =
67 temp_dir_.GetPath().AppendASCII(kTimeInStateSuffixPathCpu1);
68 all_time_in_state_path_ =
69 temp_dir_.GetPath().AppendASCII(kAllTimeInStateSuffixPath);
70
71 ASSERT_TRUE(base::CreateTemporaryFile(&time_in_state_path_cpu0_));
72 ASSERT_TRUE(base::CreateTemporaryFile(&time_in_state_path_cpu1_));
73 ASSERT_TRUE(base::CreateTemporaryFile(&all_time_in_state_path_));
74 ASSERT_TRUE(base::WriteFile(
75 time_in_state_path_cpu0_, kTimeInStateContentCpu0,
76 static_cast<int>(strlen(kTimeInStateContentCpu0))) != -1);
77 ASSERT_TRUE(base::WriteFile(
78 time_in_state_path_cpu1_, kTimeInStateContentCpu1,
79 static_cast<int>(strlen(kTimeInStateContentCpu1))) != -1);
80 ASSERT_TRUE(base::WriteFile(
81 all_time_in_state_path_, kAllTimeInStateContent,
82 static_cast<int>(strlen(kAllTimeInStateContent))) != -1);
83 }
84
85 protected:
86 // Expected cpu frequency state names after calling |ReadCpuFreqTimeInState|
87 // or |ReadCpuFreqAllTimeInState|
88 const std::vector<std::string> kExpectedCpuFreqStateNames;
89
90 // Expected time_in_state of sample for cpu0.
91 const std::vector<TimeDelta> kExpectedTimeInStateCpu0;
92
93 // Expected time_in_state of sample for cpu1.
94 const std::vector<TimeDelta> kExpectedTimeInStateCpu1;
95
96 base::ScopedTempDir temp_dir_;
97 base::FilePath time_in_state_path_cpu0_;
98 base::FilePath time_in_state_path_cpu1_;
99 base::FilePath all_time_in_state_path_;
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(CpuDataCollectorTest);
103 };
104
105 TEST_F(CpuDataCollectorTest, ReadCpuFreqTimeInState) {
106 std::vector<std::string> cpu_freq_state_names;
107 CpuDataCollector::StateOccupancySample freq_sample_cpu0;
108 CpuDataCollector::StateOccupancySample freq_sample_cpu1;
109
110 CpuDataCollector::ReadCpuFreqTimeInState(
111 time_in_state_path_cpu0_, &cpu_freq_state_names, &freq_sample_cpu0);
112 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names);
113 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_sample_cpu0.time_in_state);
114
115 cpu_freq_state_names.clear();
116 CpuDataCollector::ReadCpuFreqTimeInState(
117 time_in_state_path_cpu1_, &cpu_freq_state_names, &freq_sample_cpu1);
118 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names);
119 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_sample_cpu1.time_in_state);
120 }
121
122 TEST_F(CpuDataCollectorTest, ReadCpuFreqAllTimeInState) {
123 std::vector<std::string> cpu_freq_state_names;
124 std::vector<CpuDataCollector::StateOccupancySample> freq_samples;
125 CpuDataCollector::StateOccupancySample freq_sample_cpu0;
126 CpuDataCollector::StateOccupancySample freq_sample_cpu1;
127 // |ReadCpuFreqAllTimeInState| only completes sample for cpu that is online.
128 freq_sample_cpu0.cpu_online = true;
129 freq_sample_cpu1.cpu_online = true;
130 freq_samples.push_back(freq_sample_cpu0);
131 freq_samples.push_back(freq_sample_cpu1);
132
133 CpuDataCollector::ReadCpuFreqAllTimeInState(
134 2, all_time_in_state_path_, &cpu_freq_state_names, &freq_samples);
135 EXPECT_EQ(kExpectedCpuFreqStateNames, cpu_freq_state_names);
136 EXPECT_EQ(kExpectedTimeInStateCpu0, freq_samples[0].time_in_state);
137 EXPECT_EQ(kExpectedTimeInStateCpu1, freq_samples[1].time_in_state);
138 }
139
140 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/power/cpu_data_collector.cc ('k') | chrome/browser/ui/webui/chromeos/power_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698