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 spaceport.io's PerfMarks benchmark."""
6
7import logging
8import os
9
10from telemetry import benchmark
11from telemetry.core import util
12from telemetry.page import page_set
13from telemetry.page import page_test
14from telemetry.value import list_of_scalar_values
15from telemetry.value import scalar
16
17DESCRIPTIONS = {
18    'canvasDrawImageFullClear':
19        'Using a canvas element to render. Bitmaps are blitted to the canvas '
20        'using the "drawImage" function and the canvas is fully cleared at '
21        'the beginning of each frame.',
22    'canvasDrawImageFullClearAlign':
23        'Same as canvasDrawImageFullClear except all "x" and "y" values are '
24        'rounded to the nearest integer. This can be more efficient on '
25        'translate on certain browsers.',
26    'canvasDrawImagePartialClear':
27        'Using a canvas element to render. Bitmaps are blitted to the canvas '
28        'using the "drawImage" function and pixels drawn in the last frame '
29        'are cleared to the clear color at the beginning of each frame. '
30        'This is generally slower on hardware accelerated implementations, '
31        'but sometimes faster on CPU-based implementations.',
32    'canvasDrawImagePartialClearAlign':
33        'Same as canvasDrawImageFullClearAlign but only partially clearing '
34        'the canvas each frame.',
35    'css2dBackground':
36        'Using div elements that have a background image specified using CSS '
37        'styles. These div elements are translated, scaled, and rotated using '
38        'CSS-2D transforms.',
39    'css2dImg':
40        'Same as css2dBackground, but using img elements instead of div '
41        'elements.',
42    'css3dBackground':
43        'Same as css2dBackground, but using CSS-3D transforms.',
44    'css3dImg':
45        'Same as css2dImage but using CSS-3D tranforms.',
46}
47
48
49class _SpaceportMeasurement(page_test.PageTest):
50  def __init__(self):
51    super(_SpaceportMeasurement, self).__init__()
52
53  def CustomizeBrowserOptions(self, options):
54    options.AppendExtraBrowserArgs('--disable-gpu-vsync')
55
56  def ValidateAndMeasurePage(self, page, tab, results):
57    tab.WaitForJavaScriptExpression(
58        '!document.getElementById("start-performance-tests").disabled', 60)
59
60    tab.ExecuteJavaScript("""
61        window.__results = {};
62        window.console.log = function(str) {
63            if (!str) return;
64            var key_val = str.split(': ');
65            if (!key_val.length == 2) return;
66            __results[key_val[0]] = key_val[1];
67        };
68        document.getElementById('start-performance-tests').click();
69        """)
70
71    num_results = 0
72    num_tests_in_spaceport = 24
73    while num_results < num_tests_in_spaceport:
74      tab.WaitForJavaScriptExpression(
75          'Object.keys(window.__results).length > %d' % num_results, 180)
76      num_results = tab.EvaluateJavaScript(
77          'Object.keys(window.__results).length')
78      logging.info('Completed test %d of %d' %
79                   (num_results, num_tests_in_spaceport))
80
81    result_dict = eval(tab.EvaluateJavaScript(
82        'JSON.stringify(window.__results)'))
83    for key in result_dict:
84      chart, trace = key.split('.', 1)
85      results.AddValue(scalar.ScalarValue(
86          results.current_page, '%s.%s'% (chart, trace),
87          'objects (bigger is better)', float(result_dict[key]),
88          important=False, description=DESCRIPTIONS.get(chart)))
89    results.AddValue(list_of_scalar_values.ListOfScalarValues(
90        results.current_page, 'Score', 'objects (bigger is better)',
91        [float(x) for x in result_dict.values()],
92        description='Combined score for all parts of the spaceport benchmark.'))
93
94
95# crbug.com/166703: This test frequently times out on Windows.
96@benchmark.Disabled('mac', 'win')
97class Spaceport(benchmark.Benchmark):
98  """spaceport.io's PerfMarks benchmark."""
99  test = _SpaceportMeasurement
100
101  def CreatePageSet(self, options):
102    spaceport_dir = os.path.join(util.GetChromiumSrcDir(), 'chrome', 'test',
103        'data', 'third_party', 'spaceport')
104    ps = page_set.PageSet(file_path=spaceport_dir)
105    ps.AddPageWithDefaultRunNavigate('file://index.html')
106    return ps
107