summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/examples/api/messaging/timer/popup.js
blob: 8b70f59b9e112c517cf4c999b0345c28cfcf6854 (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
// Copyright (c) 2012 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.

if (!chrome.benchmarking) {
  alert("Warning:  Looks like you forgot to run chrome with " +
        " --enable-benchmarking set.");
  return;
}

function setChildTextNode(elementId, text) {
  document.getElementById(elementId).innerText = text;
}

// Tests the roundtrip time of sendRequest().
function testRequest() {
  setChildTextNode("resultsRequest", "running...");

  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var timer = new chrome.Interval();
    timer.start();
    var tab = tabs[0];
    chrome.tabs.sendRequest(tab.id, {counter: 1}, function handler(response) {
      if (response.counter < 1000) {
        chrome.tabs.sendRequest(tab.id, {counter: response.counter}, handler);
      } else {
        timer.stop();
        var usec = Math.round(timer.microseconds() / response.counter);
        setChildTextNode("resultsRequest", usec + "usec");
      }
    });
  });
}

// Tests the roundtrip time of Port.postMessage() after opening a channel.
function testConnect() {
  setChildTextNode("resultsConnect", "running...");

  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var timer = new chrome.Interval();
    timer.start();

    var port = chrome.tabs.connect(tabs[0].id);
    port.postMessage({counter: 1});
    port.onMessage.addListener(function getResp(response) {
      if (response.counter < 1000) {
        port.postMessage({counter: response.counter});
      } else {
        timer.stop();
        var usec = Math.round(timer.microseconds() / response.counter);
        setChildTextNode("resultsConnect", usec + "usec");
      }
    });
  });
}

document.addEventListener('DOMContentLoaded', function() {
  document.querySelector('#testRequest').addEventListener(
      'click', testRequest);
  document.querySelector('#testConnect').addEventListener(
      'click', testConnect);
});