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
3# found in the LICENSE file.
4
5"""Runs Canvasmark HTML5, Canvas 2D rendering and javascript benchmark.
6
7CanvasMark tests the HTML5 <canvas> rendering performance for commonly used
8operations in HTML5 games: bitmaps, canvas drawing, alpha blending, polygon
9fills, shadows and text functions.
10"""
11
12import os
13
14from telemetry import benchmark
15from telemetry.page import page_set
16from telemetry.page import page_test
17from telemetry.value import scalar
18
19
20class _CanvasMarkMeasurement(page_test.PageTest):
21
22  def WillNavigateToPage(self, page, tab):
23    page.script_to_evaluate_on_commit = """
24        var __results = [];
25        var __real_log = window.console.log;
26        window.console.log = function(msg) {
27          __results.push(msg);
28          __real_log.apply(this, [msg]);
29        }
30        """
31
32  def ValidateAndMeasurePage(self, _, tab, results):
33    tab.WaitForJavaScriptExpression('__results.length == 8', 300)
34    results_log = tab.EvaluateJavaScript('__results')
35    total = 0
36    for output in results_log:
37      # Split the results into score and test name.
38      # results log e.g., "489 [Test 1 - Asteroids - Bitmaps]"
39      score_and_name = output.split(' [', 2)
40      assert len(score_and_name) == 2, \
41        'Unexpected result format "%s"' % score_and_name
42      score = int(score_and_name[0])
43      name = score_and_name[1][:-1]
44      results.AddValue(scalar.ScalarValue(
45          results.current_page, name, 'score', score, important=False))
46      # Aggregate total score for all tests.
47      total += score
48    results.AddValue(scalar.ScalarValue(
49        results.current_page, 'Score', 'score', total))
50
51
52@benchmark.Disabled
53class CanvasMark(benchmark.Benchmark):
54  test = _CanvasMarkMeasurement
55
56  def CreatePageSet(self, options):
57    ps = page_set.PageSet(
58      file_path=os.path.abspath(__file__),
59      archive_data_file='../page_sets/data/canvasmark.json',
60      make_javascript_deterministic=False)
61    ps.AddPageWithDefaultRunNavigate(
62      'http://www.kevs3d.co.uk/dev/canvasmark/?auto=true')
63    return ps
64