summaryrefslogtreecommitdiffstats
path: root/tools/playback_benchmark/playback_driver.py
blob: ef6c7b4e4ef2ca05de29c1bdec8986287fc90adb (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
# Copyright (c) 2011 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.
"""Playback driver."""

import cgi
import simplejson as json
import os
import string
import sys
import threading
import urlparse

START_PAGE = """<html>
  <script type="text/javascript">
var runCount = $run_count;
var results = [];

function run() {
  var wnd = window.open('?resource=start_page_popup', '',
                        'width=$width, height=$height');
  var timerId = setInterval(function() {
    wnd.postMessage('ping', '$target_origin');
  }, 300);
  var handleMessage = function(event) {
    clearInterval(timerId);
    wnd.close();
    document.writeln('<div>' + event.data + '</div>');
    results.push(event.data);
    runCount -= 1;
    window.removeEventListener('message', handleMessage);
    if (runCount > 0) {
      run();
    } else {
      var xmlHttpRequest = new XMLHttpRequest();
      xmlHttpRequest.open("POST", '/benchmark/', true);
      xmlHttpRequest.setRequestHeader("Content-type", "application/json");
      xmlHttpRequest.send(JSON.stringify({results: results}));
    }
  }
  window.addEventListener('message', handleMessage, false);
}

run();
  </script>
</html>
"""

START_PAGE_POPUP = """<html>
  <script type="text/javascript">
window.setTimeout(function() {
  console.log(window.innerWidth, window.innerHeight);
  if (window.innerWidth == $width && window.innerHeight == $height) {
    window.location = '$start_url';
  } else {
    window.resizeBy($width - window.innerWidth, $height - window.innerHeight);
    window.location = window.location;
  }
}, 200);
  </script>
</html>
"""

DATA_JS = 'Benchmark.data = $data;'


def ReadFile(file_name, mode='r'):
  f = open(file_name, mode)
  data = f.read()
  f.close()
  return data


def ReadJSON(file_name):
  f = open(file_name, 'r')
  data = json.load(f)
  f.close()
  return data


class PlaybackRequestHandler(object):
  """This class is used to process HTTP requests during test playback.

  Attributes:
    test_dir: directory containing test files.
    test_callback: function to be called when the test is finished.
    script_dir: directory where javascript files are located.
  """

  def __init__(self, test_dir, test_callback=None, script_dir=os.getcwd()):
    self.test_dir = test_dir
    self.test_callback = test_callback
    self.script_dir = script_dir

  def ProcessRequest(self, handler):
    "Processes single HTTP request."

    parse_result = urlparse.urlparse(handler.path)
    if parse_result.path.endswith('/benchmark/'):
      query = cgi.parse_qs(parse_result.query)
      if 'run_test' in query:
        run_count = 1
        if 'run_count' in query:
          run_count = query['run_count'][0]
        self._StartTest(handler, self.test_dir, run_count)
      elif 'resource' in query:
        self._GetBenchmarkResource(query['resource'][0], handler)
      else:
        self._ProcessBenchmarkReport(handler.body, handler)
    else:
      self._GetApplicationResource(handler)

  def _StartTest(self, handler, test_dir, run_count):
    "Sends test start page to browser."

    cache_data = ReadJSON(os.path.join(test_dir, 'cache.json'))

    # Load cached responses.
    self.cache = {}
    responses_dir = os.path.join(test_dir, 'responses')
    for request in cache_data['requests']:
      response_file = os.path.join(responses_dir, request['response_file'])
      response = ReadFile(response_file, 'rb')
      key = (request['method'], request['path'])
      self.cache[key] = {'response': response, 'headers': request['headers']}

    # Load benchmark scripts.
    self.benchmark_resources = {}
    data = ReadFile(os.path.join(test_dir, 'data.json'))
    data = string.Template(DATA_JS).substitute(data=data)
    self.benchmark_resources['data.js'] = {'data': data,
                                           'type': 'application/javascript'}
    for resource in ('common.js', 'playback.js'):
      resource_file = os.path.join(self.script_dir, resource)
      self.benchmark_resources[resource] = {'data': ReadFile(resource_file),
                                            'type': 'application/javascript'}

    # Format start page.
    parse_result = urlparse.urlparse(cache_data['start_url'])
    target_origin = '%s://%s' % (parse_result.scheme, parse_result.netloc)
    start_page = string.Template(START_PAGE).substitute(
        run_count=run_count, target_origin=target_origin,
        width=cache_data['width'], height=cache_data['height'])
    self.benchmark_resources['start_page'] = {
      'data': start_page,
      'type': 'text/html; charset=UTF-8'
    }

    start_page_popup = string.Template(START_PAGE_POPUP).substitute(
        start_url=cache_data['start_url'],
        width=cache_data['width'], height=cache_data['height'])
    self.benchmark_resources['start_page_popup'] = {
      'data': start_page_popup,
      'type': 'text/html; charset=UTF-8'
    }

    self._GetBenchmarkResource('start_page', handler)

  def _GetBenchmarkResource(self, resource, handler):
    "Sends requested resource to browser."

    if resource in self.benchmark_resources:
      resource = self.benchmark_resources[resource]
      handler.send_response(200)
      handler.send_header('content-length', len(resource['data']))
      handler.send_header('content-type', resource['type'])
      handler.end_headers()
      handler.wfile.write(resource['data'])
    else:
      handler.send_response(404)
      handler.end_headers()

  def _ProcessBenchmarkReport(self, content, handler):
    "Reads benchmark score from report content and invokes callback."

    handler.send_response(204)
    handler.end_headers()
    content = json.loads(content)
    if 'results' in content:
      results = content['results']
      sys.stdout.write('Results: %s\n' % results)
      if self.test_callback: self.test_callback(results)
    elif 'error' in content:
      sys.stderr.write('Error: %s\n' % content['error'])

  def _GetApplicationResource(self, handler):
    "Searches for response in cache. If not found, responds with 204."
    key = (handler.command, handler.path)
    if key in self.cache:
      sys.stdout.write('%s %s -> found\n' % key)
      handler.wfile.write(self.cache[key]['response'])
    else:
      sys.stderr.write('%s %s -> not found\n' % key)
      handler.send_response(204, "not in cache")
      handler.end_headers()