| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 | 6 |
| 7 from metrics import cpu | 7 from metrics import cpu |
| 8 | 8 |
| 9 # Testing private method. | 9 # Testing private method. |
| 10 # pylint: disable=W0212 | 10 # pylint: disable=protected-access |
| 11 class CpuMetricTest(unittest.TestCase): | 11 class CpuMetricTest(unittest.TestCase): |
| 12 def testSubtractCpuStats(self): | 12 def testSubtractCpuStats(self): |
| 13 # The result computed is a ratio of cpu time used to time elapsed. | 13 # The result computed is a ratio of cpu time used to time elapsed. |
| 14 start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 0}} | 14 start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 0}} |
| 15 end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}} | 15 end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}} |
| 16 self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start)) | 16 self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start)) |
| 17 | 17 |
| 18 # An error is thrown if the args are called in the wrong order. | 18 # An error is thrown if the args are called in the wrong order. |
| 19 self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end) | 19 self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end) |
| 20 | 20 |
| 21 # An error is thrown if there's a process type in end that's not in start. | 21 # An error is thrown if there's a process type in end that's not in start. |
| 22 end['Renderer'] = {'CpuProcessTime': 2, 'TotalTime': 20} | 22 end['Renderer'] = {'CpuProcessTime': 2, 'TotalTime': 20} |
| 23 self.assertRaises(AssertionError, cpu._SubtractCpuStats, end, start) | 23 self.assertRaises(AssertionError, cpu._SubtractCpuStats, end, start) |
| 24 | 24 |
| 25 # A process type will be ignored if there's an empty dict for start or end. | 25 # A process type will be ignored if there's an empty dict for start or end. |
| 26 start['Renderer'] = {} | 26 start['Renderer'] = {} |
| 27 self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start)) | 27 self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start)) |
| 28 | 28 |
| 29 # Results for multiple process types can be computed. | 29 # Results for multiple process types can be computed. |
| 30 start['Renderer'] = {'CpuProcessTime': 0, 'TotalTime': 0} | 30 start['Renderer'] = {'CpuProcessTime': 0, 'TotalTime': 0} |
| 31 self.assertEqual({'Browser': 0.25, 'Renderer': 0.1}, | 31 self.assertEqual({'Browser': 0.25, 'Renderer': 0.1}, |
| 32 cpu._SubtractCpuStats(end, start)) | 32 cpu._SubtractCpuStats(end, start)) |
| 33 | 33 |
| 34 # Test 32-bit overflow. | 34 # Test 32-bit overflow. |
| 35 start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 2**32 - 20}} | 35 start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 2**32 - 20}} |
| 36 end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}} | 36 end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}} |
| 37 self.assertEqual({'Browser': 0.125}, cpu._SubtractCpuStats(end, start)) | 37 self.assertEqual({'Browser': 0.125}, cpu._SubtractCpuStats(end, start)) |
| 38 self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end) | 38 self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end) |
| 39 | 39 |
| OLD | NEW |