|
| 1 | +/* |
| 2 | + * perfetto-utils.ts |
| 3 | + * |
| 4 | + * Some utility functions for working with Perfetto traces. |
| 5 | + * |
| 6 | + * Copyright (C) 2025 Posit Software, PBC |
| 7 | + */ |
| 8 | + |
| 9 | +type PerfettoTraceEvent = { |
| 10 | + ph: "B" | "E" | "X" | "I" | "C" | "S" | "F"; |
| 11 | + ts: number; |
| 12 | + dur?: number; |
| 13 | + name?: string; |
| 14 | + cat?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export function convertCombinedLuaProfileToCSV( |
| 18 | + profileName: string, |
| 19 | +) { |
| 20 | + const combinedInformation = JSON.parse( |
| 21 | + Deno.readTextFileSync(profileName), |
| 22 | + ); |
| 23 | + |
| 24 | + const lines: string[] = ["filter,name,time"]; |
| 25 | + |
| 26 | + Object.entries(combinedInformation).forEach(([cat, data]) => { |
| 27 | + // deno-lint-ignore no-explicit-any |
| 28 | + Object.entries(data as any).forEach(([fileName, time]) => { |
| 29 | + lines.push(`${cat},${fileName},${time}`); |
| 30 | + }); |
| 31 | + }); |
| 32 | + Deno.writeTextFileSync(profileName, lines.join("\n")); |
| 33 | +} |
| 34 | + |
| 35 | +export function appendToCombinedLuaProfile( |
| 36 | + inputFileName: string, |
| 37 | + individualTraceFile: string, |
| 38 | + combinedInformationFile: string, |
| 39 | +) { |
| 40 | + const individualTrace = JSON.parse( |
| 41 | + Deno.readTextFileSync(individualTraceFile), |
| 42 | + ); |
| 43 | + let combinedInformation: Record<string, Record<string, number>> = {}; |
| 44 | + try { |
| 45 | + combinedInformation = JSON.parse( |
| 46 | + Deno.readTextFileSync(combinedInformationFile), |
| 47 | + ); |
| 48 | + } catch (_) { |
| 49 | + // ignore |
| 50 | + } |
| 51 | + |
| 52 | + const earliestBEventByCat: Record<string, number> = {}; |
| 53 | + const latestEEventByCat: Record<string, number> = {}; |
| 54 | + |
| 55 | + individualTrace.traceEvents.forEach((event: Record<string, unknown>) => { |
| 56 | + const e = event as PerfettoTraceEvent; |
| 57 | + if (e.cat && (e.ph === "B")) { |
| 58 | + if ( |
| 59 | + earliestBEventByCat[e.cat] === undefined || |
| 60 | + e.ts < earliestBEventByCat[e.cat] |
| 61 | + ) { |
| 62 | + earliestBEventByCat[e.cat] = e.ts; |
| 63 | + } |
| 64 | + } else if (e.cat && (e.ph === "E")) { |
| 65 | + if ( |
| 66 | + latestEEventByCat[e.cat] === undefined || |
| 67 | + e.ts > latestEEventByCat[e.cat] |
| 68 | + ) { |
| 69 | + latestEEventByCat[e.cat] = e.ts; |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + Object.entries(earliestBEventByCat).forEach(([cat, start]) => { |
| 75 | + if (latestEEventByCat[cat] === undefined) { |
| 76 | + console.error("No end event for category", cat); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (!combinedInformation[cat]) { |
| 81 | + combinedInformation[cat] = {}; |
| 82 | + } |
| 83 | + combinedInformation[cat][inputFileName] = latestEEventByCat[cat] - start; |
| 84 | + }); |
| 85 | + Deno.writeTextFileSync( |
| 86 | + combinedInformationFile, |
| 87 | + JSON.stringify(combinedInformation, null, 2), |
| 88 | + ); |
| 89 | +} |
0 commit comments