1// Copyright (c) 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
5var totalTime;
6var fudgeTime;
7var elapsedTime;
8var endTime;
9var iterations;
10var cycle;
11var results = false;
12var TIMEOUT = 15;
13
14/**
15 * Returns the value of the given property stored in the cookie.
16 * @param {string} name The property name.
17 * @return {string} The value of the given property, or empty string
18 *     if the property was not found.
19 */
20function __get_cookie(name) {
21  var cookies = document.cookie.split('; ');
22  for (var i = 0; i < cookies.length; ++i) {
23    var t = cookies[i].split('=');
24    if ((t[0] == name) && t[1])
25      return t[1];
26  }
27  return '';
28}
29
30function __get_timings() {
31  if (sessionStorage == null)
32    return __get_cookie("__pc_timings");
33  else {
34    if (sessionStorage.getItem("__pc_timings") == null)
35      return "";
36    else
37      return sessionStorage["__pc_timings"];
38  }
39}
40function __set_timings(timings) {
41  if (sessionStorage == null)
42    document.cookie = "__pc_timings=" + timings + "; path=/";
43  else
44    sessionStorage["__pc_timings"]=timings;
45}
46
47/**
48 * Starts the next test cycle or redirects the browser to the results page.
49 */
50function nextCycleOrResults() {
51  // Call GC twice to cleanup JS heap before starting a new test.
52  if (window.gc) {
53    window.gc();
54    window.gc();
55  }
56
57  var timings = elapsedTime;
58  var oldTimings = __get_timings();
59  if (oldTimings != '')
60    timings = oldTimings + ',' + timings;
61  __set_timings(timings);
62
63  var tLag = Date.now() - endTime - TIMEOUT;
64  if (tLag > 0)
65    fudgeTime += tLag;
66
67  var doc;
68  if (cycle == iterations) {
69    document.cookie = '__pc_done=1; path=/';
70    doc = '../../common/report.html';
71    if (window.console) {
72      console.log("Pages: [" + __get_cookie('__pc_pages') + "]");
73      console.log("times: [" + __get_timings() + "]");
74    }
75  } else {
76    doc = 'index.html';
77  }
78
79  var url = doc + '?n=' + iterations + '&i=' + cycle +
80      '&td=' + totalTime + '&tf=' + fudgeTime;
81  document.location.href = url;
82}
83
84/**
85 * Computes various running times and updates the stats reported at the end.
86 * @param {!number} cycleTime The running time of the test cycle.
87 */
88function testComplete(cycleTime) {
89  if (results)
90    return;
91
92  var oldTotalTime = 0;
93  var cycleEndTime = Date.now();
94  var cycleFudgeTime = 0;
95
96  var s = document.location.search;
97  if (s) {
98    var params = s.substring(1).split('&');
99    for (var i = 0; i < params.length; i++) {
100      var f = params[i].split('=');
101      switch (f[0]) {
102        case 'skip':
103          return;  // No calculation, just viewing
104        case 'n':
105          iterations = f[1];
106          break;
107        case 'i':
108          cycle = f[1] - 0 + 1;
109          break;
110        case 'td':
111          oldTotalTime = f[1] - 0;
112          break;
113        case 'tf':
114          cycleFudgeTime = f[1] - 0;
115          break;
116      }
117    }
118  }
119  elapsedTime = cycleTime;
120  totalTime = oldTotalTime + elapsedTime;
121  endTime = cycleEndTime;
122  fudgeTime = cycleFudgeTime;
123
124  setTimeout(nextCycleOrResults, TIMEOUT);
125}
126