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 from metrics import Metric | 5 from metrics import Metric |
6 | 6 |
7 class CpuMetric(Metric): | 7 class CpuMetric(Metric): |
8 """Calulates CPU load over a span of time.""" | 8 """Calulates CPU load over a span of time.""" |
9 | 9 |
10 def __init__(self, browser): | 10 def __init__(self, browser): |
(...skipping 12 matching lines...) Expand all Loading... |
23 def Stop(self, page, tab): | 23 def Stop(self, page, tab): |
24 assert self._start_cpu, 'Must call Start() first' | 24 assert self._start_cpu, 'Must call Start() first' |
25 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) | 25 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) |
26 | 26 |
27 # Optional argument trace_name is not in base class Metric. | 27 # Optional argument trace_name is not in base class Metric. |
28 # pylint: disable=W0221 | 28 # pylint: disable=W0221 |
29 def AddResults(self, tab, results, trace_name='cpu_utilization'): | 29 def AddResults(self, tab, results, trace_name='cpu_utilization'): |
30 assert self._results, 'Must call Stop() first' | 30 assert self._results, 'Must call Stop() first' |
31 # Add a result for each process type. | 31 # Add a result for each process type. |
32 for process_type in self._results: | 32 for process_type in self._results: |
33 trace_name = '%s_%s' % (trace_name, process_type.lower()) | 33 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) |
34 cpu_percent = 100 * self._results[process_type] | 34 cpu_percent = 100 * self._results[process_type] |
35 results.Add(trace_name, '%', cpu_percent, chart_name='cpu_utilization', | 35 results.Add(trace_name_for_process, '%', cpu_percent, |
36 data_type='unimportant') | 36 chart_name='cpu_utilization', data_type='unimportant') |
37 | 37 |
38 | 38 |
39 def _SubtractCpuStats(cpu_stats, start_cpu_stats): | 39 def _SubtractCpuStats(cpu_stats, start_cpu_stats): |
40 """Computes average cpu usage over a time period for different process types. | 40 """Computes average cpu usage over a time period for different process types. |
41 | 41 |
42 Each of the two cpu_stats arguments is a dict with the following format: | 42 Each of the two cpu_stats arguments is a dict with the following format: |
43 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, | 43 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, |
44 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} | 44 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} |
45 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} | 45 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} |
46 | 46 |
(...skipping 12 matching lines...) Expand all Loading... |
59 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]): | 59 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]): |
60 continue | 60 continue |
61 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - | 61 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - |
62 start_cpu_stats[process_type]['CpuProcessTime']) | 62 start_cpu_stats[process_type]['CpuProcessTime']) |
63 total_time = (cpu_stats[process_type]['TotalTime'] - | 63 total_time = (cpu_stats[process_type]['TotalTime'] - |
64 start_cpu_stats[process_type]['TotalTime']) | 64 start_cpu_stats[process_type]['TotalTime']) |
65 assert total_time > 0, 'Expected total_time > 0, was: %d' % total_time | 65 assert total_time > 0, 'Expected total_time > 0, was: %d' % total_time |
66 cpu_usage[process_type] = float(cpu_process_time) / total_time | 66 cpu_usage[process_type] = float(cpu_process_time) / total_time |
67 return cpu_usage | 67 return cpu_usage |
68 | 68 |
OLD | NEW |