summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/test/browser_perf-recordings-io-04.js
blob: 2da84f4385940354f6746c4832919cd4f0042155 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/* eslint-disable */
/**
 * Tests if the performance tool can import profiler data from the
 * original profiler tool (Performance Recording v1, and Profiler data v2) and the correct views and graphs are loaded.
 */
var TICKS_DATA = (function () {
  let ticks = [];
  for (let i = 0; i < 100; i++) {
    ticks.push(i * 10);
  }
  return ticks;
})();

var PROFILER_DATA = (function () {
  let data = {};
  let threads = data.threads = [];
  let thread = {};
  threads.push(thread);
  thread.name = "Content";
  thread.samples = [{
    time: 5,
    frames: [
      { location: "(root)" },
      { location: "A" },
      { location: "B" },
      { location: "C" }
    ]
  }, {
    time: 5 + 6,
    frames: [
      { location: "(root)" },
      { location: "A" },
      { location: "B" },
      { location: "D" }
    ]
  }, {
    time: 5 + 6 + 7,
    frames: [
      { location: "(root)" },
      { location: "A" },
      { location: "E" },
      { location: "F" }
    ]
  }, {
    time: 20,
    frames: [
      { location: "(root)" },
      { location: "A" },
      { location: "B" },
      { location: "C" },
      { location: "D" },
      { location: "E" },
      { location: "F" },
      { location: "G" }
    ]
  }];

  // Handled in other deflating tests
  thread.markers = [];

  let meta = data.meta = {};
  meta.version = 2;
  meta.interval = 1;
  meta.stackwalk = 0;
  meta.product = "Firefox";
  return data;
})();

var test = Task.async(function* () {
  let { target, panel, toolbox } = yield initPerformance(SIMPLE_URL);
  let { $, EVENTS, PerformanceController, DetailsView, OverviewView, JsCallTreeView } = panel.panelWin;

  // Enable memory to test the memory-calltree and memory-flamegraph.
  Services.prefs.setBoolPref(ALLOCATIONS_PREF, true);

  // Create a structure from the data that mimics the old profiler's data.
  // Different name for `ticks`, different way of storing time,
  // and no memory, markers data.
  let oldProfilerData = {
    profilerData: { profile: PROFILER_DATA },
    ticksData: TICKS_DATA,
    recordingDuration: 10000,
    fileType: "Recorded Performance Data",
    version: 1
  };

  // Save recording as an old profiler data.
  let file = FileUtils.getFile("TmpD", ["tmpprofile.json"]);
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
  yield asyncCopy(oldProfilerData, file);

  // Import recording.

  let calltreeRendered = once(OverviewView, EVENTS.UI_FRAMERATE_GRAPH_RENDERED);
  let fpsRendered = once(JsCallTreeView, EVENTS.UI_JS_CALL_TREE_RENDERED);
  let imported = once(PerformanceController, EVENTS.RECORDING_IMPORTED);
  yield PerformanceController.importRecording("", file);

  yield imported;
  ok(true, "The original profiler data appears to have been successfully imported.");

  yield calltreeRendered;
  yield fpsRendered;
  ok(true, "The imported data was re-rendered.");

  // Ensure that only framerate and js calltree/flamegraph view are available
  is(isVisible($("#overview-pane")), true, "overview graph container still shown");
  is(isVisible($("#memory-overview")), false, "memory graph hidden");
  is(isVisible($("#markers-overview")), false, "markers overview graph hidden");
  is(isVisible($("#time-framerate")), true, "fps graph shown");
  is($("#select-waterfall-view").hidden, true, "waterfall button hidden");
  is($("#select-js-calltree-view").hidden, false, "jscalltree button shown");
  is($("#select-js-flamegraph-view").hidden, false, "jsflamegraph button shown");
  is($("#select-memory-calltree-view").hidden, true, "memorycalltree button hidden");
  is($("#select-memory-flamegraph-view").hidden, true, "memoryflamegraph button hidden");
  ok(DetailsView.isViewSelected(JsCallTreeView), "jscalltree view selected as its the only option");

  // Verify imported recording.

  let importedData = PerformanceController.getCurrentRecording().getAllData();
  let expected = Object.create({
    duration: 10000,
    markers: [].toSource(),
    frames: [].toSource(),
    memory: [].toSource(),
    ticks: TICKS_DATA.toSource(),
    profile: RecordingUtils.deflateProfile(JSON.parse(JSON.stringify(PROFILER_DATA))).toSource(),
    allocations: ({sites:[], timestamps:[], frames:[], sizes: []}).toSource(),
    withTicks: true,
    withMemory: false,
    sampleFrequency: void 0
  });

  for (let field in expected) {
    if (!!~["withTicks", "withMemory", "sampleFrequency"].indexOf(field)) {
      is(importedData.configuration[field], expected[field], `${field} successfully converted in legacy import.`);
    } else if (field === "profile") {
      is(importedData.profile.toSource(), expected.profile,
        "profiler data's samples successfully converted in legacy import.");
      is(importedData.profile.meta.version, 3, "Updated meta version to 3.");
    } else {
      let data = importedData[field];
      is(typeof data === "object" ? data.toSource() : data, expected[field],
        `${field} successfully converted in legacy import.`);
    }
  }

  yield teardown(panel);
  finish();
});

function getUnicodeConverter() {
  let className = "@mozilla.org/intl/scriptableunicodeconverter";
  let converter = Cc[className].createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = "UTF-8";
  return converter;
}

function asyncCopy(data, file) {
  let deferred = Promise.defer();

  let string = JSON.stringify(data);
  let inputStream = getUnicodeConverter().convertToInputStream(string);
  let outputStream = FileUtils.openSafeFileOutputStream(file);

  NetUtil.asyncCopy(inputStream, outputStream, status => {
    if (!Components.isSuccessCode(status)) {
      deferred.reject(new Error("Could not save data to file."));
    }
    deferred.resolve();
  });

  return deferred.promise;
}
/* eslint-enable */