Path: blob/main/tools/profiler/report-profile.ts
12924 views
import { loadProfilerData } from "./lua-profiler-data-loader.ts";1import { LuaStack } from "./types.ts";23const filename = Deno.args[0];4const frameSkip = Deno.args[1] === undefined ? 3 : Number(Deno.args[1]);5const stacks = loadProfilerData(filename, frameSkip);67const estimateSampleRate = (stacks: LuaStack[]) => {8return (stacks[stacks.length - 1].time - stacks[0].time) / (stacks.length - 1);9}1011const totalTime = stacks[stacks.length - 1].time - stacks[0].time;12const sampleRate = estimateSampleRate(stacks);1314const selfTimes = new Map<string, number>();1516for (const stack of stacks) {17const topFrame = stack.frames[0];18const location = topFrame.location;19if (!selfTimes.has(location)) {20selfTimes.set(location, 0);21}22selfTimes.set(location, selfTimes.get(location)! + sampleRate);23}2425const roundToDigits = (num: number, digits: number) => {26const factor = 10 ** digits;27return Math.round(num * factor) / factor;28}2930console.log(31Array.from(selfTimes.entries())32.sort((a, b) => b[1] - a[1])33.map(([location, time]) => `${roundToDigits(100 * time / totalTime, 1)}% (${roundToDigits(time, 3)}): ${location}`).join("\n"));3435