summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/samples/benchmark/toolstrip.html
blob: 01e8dc4074ce1d80eee1982653ba1812453bd3fd (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<style>
#options {
  position: absolute;
  background-color: #FFFFCC;
  display: none;
  font-family: "Courier New";
  font-size: 9pt;
  padding: 5px;
  border: 1px solid #CCCC88;
  z-index: 3;
}
</style>

<script>
// Round a number to the 1's place.
function formatNumber(str) {
  str += '';
  if (str == '0') {
    return 'N/A ';
  }
  var x = str.split('.');
  var x1 = x[0];
  var x2 = x.length > 1 ? '.' + x[1] : '';
  var regex = /(\d+)(\d{3})/;
  while (regex.test(x1)) {
    x1 = x1.replace(regex, '$1' + ',' + '$2');
  }
  return x1;
}

// Configuration and results are stored globally.
window.iterations = 10;
window.clearConnections = true;
window.clearCache = true;
window.results = {};
window.results.data = new Array();
window.testUrl = "";
window.windowId = 0;

var optionsForm = 0;
function show_options() {
  // This functionality is a bit fragile, probably due to the extension API.
  // If we open the window from within the getSelected() callback, then the
  // window opens in a new window rather than a new tab (is that a bug?).
  // If we open the window before we call getSelected(), then the selected
  // tab URL will be NULL, because the page won't have loaded yet, but it will
  // be the selected tab.  So, we do this weird thing where we first call
  // get selected, knowing that it is asynchronous.  Then we open the window,
  // and finally when the callback occurs for getSelected, the window will
  // be created and we can use it.
  chrome.tabs.getSelected(windowId, function(tab) {
    window.testUrl = tab.url;
    optionsForm.setUrl(window.testUrl);
  });
  optionsForm = window.open("options.html", "optionswindow");
}

function Benchmark() {
  var runCount_ = 0;
  var count_;
  var totalTime_;
  var lastWin_;
  var me_ = this;
  var current_;
  var initialReadBytes_;
  var initialWriteBytes_;

  // Start a test run
  this.start = function(url) {
    // Check if a run is already in progress.
    if (me_.isRunning()) {
      return;
    }

    console.log("Starting test for url: " + url);

    runCount_ = window.iterations;
    count_ = 0;
    totalTime_ = 0;
    lastWin_ = 0;

    current_ = {};
    current_.url = url;
    current_.docLoadResults = new Array();  // times to docload
    current_.paintResults = new Array();    // times to paint
    current_.totalResults = new Array();    // times to complete load
    initialReadBytes = chrome.benchmarking.counter("tcp.read_bytes");
    initialWriteBytes = chrome.benchmarking.counter("tcp.write_bytes");

    me_.runPage();
  }

  // Is the benchmark currently in progress.
  this.isRunning = function() {
    return runCount_ > 0;
  }

  // Called when the test run completes.
  this.finish = function() {
    lastWin_.close();
    lastWin_ = 0;

    // Record some more stats.
    current_.bytesRead = chrome.benchmarking.counter("tcp.read_bytes") -
                          initialReadBytes;
    current_.bytesWritten = chrome.benchmarking.counter("tcp.write_bytes") -
                            initialWriteBytes;
    current_.totalTime = totalTime_;

    // push the result
    window.results.data.push(current_);
    current_ = 0;

    // show the latest
    show_options();
  }

  // Update the UI after a test run.
  this.displayResults = function() {
    var span = document.getElementById("result");
    var score = 0;
    if (count_ > 0) {
      score = totalTime_ / count_;
    }
    span.innerHTML = score.toFixed(1) + " (" + (runCount_) + ")";
  }

  // Run a single page in the benchmark
  this.runPage = function() {
    if (window.clearCache) {
      chrome.benchmarking.clearCache();
    }
    if (window.clearConnections) {
      chrome.benchmarking.closeConnections();
    }

    if (lastWin_) {
      lastWin_.location = current_.url;
    } else {
      lastWin_ = window.open(current_.url);
    }
  }

  // Called when a page finishes loading.
  this.pageFinished = function(csi) {
    var docLoadTime =
        Math.round((csi.finishDocumentLoadTime - csi.startLoadTime) * 1000.0);
    var paintTime =
        Math.round((csi.firstPaintTime - csi.startLoadTime) * 1000.0);
    var totalTime =
        Math.round((csi.finishLoadTime - csi.startLoadTime) * 1000.0);

    // Record the result
    current_.docLoadResults.push(docLoadTime);
    current_.paintResults.push(paintTime);
    current_.totalResults.push(totalTime);

    // For our toolbar counters
    totalTime_ += totalTime;
    count_++;

    if (--runCount_ > 0) {
      setTimeout(me_.runPage, 100);
    } else {
      me_.finish();
    }

    // Update the UI
    me_.displayResults();
  }
}

var benchmarks = new Array();

chrome.extension.onConnect.addListener(function(port) {
  port.onMessage.addListener(function(data) {
    if (data.message == "load") {
      var benchmark = benchmarks[data.url];
      if (benchmark != undefined && benchmark.isRunning()) {
        benchmark.pageFinished(data.values);
      }
    }
  });
});

function run() {
  show_options();
  var urls = testUrl.split(",");
  for (var i = 0; i < urls.length; i++) {
    var benchmark = new Benchmark();
    benchmarks[urls[i]] = benchmark;
    benchmark.start(urls[i]);  // XXXMB - move to constructor
  }
}

// Run at startup
chrome.windows.getCurrent(function(currentWindow) {
  window.windowId = currentWindow.id;
});
</script>

<style>
#result {
  color: green;
  text-align: center;
  vertical-align: center;
}
</style>

<div id="bench">
<img src="stopwatch.jpg" height="25" width="25" align=top onclick="show_options()">
<span id="result"></span>
</div>