summaryrefslogtreecommitdiffstats
path: root/tools/perf/metrics/cpu_unittest.py
blob: 6c7f893d0a18a6582a129bc1f18d4072ca3ca1f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from metrics import cpu

# Testing private method.
# pylint: disable=W0212
class CpuMetricTest(unittest.TestCase):
  def testSubtractCpuStats(self):
    # The result computed is a ratio of cpu time used to time elapsed.
    start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 0}}
    end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}}
    self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start))

    # An error is thrown if the args are called in the wrong order.
    self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end)

    # An error is thrown if there's a process type in end that's not in start.
    end['Renderer'] = {'CpuProcessTime': 2, 'TotalTime': 20}
    self.assertRaises(AssertionError, cpu._SubtractCpuStats, end, start)

    # A process type will be ignored if there's an empty dict for start or end.
    start['Renderer'] = {}
    self.assertEqual({'Browser': 0.25}, cpu._SubtractCpuStats(end, start))

    # Results for multiple process types can be computed.
    start['Renderer'] = {'CpuProcessTime': 0, 'TotalTime': 0}
    self.assertEqual({'Browser': 0.25, 'Renderer': 0.1},
                     cpu._SubtractCpuStats(end, start))

    # Test 32-bit overflow.
    start = {'Browser': {'CpuProcessTime': 0, 'TotalTime': 2**32 - 20}}
    end = {'Browser': {'CpuProcessTime': 5, 'TotalTime': 20}}
    self.assertEqual({'Browser': 0.125}, cpu._SubtractCpuStats(end, start))
    self.assertRaises(AssertionError, cpu._SubtractCpuStats, start, end)