Path: blob/main/tools/profiler/lua-profiler-data-loader.ts
6435 views
import { LuaStack } from "./types.ts";12export const loadProfilerData = (filename: string, frameSkip: number = 3): LuaStack[] => {3let longestCommonPrefix: string | undefined = undefined;4const file = Deno.readTextFileSync(filename);5// find the longest common prefix for source to trim6for (const fileLine of file.split("\n")) {7if (fileLine === "")8continue;9const entries = fileLine.split(" ");10if (entries.length === 4) {11continue;12}13const [_name, source] = fileLine.split(" ");14if (source === "=[C]") continue;1516if (longestCommonPrefix === undefined) {17longestCommonPrefix = source;18} else {19// find the longest common prefix20let i = 0;21while (i < longestCommonPrefix.length && longestCommonPrefix[i] === source[i]) {22i++;23}24longestCommonPrefix = longestCommonPrefix.slice(0, i);25}26}2728if (longestCommonPrefix === undefined) {29throw new Error("Internal error: no common prefix found");30}3132type LuaStack = {33frames: LuaStackFrame[];34time: number;35line: number;36category: string;37}3839type LuaStackFrame = {40location: string;41// name: string;42// source: string;43// line: number;44}4546const stacks: LuaStack[] = [];47let stackNum = "";48let time = 0;49let thisStack: LuaStackFrame[] = [];50let category = "";5152for (const fileLine of file.split("\n")) {53if (fileLine === "") continue;54const entries = fileLine.split(" ");55if (entries.length === 4) {56category = entries[2];57if (stackNum !== entries[0]) {58if (thisStack.length) {59thisStack[0].location = `${thisStack[0].location}:${entries[3]}`;60}61stacks.push({62frames: thisStack,63time: Number(entries[1]),64line: Number(entries[3]),65category66});67thisStack = [];68stackNum = entries[0];69}70continue;71}72const [name, source, line] = fileLine.split(" ");73try {74const frame: LuaStackFrame = {75location: `${source.slice(longestCommonPrefix.length)}:${line}:${name}`,76};77thisStack.push(frame);78} catch (_e) {79throw new Error(`Error parsing line: ${fileLine}`);80}81}8283return stacks;84}858687