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 from telemetry.value import improvement_direction |
6 from telemetry.value import scalar | 7 from telemetry.value import scalar |
7 | 8 |
8 | 9 |
9 class CpuMetric(Metric): | 10 class CpuMetric(Metric): |
10 """Calulates CPU load over a span of time.""" | 11 """Calulates CPU load over a span of time.""" |
11 | 12 |
12 def __init__(self, browser): | 13 def __init__(self, browser): |
13 super(CpuMetric, self).__init__() | 14 super(CpuMetric, self).__init__() |
14 self._browser = browser | 15 self._browser = browser |
15 self._start_cpu = None | 16 self._start_cpu = None |
(...skipping 14 matching lines...) Expand all Loading... |
30 # pylint: disable=W0221 | 31 # pylint: disable=W0221 |
31 def AddResults(self, tab, results, trace_name='cpu_utilization'): | 32 def AddResults(self, tab, results, trace_name='cpu_utilization'): |
32 assert self._stop_cpu, 'Must call Stop() first' | 33 assert self._stop_cpu, 'Must call Stop() first' |
33 cpu_stats = _SubtractCpuStats(self._stop_cpu, self._start_cpu) | 34 cpu_stats = _SubtractCpuStats(self._stop_cpu, self._start_cpu) |
34 # Add a result for each process type. | 35 # Add a result for each process type. |
35 for process_type in cpu_stats: | 36 for process_type in cpu_stats: |
36 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) | 37 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) |
37 cpu_percent = 100 * cpu_stats[process_type] | 38 cpu_percent = 100 * cpu_stats[process_type] |
38 results.AddValue(scalar.ScalarValue( | 39 results.AddValue(scalar.ScalarValue( |
39 results.current_page, 'cpu_utilization.%s' % trace_name_for_process, | 40 results.current_page, 'cpu_utilization.%s' % trace_name_for_process, |
40 '%', cpu_percent, important=False)) | 41 '%', cpu_percent, important=False, |
| 42 improvement_direction=improvement_direction.DOWN)) |
41 | 43 |
42 | 44 |
43 def _SubtractCpuStats(cpu_stats, start_cpu_stats): | 45 def _SubtractCpuStats(cpu_stats, start_cpu_stats): |
44 """Computes average cpu usage over a time period for different process types. | 46 """Computes average cpu usage over a time period for different process types. |
45 | 47 |
46 Each of the two cpu_stats arguments is a dict with the following format: | 48 Each of the two cpu_stats arguments is a dict with the following format: |
47 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, | 49 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, |
48 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} | 50 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} |
49 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} | 51 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} |
50 | 52 |
(...skipping 19 matching lines...) Expand all Loading... |
70 # Linux kernel starts with a value close to an overflow, so correction is | 72 # Linux kernel starts with a value close to an overflow, so correction is |
71 # necessary. | 73 # necessary. |
72 if total_time < 0: | 74 if total_time < 0: |
73 total_time += 2**32 | 75 total_time += 2**32 |
74 # Assert that the arguments were given in the correct order. | 76 # Assert that the arguments were given in the correct order. |
75 assert total_time > 0 and total_time < 2**31, ( | 77 assert total_time > 0 and total_time < 2**31, ( |
76 'Expected total_time > 0, was: %d' % total_time) | 78 'Expected total_time > 0, was: %d' % total_time) |
77 cpu_usage[process_type] = float(cpu_process_time) / total_time | 79 cpu_usage[process_type] = float(cpu_process_time) / total_time |
78 return cpu_usage | 80 return cpu_usage |
79 | 81 |
OLD | NEW |