1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import sys
6
7from metrics import Metric
8from telemetry.value import scalar
9
10
11class IOMetric(Metric):
12  """IO-related metrics, obtained via telemetry.core.Browser."""
13
14  @classmethod
15  def CustomizeBrowserOptions(cls, options):
16    # TODO(tonyg): This is the host platform, so not totally correct.
17    if sys.platform not in ('darwin', 'win32'):
18      # TODO(playmobil): Get rid of this on all platforms crbug.com/361049.
19      options.AppendExtraBrowserArgs('--no-sandbox')
20
21  def Start(self, page, tab):
22    raise NotImplementedError()
23
24  def Stop(self, page, tab):
25    raise NotImplementedError()
26
27  def AddResults(self, tab, results):
28    # This metric currently only returns summary results, not per-page results.
29    raise NotImplementedError()
30
31  def AddSummaryResults(self, browser, results):
32    """Add summary results to the results object."""
33    io_stats = browser.io_stats
34    if not io_stats['Browser']:
35      return
36
37    def AddSummariesForProcessType(process_type_io, process_type_trace):
38      """For a given process type, add all relevant summary results.
39
40      Args:
41        process_type_io: Type of process (eg Browser or Renderer).
42        process_type_trace: String to be added to the trace name in the results.
43      """
44
45      def AddSummaryForOperation(operation_name, trace_name_prefix, units,
46                                 description):
47        """Adds summary results for an operation in a process.
48
49        Args:
50          operation_name: The name of the operation, e.g. 'ReadOperationCount'
51          trace_name_prefix: The prefix for the trace name.
52        """
53        if operation_name in io_stats[process_type_io]:
54          value = io_stats[process_type_io][operation_name]
55          if units == 'kb':
56            value = value / 1024
57          results.AddSummaryValue(
58              scalar.ScalarValue(None, trace_name_prefix + process_type_trace,
59                                 units, value, important=False,
60                                 description=description))
61
62      AddSummaryForOperation('ReadOperationCount', 'read_operations_', 'count',
63                             'Number of IO read operations.')
64      AddSummaryForOperation('WriteOperationCount', 'write_operations_',
65                             'count', 'Number of IO write operations.')
66      AddSummaryForOperation('ReadTransferCount', 'read_bytes_', 'kb',
67                             'Number of IO bytes read.')
68      AddSummaryForOperation('WriteTransferCount', 'write_bytes_', 'kb',
69                             'Number of IO bytes written.')
70
71    AddSummariesForProcessType('Browser', 'browser')
72    AddSummariesForProcessType('Renderer', 'renderer')
73    AddSummariesForProcessType('Gpu', 'gpu')
74