-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathmetrics.ts
More file actions
76 lines (67 loc) · 1.93 KB
/
metrics.ts
File metadata and controls
76 lines (67 loc) · 1.93 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
/*
* metrics.ts
*
* Copyright (C) 2020-2023 Posit Software, PBC
*/
import { info } from "../../deno_ral/log.ts";
import { inputTargetIndexCacheMetrics } from "../../project/project-index.ts";
import { functionTimes } from "./function-times.ts";
import { Stats } from "./stats.ts";
type FileReadRecord = {
path: string;
stack: string[];
};
let fileReads: FileReadRecord[] | undefined = undefined;
export function captureFileReads() {
fileReads = [];
const originalReadTextFileSync = Deno.readTextFileSync;
Deno.readTextFileSync = function (path: string | URL) {
try {
throw new Error("File read");
} catch (e) {
if (!(e instanceof Error)) throw e;
const stack = e.stack!.split("\n").slice(2);
fileReads!.push({ path: String(path), stack });
}
return originalReadTextFileSync(path);
};
}
const metricsObject = {
inputTargetIndexCache: inputTargetIndexCacheMetrics,
fileReads,
functionTimes,
};
export type MetricsKeys = keyof typeof metricsObject;
export function quartoPerformanceMetrics(keys?: MetricsKeys[]) {
if (!keys) {
return metricsObject;
}
const result: Record<string, unknown> = {};
for (const key of keys) {
if (key === "functionTimes") {
const metricsObjects = metricsObject[key] as Record<string, Stats>;
const entries = Object.entries(metricsObjects);
result[key] = Object.fromEntries(
entries.map(([name, stats]) => [name, stats.report()]),
);
} else {
result[key] = metricsObject[key];
}
}
return result;
}
export function reportPerformanceMetrics(keys?: MetricsKeys[]) {
const content = JSON.stringify(quartoPerformanceMetrics(keys), null, 2);
const outFile = Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS_FILE");
if (outFile) {
Deno.writeTextFileSync(outFile, content);
} else {
const msg = [
"---",
"Performance metrics",
"Quarto:",
content,
];
info(msg.join("\n"));
}
}