1# Copyright 2014 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
5import unittest
6
7from telemetry.core.platform.power_monitor import sysfs_power_monitor
8
9
10class SysfsPowerMonitorMonitorTest(unittest.TestCase):
11  initial_freq = {
12    'cpu0': '1700000 6227\n1600000 0\n1500000 0\n1400000 28\n1300000 22\n'
13            '1200000 14\n1100000 19\n1000000 22\n900000 14\n800000 20\n'
14            '700000 15\n600000 23\n500000 23\n400000 9\n300000 28\n200000 179',
15    'cpu1': '1700000 11491\n1600000 0\n1500000 0\n1400000 248\n1300000 1166\n'
16            '1200000 2082\n1100000 2943\n1000000 6560\n900000 12517\n'
17            '800000 8690\n700000 5105\n600000 3800\n500000 5131\n400000 5479\n'
18            '300000 7571\n200000 133618',
19    'cpu2': '1700000 1131',
20    'cpu3': '1700000 1131'
21  }
22  final_freq = {
23    'cpu0': '1700000 7159\n1600000 0\n1500000 0\n1400000 68\n1300000 134\n'
24            '1200000 194\n1100000 296\n1000000 716\n900000 1301\n800000 851\n'
25            '700000 554\n600000 343\n500000 612\n400000 691\n300000 855\n'
26            '200000 15525',
27    'cpu1': '1700000 12048\n1600000 0\n1500000 0\n1400000 280\n1300000 1267\n'
28            '1200000 2272\n1100000 3163\n1000000 7039\n900000 13800\n'
29            '800000 9599\n700000 5655\n600000 4144\n500000 5655\n400000 6005\n'
30            '300000 8288\n200000 149724',
31    'cpu2': None,
32    'cpu3': ''
33  }
34  expected_initial_freq = {
35    'cpu0': {
36      1700000000: 6227,
37      1600000000: 0,
38      1500000000: 0,
39      1400000000: 28,
40      1300000000: 22,
41      1200000000: 14,
42      1100000000: 19,
43      1000000000: 22,
44      900000000: 14,
45      800000000: 20,
46      700000000: 15,
47      600000000: 23,
48      500000000: 23,
49      400000000: 9,
50      300000000: 28,
51      200000000: 179
52    },
53    'cpu1': {
54      1700000000: 11491,
55      1600000000: 0,
56      1500000000: 0,
57      1400000000: 248,
58      1300000000: 1166,
59      1200000000: 2082,
60      1100000000: 2943,
61      1000000000: 6560,
62      900000000: 12517,
63      800000000: 8690,
64      700000000: 5105,
65      600000000: 3800,
66      500000000: 5131,
67      400000000: 5479,
68      300000000: 7571,
69      200000000: 133618
70    },
71    'cpu2': {
72      1700000000: 1131
73    },
74    'cpu3': {
75      1700000000: 1131
76    }
77  }
78  expected_final_freq = {
79    'cpu0': {
80      1700000000: 7159,
81      1600000000: 0,
82      1500000000: 0,
83      1400000000: 68,
84      1300000000: 134,
85      1200000000: 194,
86      1100000000: 296,
87      1000000000: 716,
88      900000000: 1301,
89      800000000: 851,
90      700000000: 554,
91      600000000: 343,
92      500000000: 612,
93      400000000: 691,
94      300000000: 855,
95      200000000: 15525
96    },
97    'cpu1': {
98      1700000000: 12048,
99      1600000000: 0,
100      1500000000: 0,
101      1400000000: 280,
102      1300000000: 1267,
103      1200000000: 2272,
104      1100000000: 3163,
105      1000000000: 7039,
106      900000000: 13800,
107      800000000: 9599,
108      700000000: 5655,
109      600000000: 4144,
110      500000000: 5655,
111      400000000: 6005,
112      300000000: 8288,
113      200000000: 149724
114    },
115    'cpu2': None,
116    'cpu3': {}
117  }
118  expected_freq_percents = {
119    'whole_package': {
120      1700000000: 3.29254111574526,
121      1600000000: 0.0,
122      1500000000: 0.0,
123      1400000000: 0.15926805099535601,
124      1300000000: 0.47124116307273645,
125      1200000000: 0.818756100807525,
126      1100000000: 1.099381692400982,
127      1000000000: 2.5942528544384302,
128      900000000: 5.68661122326737,
129      800000000: 3.850545467654628,
130      700000000: 2.409691872245393,
131      600000000: 1.4693702487650486,
132      500000000: 2.4623575553879373,
133      400000000: 2.672038150383057,
134      300000000: 3.415770495015825,
135      200000000: 69.59817400982045
136    },
137    'cpu0': {
138      1700000000: 4.113700564971752,
139      1600000000: 0.0,
140      1500000000: 0.0,
141      1400000000: 0.1765536723163842,
142      1300000000: 0.4943502824858757,
143      1200000000: 0.7944915254237288,
144      1100000000: 1.2226341807909604,
145      1000000000: 3.0632062146892656,
146      900000000: 5.680614406779661,
147      800000000: 3.6679025423728815,
148      700000000: 2.379060734463277,
149      600000000: 1.4124293785310735,
150      500000000: 2.599752824858757,
151      400000000: 3.0102401129943503,
152      300000000: 3.650247175141243,
153      200000000: 67.73481638418079
154    },
155    'cpu1': {
156      1700000000: 2.4713816665187682,
157      1600000000: 0.0,
158      1500000000: 0.0,
159      1400000000: 0.1419824296743278,
160      1300000000: 0.44813204365959713,
161      1200000000: 0.8430206761913214,
162      1100000000: 0.9761292040110037,
163      1000000000: 2.1252994941875945,
164      900000000: 5.69260803975508,
165      800000000: 4.033188392936374,
166      700000000: 2.4403230100275093,
167      600000000: 1.526311118999024,
168      500000000: 2.3249622859171177,
169      400000000: 2.3338361877717633,
170      300000000: 3.1812938148904073,
171      200000000: 71.46153163546012
172    },
173    'cpu2': {
174      1700000000: 0.0,
175    },
176    'cpu3': {
177      1700000000: 0.0,
178   }
179  }
180
181  def testParseCpuFreq(self):
182    initial = sysfs_power_monitor.SysfsPowerMonitor.ParseFreqSample(
183        self.initial_freq)
184    final = sysfs_power_monitor.SysfsPowerMonitor.ParseFreqSample(
185        self.final_freq)
186    self.assertDictEqual(initial, self.expected_initial_freq)
187    self.assertDictEqual(final, self.expected_final_freq)
188
189  def testComputeCpuStats(self):
190    results = sysfs_power_monitor.SysfsPowerMonitor.ComputeCpuStats(
191        self.expected_initial_freq, self.expected_final_freq)
192    for cpu in self.expected_freq_percents:
193      for freq in results[cpu]:
194        self.assertAlmostEqual(results[cpu][freq],
195                               self.expected_freq_percents[cpu][freq])
196