summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_perf-realtime-markers.js
blob: b9eab211cecfe8ee0af0eb31acb3e23d970283d7 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test functionality of real time markers.
 */

const { PerformanceFront } = require("devtools/shared/fronts/performance");

add_task(function* () {
  let browser = yield addTab(MAIN_DOMAIN + "doc_perf.html");
  let doc = browser.contentDocument;

  initDebuggerServer();
  let client = new DebuggerClient(DebuggerServer.connectPipe());
  let form = yield connectDebuggerClient(client);
  let front = PerformanceFront(client, form);
  yield front.connect();

  let lastMemoryDelta = 0;
  let lastTickDelta = 0;

  let counters = {
    markers: [],
    memory: [],
    ticks: []
  };

  let deferreds = {
    markers: defer(),
    memory: defer(),
    ticks: defer()
  };

  front.on("timeline-data", handler);

  let rec = yield front.startRecording({ withMarkers: true, withMemory: true, withTicks: true });
  yield Promise.all(Object.keys(deferreds).map(type => deferreds[type].promise));
  yield front.stopRecording(rec);
  front.off("timeline-data", handler);

  is(counters.markers.length, 1, "one marker event fired.");
  is(counters.memory.length, 3, "three memory events fired.");
  is(counters.ticks.length, 3, "three ticks events fired.");

  yield front.destroy();
  yield client.close();
  gBrowser.removeCurrentTab();

  function handler(name, data) {
    if (name === "markers") {
      if (counters.markers.length >= 1) { return; }
      ok(data.markers[0].start, "received atleast one marker with `start`");
      ok(data.markers[0].end, "received atleast one marker with `end`");
      ok(data.markers[0].name, "received atleast one marker with `name`");

      counters.markers.push(data.markers);
    }
    else if (name === "memory") {
      if (counters.memory.length >= 3) { return; }
      let { delta, measurement } = data;
      is(typeof delta, "number", "received `delta` in memory event");
      ok(delta > lastMemoryDelta, "received `delta` in memory event");
      ok(measurement.total, "received `total` in memory event");

      counters.memory.push({ delta, measurement });
      lastMemoryDelta = delta;
    }
    else if (name === "ticks") {
      if (counters.ticks.length >= 3) { return; }
      let { delta, timestamps } = data;
      ok(delta > lastTickDelta, "received `delta` in ticks event");

      // Timestamps aren't guaranteed to always contain tick events, since
      // they're dependent on the refresh driver, which may be blocked.

      counters.ticks.push({ delta, timestamps });
      lastTickDelta = delta;
    }
    else if (name === "frames") {
      // Nothing to do here.
    }
    else {
      ok(false, `Received unknown event: ${name}`);
    }

    if (name === "markers" && counters[name].length === 1 ||
        name === "memory" && counters[name].length === 3 ||
        name === "ticks" && counters[name].length === 3) {
      deferreds[name].resolve();
    }
  }
});