summaryrefslogtreecommitdiffstats
path: root/tools/perf/benchmarks/dromaeo.py
blob: 775c9fe8411405a0490af9038e5bc218534f6e68 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 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 math
import os

from metrics import power
from telemetry import test
from telemetry.page import page_measurement
from telemetry.page import page_set


class _DromaeoMeasurement(page_measurement.PageMeasurement):
  def __init__(self):
    super(_DromaeoMeasurement, self).__init__()
    self._power_metric = power.PowerMetric()

  def CustomizeBrowserOptions(self, options):
    power.PowerMetric.CustomizeBrowserOptions(options)

  def DidNavigateToPage(self, page, tab):
    self._power_metric.Start(page, tab)

  def MeasurePage(self, page, tab, results):
    tab.WaitForJavaScriptExpression(
        'window.document.getElementById("pause").value == "Run"', 60)

    # Start spying on POST request that will report benchmark results, and
    # intercept result data.
    tab.ExecuteJavaScript('(function() {' +
                          '  var real_jquery_ajax_ = window.jQuery;' +
                          '  window.results_ = "";' +
                          '  window.jQuery.ajax = function(request) {' +
                          '    if (request.url == "store.php") {' +
                          '      window.results_ =' +
                          '          decodeURIComponent(request.data);' +
                          '      window.results_ = window.results_.substring(' +
                          '          window.results_.indexOf("=") + 1, ' +
                          '          window.results_.lastIndexOf("&"));' +
                          '      real_jquery_ajax_(request);' +
                          '    }' +
                          '  };' +
                          '})();')
    # Starts benchmark.
    tab.ExecuteJavaScript('window.document.getElementById("pause").click();')

    tab.WaitForJavaScriptExpression('!!window.results_', 600)

    self._power_metric.Stop(page, tab)
    self._power_metric.AddResults(tab, results)

    score = eval(tab.EvaluateJavaScript('window.results_ || "[]"'))

    def Escape(k):
      chars = [' ', '.', '-', '/', '(', ')', '*']
      for c in chars:
        k = k.replace(c, '_')
      return k

    def AggregateData(container, key, value):
      if key not in container:
        container[key] = {'count': 0, 'sum': 0}
      container[key]['count'] += 1
      container[key]['sum'] += math.log(value)

    suffix = page.url[page.url.index('?') + 1 :]
    def AddResult(name, value):
      data_type = 'unimportant'
      if name == suffix:
        data_type = 'default'

      results.Add(Escape(name), 'run/s', value, data_type=data_type)

    aggregated = {}
    for data in score:
      AddResult('%s/%s' % (data['collection'], data['name']),
                data['mean'])

      escaped_top_name = data['collection'].split('-', 1)[0]
      AggregateData(aggregated, escaped_top_name, data['mean'])

      escaped_collection = data['collection']
      AggregateData(aggregated, escaped_collection, data['mean'])

    for key, value in aggregated.iteritems():
      AddResult(key, math.exp(value['sum'] / value['count']))

class _DromaeoBenchmark(test.Test):
  """A base class for Dromaeo benchmarks."""
  test = _DromaeoMeasurement

  def CreatePageSet(self, options):
    """Makes a PageSet for Dromaeo benchmarks."""
    # Subclasses are expected to define class members called query_param and
    # tag.
    if not hasattr(self, 'query_param') or not hasattr(self, 'tag'):
      raise NotImplementedError('query_param or tag not in Dromaeo benchmark.')
    archive_data_file = '../page_sets/data/dromaeo.%s.json' % self.tag
    ps = page_set.PageSet(
        make_javascript_deterministic=False,
        archive_data_file=archive_data_file,
        file_path=os.path.abspath(__file__))
    url = 'http://dromaeo.com?%s' % self.query_param
    ps.AddPageWithDefaultRunNavigate(url)
    return ps


class DromaeoDomCoreAttr(_DromaeoBenchmark):
  """Dromaeo DOMCore attr JavaScript benchmark."""
  tag = 'domcoreattr'
  query_param = 'dom-attr'


@test.Disabled('xp')  # crbug.com/323782
class DromaeoDomCoreModify(_DromaeoBenchmark):
  """Dromaeo DOMCore modify JavaScript benchmark."""
  tag = 'domcoremodify'
  query_param = 'dom-modify'


class DromaeoDomCoreQuery(_DromaeoBenchmark):
  """Dromaeo DOMCore query JavaScript benchmark."""
  tag = 'domcorequery'
  query_param = 'dom-query'


class DromaeoDomCoreTraverse(_DromaeoBenchmark):
  """Dromaeo DOMCore traverse JavaScript benchmark."""
  tag = 'domcoretraverse'
  query_param = 'dom-traverse'


class DromaeoJslibAttrJquery(_DromaeoBenchmark):
  """Dromaeo JSLib attr jquery JavaScript benchmark"""
  tag = 'jslibattrjquery'
  query_param = 'jslib-attr-jquery'


class DromaeoJslibAttrPrototype(_DromaeoBenchmark):
  """Dromaeo JSLib attr prototype JavaScript benchmark"""
  tag = 'jslibattrprototype'
  query_param = 'jslib-attr-prototype'


class DromaeoJslibEventJquery(_DromaeoBenchmark):
  """Dromaeo JSLib event jquery JavaScript benchmark"""
  tag = 'jslibeventjquery'
  query_param = 'jslib-event-jquery'


class DromaeoJslibEventPrototype(_DromaeoBenchmark):
  """Dromaeo JSLib event prototype JavaScript benchmark"""
  tag = 'jslibeventprototype'
  query_param = 'jslib-event-prototype'


class DromaeoJslibModifyJquery(_DromaeoBenchmark):
  """Dromaeo JSLib modify jquery JavaScript benchmark"""
  tag = 'jslibmodifyjquery'
  query_param = 'jslib-modify-jquery'


class DromaeoJslibModifyPrototype(_DromaeoBenchmark):
  """Dromaeo JSLib modify prototype JavaScript benchmark"""
  tag = 'jslibmodifyprototype'
  query_param = 'jslib-modify-prototype'


class DromaeoJslibStyleJquery(_DromaeoBenchmark):
  """Dromaeo JSLib style jquery JavaScript benchmark"""
  tag = 'jslibstylejquery'
  query_param = 'jslib-style-jquery'


class DromaeoJslibStylePrototype(_DromaeoBenchmark):
  """Dromaeo JSLib style prototype JavaScript benchmark"""
  tag = 'jslibstyleprototype'
  query_param = 'jslib-style-prototype'


class DromaeoJslibTraverseJquery(_DromaeoBenchmark):
  """Dromaeo JSLib traverse jquery JavaScript benchmark"""
  tag = 'jslibtraversejquery'
  query_param = 'jslib-traverse-jquery'


class DromaeoJslibTraversePrototype(_DromaeoBenchmark):
  """Dromaeo JSLib traverse prototype JavaScript benchmark"""
  tag = 'jslibtraverseprototype'
  query_param = 'jslib-traverse-prototype'


class DromaeoCSSQueryJquery(_DromaeoBenchmark):
  """Dromaeo CSS Query jquery JavaScript benchmark"""
  tag = 'cssqueryjquery'
  query_param = 'cssquery-jquery'