Open History to see the last commit
1#!/usr/bin/env node
2import { spawn } from 'node:child_process'
3import fs from 'node:fs'
4import { createRequire } from 'node:module'
5import path from 'node:path'
6import process from 'node:process'
7β
8const [, , entrypointArg, sourcemapDirArg] = process.argv
9β
10if (!entrypointArg || !sourcemapDirArg) {
11 console.error(
12 'Usage: node benchmarks/memory/run-flame.mjs <entrypoint> <sourcemap-dir>',
13 )
14 process.exit(1)
15}
16β
17const entrypointPath = path.resolve(entrypointArg)
18const sourcemapDirPath = path.resolve(sourcemapDirArg)
19β
20if (!fs.existsSync(entrypointPath)) {
21 console.error(`Flame entrypoint not found: ${entrypointPath}`)
22 process.exit(1)
23}
24β
25if (!fs.existsSync(sourcemapDirPath)) {
26 console.error(`Flame sourcemap directory not found: ${sourcemapDirPath}`)
27 process.exit(1)
28}
29β
30const profileDir = path.join(
31 path.dirname(entrypointPath),
32 '.profiles',
33 new Date().toISOString().replace(/[:.]/g, '-'),
34)
35β
36fs.mkdirSync(profileDir, { recursive: true })
37β
38const entrypointRequire = createRequire(entrypointPath)
39const { generateFlamegraph, generateMarkdown } = entrypointRequire(
40 '@platformatic/flame',
41)
42β
43process.env.NODE_ENV = 'production'
44β
45console.log(`Flame profile directory: ${profileDir}`)
46β
47const childProcess = spawn(process.execPath, [entrypointPath], {
48 cwd: profileDir,
49 env: {
50 ...process.env,
51 NODE_ENV: 'production',
52 TSR_MEMORY_FLAME: '1',
53 TSR_MEMORY_PROFILE_DIR: profileDir,
54 TSR_MEMORY_REQUIRE_FROM: entrypointPath,
55 TSR_MEMORY_SOURCEMAP_DIRS: sourcemapDirPath,
56 },
57 stdio: 'inherit',
58})
59β
60console.log(`Flame profiling process: ${childProcess.pid}`)
61β
62const childCode = await new Promise((resolve) => {
63 childProcess.on('error', (error) => {
64 console.error(`Failed to start Flame profiled process: ${error.message}`)
65 resolve(1)
66 })
67β
68 childProcess.on('close', (code, signal) => {
69 if (signal) {
70 console.error(`Flame profiled process exited via signal ${signal}`)
71 resolve(1)
72 return
73 }
74β
75 resolve(code ?? 0)
76 })
77})
78β
79const heapProfilePaths = fs
80 .readdirSync(profileDir)
81 .filter((fileName) => /^heap-profile-.*\.pb$/.test(fileName))
82 .sort()
83 .map((fileName) => path.join(profileDir, fileName))
84β
85for (const heapProfilePath of heapProfilePaths) {
86 const htmlPath = heapProfilePath.replace(/\.pb$/, '.html')
87 const mdPath = heapProfilePath.replace(/\.pb$/, '.md')
88β
89 console.log(`Generating heap flamegraph: ${htmlPath}`)
90 try {
91 await generateFlamegraph(heapProfilePath, htmlPath)
92 } catch (error) {
93 console.warn(`Failed to generate heap flamegraph: ${error.message}`)
94 }
95β
96 console.log(`Generating heap markdown: ${mdPath}`)
97 try {
98 await generateMarkdown(heapProfilePath, mdPath, { format: 'detailed' })
99 } catch (error) {
100 console.warn(`Failed to generate heap markdown: ${error.message}`)
101 }
102}
103β
104const heapReportPaths = fs
105 .readdirSync(profileDir)
106 .filter((fileName) => /^heap-profile-.*\.(?:html|md)$/.test(fileName))
107 .sort()
108 .map((fileName) => path.join(profileDir, fileName))
109β
110if (heapReportPaths.length > 0) {
111 console.log('Generated heap profile reports:')
112 for (const heapReportPath of heapReportPaths) {
113 console.log(` ${heapReportPath}`)
114 }
115}
116β
117process.exit(childCode)