| 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): |
| 11 super(CpuMetric, self).__init__() | 11 super(CpuMetric, self).__init__() |
| 12 self._results = None | 12 self._results = None |
| 13 self._browser = browser | 13 self._browser = browser |
| 14 self._start_cpu = None | 14 self._start_cpu = None |
| 15 | 15 |
| 16 def DidStartBrowser(self, browser): | 16 def DidStartBrowser(self, browser): |
| 17 # Save the browser object so that cpu_stats can be accessed later. | 17 # Save the browser object so that cpu_stats can be accessed later. |
| 18 self._browser = browser | 18 self._browser = browser |
| 19 | 19 |
| 20 def Start(self, page, tab): | 20 def Start(self, page, tab): |
| 21 self._start_cpu = self._browser.cpu_stats | 21 self._start_cpu = self._browser.cpu_stats |
| 22 | 22 |
| 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 = CpuMetric.SubtractCpuStats(self._browser.cpu_stats, | 25 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) |
| 26 self.start_cpu) | |
| 27 | 26 |
| 28 # Optional argument trace_name is not in base class Metric. | 27 # Optional argument trace_name is not in base class Metric. |
| 29 # pylint: disable=W0221 | 28 # pylint: disable=W0221 |
| 30 def AddResults(self, tab, results, trace_name='cpu_utilization'): | 29 def AddResults(self, tab, results, trace_name='cpu_utilization'): |
| 31 assert self._results, 'Must call Stop() first' | 30 assert self._results, 'Must call Stop() first' |
| 32 # Add a result for each process type. | 31 # Add a result for each process type. |
| 33 for process_type in self._results: | 32 for process_type in self._results: |
| 34 trace_name = '%s_%s' % (trace_name, process_type.lower()) | 33 trace_name = '%s_%s' % (trace_name, process_type.lower()) |
| 35 cpu_percent = 100 * self._results[process_type] | 34 cpu_percent = 100 * self._results[process_type] |
| 36 results.Add(trace_name, '%', cpu_percent, chart_name='cpu_utilization', | 35 results.Add(trace_name, '%', cpu_percent, chart_name='cpu_utilization', |
| (...skipping 23 matching lines...) Expand all Loading... |
| 60 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]): |
| 61 continue | 60 continue |
| 62 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - | 61 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - |
| 63 start_cpu_stats[process_type]['CpuProcessTime']) | 62 start_cpu_stats[process_type]['CpuProcessTime']) |
| 64 total_time = (cpu_stats[process_type]['TotalTime'] - | 63 total_time = (cpu_stats[process_type]['TotalTime'] - |
| 65 start_cpu_stats[process_type]['TotalTime']) | 64 start_cpu_stats[process_type]['TotalTime']) |
| 66 assert total_time > 0 | 65 assert total_time > 0 |
| 67 cpu_usage[process_type] = float(cpu_process_time) / total_time | 66 cpu_usage[process_type] = float(cpu_process_time) / total_time |
| 68 return cpu_usage | 67 return cpu_usage |
| 69 | 68 |
| OLD | NEW |