-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
388 lines (363 loc) · 13 KB
/
index.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>USB power profiling</title>
<style>
.profile {
border: 1px solid black;
padding-top: 1px
}
.profile > div {
position: relative;
padding-bottom: 5%
}
.profile > div > svg {
position: absolute;
}
path {
fill: #73737388;
stroke: #737373;
stroke-width: 15px;
stroke-linejoin: bevel;
}
/* stock-width = 3px * (viewBox width = 2400px) / min-width */
@media (min-width: 600px) {
path {
stroke-width: 12px;
}
}
@media (min-width: 720px) {
path {
stroke-width: 10px;
}
}
@media (min-width: 900px) {
path {
stroke-width: 8px;
}
}
@media (min-width: 1020px) {
path {
stroke-width: 7px;
}
}
@media (min-width: 1200px) {
path {
stroke-width: 6px;
}
}
@media (min-width: 1440px) {
path {
stroke-width: 5px;
}
}
@media (min-width: 1800px) {
path {
stroke-width: 4px;
}
}
@media (min-width: 2400px) {
path {
stroke-width: 3px;
}
}
@media (min-width: 3400px) {
path {
stroke-width: 2px;
}
}
.profile > p {
text-align: center;
background-color: #73737322;
margin-top: 0;
border-top: 1px solid #737373;
}
footer{
text-align: center;
font-size:smaller;
}
footer img {
height:22px !important;
margin-left: 3px;
vertical-align: text-bottom;
}
</style>
</head>
<body>
<h1 id="power"><span id="deviceNameH1">USB</span> live profile: <span id="lastPower"></span>W</h1>
<p>usb-power-profiling base URL: <code>http://<input type=text id="hostname" value="localhost:2121">/</code></p>
<div class="profile">
<div>
<svg viewBox="0 0 2400 120">
<path d=""/>
</svg>
</div>
<p><span id="deviceNameProfile">USB</span>, <span id="sampleCount">0</span> samples.</p>
<ul>
<li>Energy used: <span id="totalEnergy"></span>Wh in <span id="totalTime"></span></li>
<li>Average power: <span id="averagePower"></span>W</li>
<li>Max power: <span id="maxPower"></span>W</li>
<li>Median power: <span id="medianPower"></span>W</li>
</ul>
</div>
<p><a id="open" href="#">Open in the Firefox Profiler</a>. Download as: <a id="csv" href="#">csv</a>, <a id="profile" href="#">profile</a>.</p>
<script type="application/javascript">
let sampleData = [];
let lastSampleTime = 0;
let deviceName = "USB";
function setText(id, text) {
document.getElementById(id).textContent = text;
}
function formatDuration(timeS) {
let result = "";
if (timeS > 60) {
result = Math.round(timeS % 60) + "s";
let timeMin = Math.floor(timeS / 60);
if (timeMin > 60) {
result = Math.floor(timeMin / 60) + "h" + (timeMin % 60) + "min";
} else {
result = timeMin + "min" + result;
}
} else {
result = Math.round(timeS) + "s";
}
return result;
}
function setDeviceName(name) {
deviceName = name;
document.title = `${deviceName} power profiling`;
setText("deviceNameProfile", deviceName);
setText("deviceNameH1", deviceName);
}
async function fetchSamples() {
let url = `http://${document.getElementById("hostname").value}/rawdata`;
if (lastSampleTime > 0) {
url += "?last=" + lastSampleTime;
}
let response = await fetch(url);
let data = await response.json();
if (data[0].samples.length == 0) {
// No new sample.
return;
}
if (lastSampleTime == 0) {
sampleData = data[0];
setDeviceName(sampleData.deviceName);
lastSampleTime = sampleData.sampleTimes.at(-1);
} else if (lastSampleTime < data[0].sampleTimes[0]) {
Array.prototype.push.apply(sampleData.sampleTimes, data[0].sampleTimes);
Array.prototype.push.apply(sampleData.samples, data[0].samples);
lastSampleTime = sampleData.sampleTimes.at(-1);
} else {
console.log("unexpected", data);
}
setText("sampleCount", sampleData.samples.length);
setText("lastPower", sampleData.samples.at(-1));
showGraph();
}
const graphHeight = 120;
const graphWidth = 2400;
const halfStrokeWidth = 3;
function makeSVGPath(graph) {
let lastLetter = "";
function letter(l) {
if (l == lastLetter) {
return "";
}
lastLetter = l;
return l;
}
let path;
function append(cmd) {
if (/^\d/.test(cmd) && /\d$/.test(path)) {
path += " ";
}
path += cmd;
}
let x = i => Math.round(graph[i].x * graphWidth);
let y = i => graph[i].y == 0 ? graphHeight + halfStrokeWidth
: Math.round(Math.max(halfStrokeWidth, (1 - graph[i].y) * graphHeight));
let lastX = -halfStrokeWidth * 2;
let lastY = y(0);
path = `${letter('M')}${lastX} ${graphHeight}V${lastY}`;
for (let i = 0; i < graph.length; ++i) {
let xi = x(i);
let yi = y(i);
if (xi == lastX && yi == lastY) {
continue;
}
if (yi == lastY) {
while (i + 1 < graph.length && y(i + 1) == lastY) {
xi = x(++i);
}
append(`${letter('h')}${xi - lastX}`);
} else {
if (xi == lastX) {
let ys = [yi];
let j = 1;
while (i + j < graph.length && x(i + j) == xi) {
ys.push(y(i + j));
j++;
}
let usefulYs = [];
let max = Math.max(...ys);
let min = Math.min(...ys);
let last = ys[ys.length - 1];
if (max != last && max > lastY) {
usefulYs.push(max);
}
if (min != last && min < lastY) {
usefulYs.push(min);
}
usefulYs.push(last);
i += j - 1;
for (let usefulY of usefulYs) {
yi = usefulY;
let v = `${lastLetter != 'v' ? 'v' : ''}${yi - lastY}`;
let V = `${lastLetter != 'V' ? 'V' : ''}${yi}`;
if (v.length <= V.length) {
append(v);
lastLetter = 'v';
} else {
append(V);
lastLetter = 'V';
}
lastY = yi;
}
} else {
append(`${letter('l')}${xi - lastX}`);
append(`${yi - lastY}`);
}
}
lastX = xi;
lastY = yi;
}
path += `H${graphWidth + halfStrokeWidth * 2}V${graphHeight}`;
return path;
}
function showGraph() {
if (document.hidden) {
return;
}
const {samples, sampleTimes} = sampleData;
let maxPowerW = samples.reduce((a,b) => Math.max(a,b));
setText("maxPower", maxPowerW);
let startTime = sampleData.sampleTimes[0];
let durationMs = sampleTimes.at(-1) - startTime;
setText("totalTime", formatDuration(durationMs / 1000));
let graph = samples.map((v, i) => ({
x: (sampleTimes[i] - startTime) / durationMs,
y: v / maxPowerW}));
document.querySelector("path").setAttribute("d", makeSVGPath(graph));
let energyWms = samples.reduce((acc, val) => acc + val);
setText("averagePower", (energyWms / durationMs).toPrecision(3));
setText("medianPower", samples.slice().sort((a, b) => a - b)[Math.floor(samples.length / 2)]);
setText("totalEnergy", (energyWms /1000 / 3600).toPrecision(3));
}
setInterval(fetchSamples, 5000);
fetchSamples();
function downloadCsv(event) {
let mimeType = "text/plain";
let data = sampleData.sampleTimes.map((v, i) => `${v};${sampleData.samples[i]}`).join("\n");
let url = URL.createObjectURL(
new Blob([data], { type: mimeType })
);
event.target.href = url;
event.target.download = `${new Date().toDateString()} - ${document.getElementById("sampleCount").innerText} samples.csv`;
setTimeout(() => URL.revokeObjectURL(url), 0);
}
function counterObject(name, description, times, samples) {
let time = [];
// Remove consecutive 0 samples.
let count = samples.filter((sample, index) => {
let keep =
sample != 0 ||
index == 0 || index == samples.length - 1 ||
samples[index - 1] != 0 || samples[index + 1] != 0;
if (keep) {
time.push(times[index])
}
return keep;
});
return {
name,
category: "power",
description,
pid: "0",
mainThreadIndex: 0,
samples: {
time, count, length: count.length
}
}
}
function WattMillisecondToPicoWattHour(value) {
return value / 1000 / 3600 * 1e12;
}
function makeProfile() {
const baseProfile = '{"meta":{"interval":1000,"startTime":0,"abi":"","misc":"","oscpu":"","platform":"","processType":0,"extensions":{"id":[],"name":[],"baseURL":[],"length":0},"categories":[{"name":"Other","color":"grey","subcategories":["Other"]}],"product":"USB power profiling","stackwalk":0,"toolkit":"","version":27,"preprocessedProfileVersion":48,"appBuildID":"","sourceURL":"","physicalCPUs":1,"logicalCPUs":0,"CPUName":"USB power meter","symbolicationNotSupported":true,"markerSchema":[]},"libs":[],"pages":[],"threads":[{"processType":"default","processStartupTime":0,"processShutdownTime":null,"registerTime":0,"unregisterTime":null,"pausedRanges":[],"name":"GeckoMain","isMainThread":true,"pid":"0","tid":0,"samples":{"weightType":"samples","weight":null,"eventDelay":[],"stack":[],"time":[],"length":0},"markers":{"data":[],"name":[],"startTime":[],"endTime":[],"phase":[],"category":[],"length":0},"stackTable":{"frame":[0],"prefix":[null],"category":[0],"subcategory":[0],"length":1},"frameTable":{"address":[-1],"inlineDepth":[0],"category":[null],"subcategory":[0],"func":[0],"nativeSymbol":[null],"innerWindowID":[0],"implementation":[null],"line":[null],"column":[null],"length":1},"stringTable":{"_array":["(root)"],"_stringToIndex":{}},"funcTable":{"isJS":[false],"relevantForJS":[false],"name":[0],"resource":[-1],"fileName":[null],"lineNumber":[null],"columnNumber":[null],"length":1},"resourceTable":{"lib":[],"name":[],"host":[],"type":[],"length":0},"nativeSymbols":{"libIndex":[],"address":[],"name":[],"functionSize":[],"length":0}}],"counters":[]}';
let profile = JSON.parse(baseProfile);
profile.meta.startTime = sampleData.startTime;
let sampleTimes = sampleData.sampleTimes;
profile.meta.profilingStartTime = sampleTimes[0];
profile.meta.profilingEndTime = sampleTimes.at(-1);
profile.meta.CPUName = deviceName;
let zeros = new Array(sampleTimes.length).fill(0);
let threadSamples = profile.threads[0].samples;
threadSamples.stack = zeros;
threadSamples.time = sampleTimes;
threadSamples.length = sampleTimes.length;
profile.counters = [
counterObject(deviceName,
`Data recorded by a ${deviceName} power meter`,
sampleTimes,
sampleData.samples.map((v, i) =>
Math.round(WattMillisecondToPicoWattHour(v) *
(sampleTimes[i] - (i > 0 ? sampleTimes[i - 1] : 0)))))
];
return profile;
}
function downloadProfile(event) {
let mimeType = "application/json";
let url = URL.createObjectURL(
new Blob([JSON.stringify(makeProfile())], { type: mimeType })
);
event.target.href = url;
event.target.download = `${new Date().toDateString()} - ${document.getElementById("sampleCount").innerText} samples.json`;
setTimeout(() => URL.revokeObjectURL(url), 0);
}
async function openProfile() {
const origin = "https://profiler.firefox.com";
const profilerURL = origin + "/from-post-message/";
const profilerWindow = window.open(profilerURL, "_blank");
if (!profilerWindow) {
console.error("Failed to open the new window.");
return;
}
let isReady = false;
window.addEventListener("message", function listener(event) {
if (event.data && event.data.name === "ready:response") {
window.removeEventListener("message", listener);
isReady = true;
const message = {
name: "inject-profile",
profile: makeProfile(),
};
profilerWindow.postMessage(message, origin);
}
});
while (true) {
await new Promise((resolve) => setTimeout(resolve, 100));
if (isReady) {
break;
}
profilerWindow.postMessage({ name: "ready:request" }, origin);
}
}
document.getElementById("csv").addEventListener("click", downloadCsv);
document.getElementById("profile").addEventListener("click", downloadProfile);
document.getElementById("open").addEventListener("click", openProfile);
</script>
<footer>This work © 2024 by Florian Quèze is licensed under <a href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-NC 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg" alt="CC"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg" alt="BY"><img src="https://mirrors.creativecommons.org/presskit/icons/nc.svg" alt="NC"></a></footer>
</body>
</html>