Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tools/profiler/lua-profiler-data-loader.ts
6435 views
1
import { LuaStack } from "./types.ts";
2
3
export const loadProfilerData = (filename: string, frameSkip: number = 3): LuaStack[] => {
4
let longestCommonPrefix: string | undefined = undefined;
5
const file = Deno.readTextFileSync(filename);
6
// find the longest common prefix for source to trim
7
for (const fileLine of file.split("\n")) {
8
if (fileLine === "")
9
continue;
10
const entries = fileLine.split(" ");
11
if (entries.length === 4) {
12
continue;
13
}
14
const [_name, source] = fileLine.split(" ");
15
if (source === "=[C]") continue;
16
17
if (longestCommonPrefix === undefined) {
18
longestCommonPrefix = source;
19
} else {
20
// find the longest common prefix
21
let i = 0;
22
while (i < longestCommonPrefix.length && longestCommonPrefix[i] === source[i]) {
23
i++;
24
}
25
longestCommonPrefix = longestCommonPrefix.slice(0, i);
26
}
27
}
28
29
if (longestCommonPrefix === undefined) {
30
throw new Error("Internal error: no common prefix found");
31
}
32
33
type LuaStack = {
34
frames: LuaStackFrame[];
35
time: number;
36
line: number;
37
category: string;
38
}
39
40
type LuaStackFrame = {
41
location: string;
42
// name: string;
43
// source: string;
44
// line: number;
45
}
46
47
const stacks: LuaStack[] = [];
48
let stackNum = "";
49
let time = 0;
50
let thisStack: LuaStackFrame[] = [];
51
let category = "";
52
53
for (const fileLine of file.split("\n")) {
54
if (fileLine === "") continue;
55
const entries = fileLine.split(" ");
56
if (entries.length === 4) {
57
category = entries[2];
58
if (stackNum !== entries[0]) {
59
if (thisStack.length) {
60
thisStack[0].location = `${thisStack[0].location}:${entries[3]}`;
61
}
62
stacks.push({
63
frames: thisStack,
64
time: Number(entries[1]),
65
line: Number(entries[3]),
66
category
67
});
68
thisStack = [];
69
stackNum = entries[0];
70
}
71
continue;
72
}
73
const [name, source, line] = fileLine.split(" ");
74
try {
75
const frame: LuaStackFrame = {
76
location: `${source.slice(longestCommonPrefix.length)}:${line}:${name}`,
77
};
78
thisStack.push(frame);
79
} catch (_e) {
80
throw new Error(`Error parsing line: ${fileLine}`);
81
}
82
}
83
84
return stacks;
85
}
86
87