popup.js revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
5if (!chrome.benchmarking) {
6  alert("Warning:  Looks like you forgot to run chrome with " +
7        " --enable-benchmarking set.");
8  return;
9}
10
11function setChildTextNode(elementId, text) {
12  document.getElementById(elementId).innerText = text;
13}
14
15// Tests the roundtrip time of sendRequest().
16function testRequest() {
17  setChildTextNode("resultsRequest", "running...");
18
19  chrome.tabs.getSelected(null, function(tab) {
20    var timer = new chrome.Interval();
21    timer.start();
22
23    chrome.tabs.sendRequest(tab.id, {counter: 1}, function handler(response) {
24      if (response.counter < 1000) {
25        chrome.tabs.sendRequest(tab.id, {counter: response.counter}, handler);
26      } else {
27        timer.stop();
28        var usec = Math.round(timer.microseconds() / response.counter);
29        setChildTextNode("resultsRequest", usec + "usec");
30      }
31    });
32  });
33}
34
35// Tests the roundtrip time of Port.postMessage() after opening a channel.
36function testConnect() {
37  setChildTextNode("resultsConnect", "running...");
38
39  chrome.tabs.getSelected(null, function(tab) {
40    var timer = new chrome.Interval();
41    timer.start();
42
43    var port = chrome.tabs.connect(tab.id);
44    port.postMessage({counter: 1});
45    port.onMessage.addListener(function getResp(response) {
46      if (response.counter < 1000) {
47        port.postMessage({counter: response.counter});
48      } else {
49        timer.stop();
50        var usec = Math.round(timer.microseconds() / response.counter);
51        setChildTextNode("resultsConnect", usec + "usec");
52      }
53    });
54  });
55}
56
57document.addEventListener('DOMContentLoaded', function() {
58  document.querySelector('#testRequest').addEventListener(
59      'click', testRequest);
60  document.querySelector('#testConnect').addEventListener(
61      'click', testConnect);
62});