Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / perf / benchmarks / kraken.py
1 # Copyright 2012 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
5 """Runs Mozilla's Kraken JavaScript benchmark."""
6
7 import os
8
9 from metrics import power
10 from telemetry import benchmark
11 from telemetry.page import page_set
12 from telemetry.page import page_test
13 from telemetry.value import list_of_scalar_values
14 from telemetry.value import scalar
15
16 DESCRIPTIONS = {
17     'ai-astar':
18         'This benchmark uses the [A* search algorithm]'
19         '(http://en.wikipedia.org/wiki/A*_search_algorithm) to automatically '
20         'plot an efficient path between two points, in the presence of '
21         'obstacles. Adapted from code by [Brian Gringstead]'
22         '(http://www.briangrinstead.com/blog/astar-search-algorithm-in-'
23         'javascript).',
24     'audio-beat-detection':
25         'This benchmark performs [beat detection]'
26         '(http://en.wikipedia.org/wiki/Beat_detection) on an Audio sample '
27         'using [code](http://beatdetektor.svn.sourceforge.net/viewvc'
28         '/beatdetektor/trunk/core/js/beatdetektor.js?revision=18&view=markup) '
29         'from [BeatDetektor](http://www.cubicproductions.com/index.php'
30         '?option=com_content&view=article&id=67&Itemid=82) and '
31         '[DSP.js](http://github.com/corbanbrook/dsp.js/).',
32     'audio-dft':
33         'This benchmark performs a [Discrete Fourier Transform]'
34         '(http://en.wikipedia.org/wiki/Discrete_Fourier_transform) on an '
35         'Audio sample using code from [DSP.js]'
36         '(http://github.com/corbanbrook/dsp.js).',
37     'audio-fft':
38         'This benchmark performs a [Fast Fourier Transform]'
39         '(http://en.wikipedia.org/wiki/Fast_Fourier_transform) on an Audio '
40         'sample using code from [DSP.js]'
41         '(http://github.com/corbanbrook/dsp.js/).',
42     'audio-oscillator':
43         'This benchmark generates a soundwave using code from [DSP.js]'
44         '(http://github.com/corbanbrook/dsp.js/).',
45     'imaging-darkroom':
46         'This benchmark performs a variety of photo manipulations such as '
47         'Fill, Brightness, Contrast, Saturation, and Temperature.',
48     'imaging-desaturate':
49         'This benchmark [desaturates]'
50         '(http://en.wikipedia.org/wiki/Colorfulness) a photo using code from '
51         '[Pixastic](http://www.pixastic.com/).',
52     'imaging-gaussian-blur':
53         'This benchmark performs a [Gaussian blur]'
54         '(http://en.wikipedia.org/wiki/Gaussian_blur) on a photo.',
55     'json-parse-financial':
56         'This benchmark parses [JSON](http://www.json.org) records.',
57     'json-stringify-tinderbox':
58         'This benchmark serializes [Tinderbox]'
59         '(http://tests.themasta.com/tinderboxpushlog/?tree=Firefox) build '
60         'data to [JSON](http://www.json.org).',
61 }
62
63
64 def _Mean(l):
65   return float(sum(l)) / len(l) if len(l) > 0 else 0.0
66
67
68 class _KrakenMeasurement(page_test.PageTest):
69   def __init__(self):
70     super(_KrakenMeasurement, self).__init__()
71     self._power_metric = None
72
73   def CustomizeBrowserOptions(self, options):
74     power.PowerMetric.CustomizeBrowserOptions(options)
75
76   def WillStartBrowser(self, browser):
77     self._power_metric = power.PowerMetric(browser)
78
79   def DidNavigateToPage(self, page, tab):
80     self._power_metric.Start(page, tab)
81
82   def ValidateAndMeasurePage(self, page, tab, results):
83     tab.WaitForJavaScriptExpression(
84         'document.title.indexOf("Results") != -1', 700)
85     tab.WaitForDocumentReadyStateToBeComplete()
86
87     self._power_metric.Stop(page, tab)
88     self._power_metric.AddResults(tab, results)
89
90     js_get_results = """
91         var formElement = document.getElementsByTagName("input")[0];
92         decodeURIComponent(formElement.value.split("?")[1]);
93         """
94     result_dict = eval(tab.EvaluateJavaScript(js_get_results))
95     total = 0
96     for key in result_dict:
97       if key == 'v':
98         continue
99       results.AddValue(list_of_scalar_values.ListOfScalarValues(
100           results.current_page, key, 'ms', result_dict[key], important=False,
101           description=DESCRIPTIONS.get(key)))
102       total += _Mean(result_dict[key])
103
104     # TODO(tonyg/nednguyen): This measurement shouldn't calculate Total. The
105     # results system should do that for us.
106     results.AddValue(scalar.ScalarValue(
107         results.current_page, 'Total', 'ms', total,
108         description='Total of the means of the results for each type '
109                     'of benchmark in [Mozilla\'s Kraken JavaScript benchmark]'
110                     '(http://krakenbenchmark.mozilla.org/)'))
111
112
113 class Kraken(benchmark.Benchmark):
114   """Mozilla's Kraken JavaScript benchmark."""
115   test = _KrakenMeasurement
116
117   def CreatePageSet(self, options):
118     ps = page_set.PageSet(
119       archive_data_file='../page_sets/data/kraken.json',
120       file_path=os.path.abspath(__file__))
121     ps.AddPageWithDefaultRunNavigate(
122       'http://krakenbenchmark.mozilla.org/kraken-1.1/driver.html')
123     return ps