forked from Appsilon/shiny.tictoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiny-tic-toc.js
More file actions
409 lines (341 loc) · 10.6 KB
/
shiny-tic-toc.js
File metadata and controls
409 lines (341 loc) · 10.6 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
function makeLabel(id, suffix) {
return `${id}_${suffix}`;
}
function makeStartMarkLabel(id) {
return makeLabel(id, 'start');
}
function makeEndMarkLabel(id) {
return makeLabel(id, 'end');
}
function makeMeasurementLabel(id) {
return id;
}
function startMeasurement(id) {
const startMarkLabel = makeStartMarkLabel(id);
try {
performance.mark(startMarkLabel);
} catch (error) {
console.warn('Failed to create performance mark:', error);
}
}
function endMeasurement(id) {
const startMarkLabel = makeStartMarkLabel(id);
const endMarkLabel = makeEndMarkLabel(id);
const measurementLabel = makeMeasurementLabel(id);
try {
performance.mark(endMarkLabel);
performance.measure(
measurementLabel,
{
start: startMarkLabel,
end: endMarkLabel
}
);
} catch (error) {
console.warn('Failed to create performance measurement:', error);
}
}
function outputRecalculatingHandler(event) {
const outputId = event.target.id;
startMeasurement(outputId);
}
function outputValueHandler(event) {
const outputId = event.target.id;
// setTimeout to end measuring after the output JS code is run
// See https://github.com/rstudio/shiny/issues/2127
setTimeout(() => {
endMeasurement(outputId);
}, 0);
}
function serverBusyHandler(event) {
startMeasurement("server_computation");
}
function serverIdleHandler(event) {
endMeasurement("server_computation");
}
function customMessageEventHandler(event) {
if (event.message.custom === undefined) {
return;
}
const handlerName = Object.keys(event.message.custom)[0];
startMeasurement(handlerName);
setTimeout(() => {
endMeasurement(handlerName);
}, 0)
}
$(document).ready(function () {
// Handler for output start marks
$(document).on('shiny:recalculating', outputRecalculatingHandler);
// Handler for output end marks
$(document).on('shiny:value', outputValueHandler);
// Handler for server calculation start marks
$(document).on('shiny:busy', serverBusyHandler);
// Handler for server calculation end marks
$(document).on('shiny:idle', serverIdleHandler);
// Handler for custom handlers
$(document).on('shiny:message', customMessageEventHandler);
});
function showAllMeasurements() {
try {
const entries = performance.getEntriesByType("measure") || [];
entries
.filter(entry => entry != null &&
typeof entry === 'object' &&
entry.name &&
typeof entry.duration === 'number')
.forEach((entry) => {
console.log(`${entry.name}'s duration: ${entry.duration}`);
});
} catch (error) {
console.warn('Failed to retrieve performance measurements:', error);
}
}
function showSummarisedMeasurements() {
try {
const serverComputationLabel = makeMeasurementLabel("server_computation");
const measurements = (performance.getEntriesByType("measure") || [])
.filter(entry => entry != null &&
typeof entry === 'object' &&
entry.name &&
typeof entry.duration === 'number');
if (measurements.length === 0) {
console.log('No performance measurements available');
return;
}
const serverComputations = measurements
.filter(entry => entry.name === serverComputationLabel)
.map(entry => entry.duration);
const outputComputations = measurements
.filter(entry => entry.name !== serverComputationLabel)
.map(entry => entry.duration);
const slowestServerComputation = serverComputations.length > 0
? Math.max(...serverComputations)
: 0;
const slowestOutputComputation = outputComputations.length > 0
? Math.max(...outputComputations)
: 0;
const slowestOutputEntry = measurements
.find(entry => entry.duration === slowestOutputComputation);
const slowestOutputLabel = slowestOutputEntry?.name || 'Unknown';
console.log(`Slowest Server Computation: ${slowestServerComputation}`);
console.log(`Slowest output computation: ${slowestOutputComputation} (${slowestOutputLabel})`);
} catch (error) {
console.warn('Failed to analyze performance measurements:', error);
}
}
function getCurrentDateTime() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Months are zero-based
const day = String(currentDate.getDate()).padStart(2, '0');
const hours = String(currentDate.getHours()).padStart(2, '0');
const minutes = String(currentDate.getMinutes()).padStart(2, '0');
const seconds = String(currentDate.getSeconds()).padStart(2, '0');
const formattedDateTime = `${year}_${month}_${day}-${hours}_${minutes}_${seconds}`;
return formattedDateTime;
}
function downloadFile(content, contentType, filename) {
const blob = new Blob([content], { type: contentType });
const link = document.createElement('a')
const url = window.URL.createObjectURL(blob)
link.href = url
link.download = filename
link.click()
window.URL.revokeObjectURL(url);
}
function downloadCsvFile(data) {
const csvContent = data.map(e => e.join(",")).join("\n");
downloadFile(
csvContent,
'text/csv;charset=utf-8',
`${getCurrentDateTime()}-tictoc.csv`
)
}
function getMeasurements() {
try {
const entries = performance.getEntries();
if (!Array.isArray(entries)) {
console.warn('Performance API returned non-array result');
return [];
}
return entries
.filter(entry => entry != null &&
typeof entry === 'object' &&
entry.entryType === 'measure' &&
entry.name &&
typeof entry.duration === 'number' &&
typeof entry.startTime === 'number')
.map(entry => ({
name: entry?.name || '',
duration: entry?.duration || 0,
startTime: entry?.startTime || 0
}));
} catch (error) {
console.warn('Failed to retrieve performance measurements:', error);
return [];
}
}
function prepareCsvData() {
const dataHeader = ["measurement_id", "start_time", "duration (ms)"];
const measurementData = getMeasurements()
.map(measurement => [
measurement.name,
measurement.startTime,
measurement.duration
]);
const csvData = [dataHeader].concat(measurementData);
return csvData;
}
function exportMeasurements() {
csvData = prepareCsvData();
downloadCsvFile(csvData);
}
// Plotting script
function plotMeasurements() {
const chartDom = document.getElementById('measurementsTimeline');
const myChart = echarts.init(chartDom);
const measurementData = JSON.parse(document.getElementById('measurementData').text);
const measurementIds = measurementData.map(entry => entry.name);
const uniqueMeasurementIds = [...new Set(measurementIds)];
const plotData = measurementData.map(
entry => {
const measurementIdIndex = uniqueMeasurementIds.indexOf(entry.name);
return {
name: entry.name,
value: [
measurementIdIndex,
entry.startTime,
entry.startTime + entry.duration,
entry.duration
]
};
}
);
function renderItem(params, api) {
const categoryIndex = api.value(0);
const start = api.coord([api.value(1), categoryIndex]);
const end = api.coord([api.value(2), categoryIndex]);
const height = api.size([0, 1])[1] * 0.6;
const rectShape = echarts.graphic.clipRectByRect(
{
x: start[0],
y: start[1] - height / 2,
width: end[0] - start[0],
height: height
},
{
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
}
);
return (
rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: api.style()
}
);
}
const option = {
tooltip: {
formatter: function (params) {
return params.marker + params.name + ': ' + params.value[3] + ' ms';
}
},
title: {
text: 'shiny.tictoc report',
left: 'center'
},
dataZoom: [
{
type: 'slider',
filterMode: 'weakFilter',
showDataShadow: false,
top: 400,
labelFormatter: ''
},
{
type: 'inside',
filterMode: 'weakFilter'
}
],
grid: {
height: 300,
containLabel: true
},
xAxis: {
min: 0,
scale: true,
axisLabel: {
formatter: function (val) {
return Math.max(0, val) + ' ms';
}
}
},
yAxis: {
data: uniqueMeasurementIds
},
series: [
{
type: 'custom',
renderItem: renderItem,
itemStyle: {
opacity: 0.8
},
encode: {
x: [1, 2],
y: 0
},
data: plotData
}
]
};
myChart.setOption(option);
}
async function createHtmlReport() {
const measurementData = getMeasurements();
const report = document.implementation.createHTMLDocument("shiny.tictoc report");
// Data Script
const dataScript = document.createElement("script");
dataScript.id = "measurementData";
dataScript.type = "application/json";
dataScript.innerText = `${JSON.stringify(measurementData)}`;
// Plot div
const plotDiv = document.createElement("div");
plotDiv.id = "measurementsTimeline";
plotDiv.style.height = "100vh";
// Echarts script with error handling
const echartsCDN = "https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js";
try {
const echartsLibSourceCode = await fetch(echartsCDN).then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch ECharts library: ${response.status}`);
}
return response.text();
});
const echartsLibraryScriptTag = document.createElement("script");
echartsLibraryScriptTag.text = echartsLibSourceCode;
report.head.appendChild(echartsLibraryScriptTag);
} catch (error) {
console.warn('Failed to load ECharts library from CDN:', error);
// Add fallback message to the plot div
plotDiv.innerHTML = '<p>Unable to load charting library. Chart visualization is not available.</p>';
}
const plottingScript = document.createElement("script");
plottingScript.text = `${plotMeasurements.toString()}; window.onload = plotMeasurements`;
report.head.appendChild(dataScript);
report.head.appendChild(plottingScript);
report.body.appendChild(plotDiv);
return report;
}
async function exportHtmlReport() {
const htmlReport = await createHtmlReport();
downloadFile(
htmlReport.documentElement.innerHTML,
'text/html;charset=utf-8',
`${getCurrentDateTime()}-tictoc.html`
)
}