-
Notifications
You must be signed in to change notification settings - Fork 590
feat: add aztec profile flamegraph command #20741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b7d5eb1
feat: add aztec profile flamegraph command
nchamo 48bd09b
fix: pass PROFILER_PATH to CI test runner
nchamo 192dfd3
fix: write profiler output to artifact dir to avoid cross-device rename
nchamo 391f4e2
fix: run CLI tests sequentially to avoid shared target dir race
nchamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
yarn-project/aztec/src/cli/cmds/profile_flamegraph.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'; | ||
| import { execFileSync } from 'child_process'; | ||
| import { existsSync, readFileSync, rmSync } from 'fs'; | ||
| import { dirname, join } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), '../../..'); | ||
| const CLI = join(PACKAGE_ROOT, 'dest/bin/index.js'); | ||
| const WORKSPACE = join(PACKAGE_ROOT, 'test/mixed-workspace'); | ||
| const TARGET = join(WORKSPACE, 'target'); | ||
| const CONTRACT_ARTIFACT = join(TARGET, 'simple_contract-SimpleContract.json'); | ||
|
|
||
| describe('aztec profile flamegraph', () => { | ||
| const svgPath = join(TARGET, 'simple_contract-SimpleContract-private_function-flamegraph.svg'); | ||
|
|
||
| beforeAll(() => { | ||
| rmSync(TARGET, { recursive: true, force: true }); | ||
| runCompile(); | ||
| runFlamegraph(CONTRACT_ARTIFACT, 'private_function'); | ||
| }, 300_000); | ||
|
|
||
| afterAll(() => { | ||
| rmSync(TARGET, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('generates a valid flamegraph SVG', () => { | ||
| expect(existsSync(svgPath)).toBe(true); | ||
| const content = readFileSync(svgPath, 'utf-8'); | ||
| expect(content).toContain('<svg'); | ||
| expect(content).toContain('</svg>'); | ||
| }); | ||
| }); | ||
|
|
||
| function runCompile() { | ||
| try { | ||
| execFileSync('node', [CLI, 'compile'], { cwd: WORKSPACE, stdio: 'pipe' }); | ||
| } catch (e: any) { | ||
| throw new Error(`compile failed:\n${e.stderr?.toString() ?? e.message}`); | ||
| } | ||
| } | ||
|
|
||
| function runFlamegraph(artifactPath: string, functionName: string) { | ||
| try { | ||
| execFileSync('node', [CLI, 'profile', 'flamegraph', artifactPath, functionName], { | ||
| encoding: 'utf-8', | ||
| stdio: 'pipe', | ||
| }); | ||
| } catch (e: any) { | ||
| throw new Error(`profile flamegraph failed:\n${e.stderr?.toString() ?? e.message}`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { LogFn } from '@aztec/foundation/log'; | ||
|
|
||
| import { readFile, rename, rm, writeFile } from 'fs/promises'; | ||
| import { basename, dirname, join } from 'path'; | ||
|
|
||
| import { makeFunctionArtifact } from './profile_utils.js'; | ||
| import type { CompiledArtifact } from './utils/artifacts.js'; | ||
| import { run } from './utils/spawn.js'; | ||
|
|
||
| /** Generates a gate count flamegraph SVG for a single contract function. */ | ||
| export async function profileFlamegraph(artifactPath: string, functionName: string, log: LogFn): Promise<void> { | ||
| const raw = await readFile(artifactPath, 'utf-8'); | ||
| const artifact: CompiledArtifact = JSON.parse(raw); | ||
|
|
||
| if (!Array.isArray(artifact.functions)) { | ||
| throw new Error(`${artifactPath} does not appear to be a contract artifact (no functions array)`); | ||
| } | ||
|
|
||
| const func = artifact.functions.find(f => f.name === functionName); | ||
| if (!func) { | ||
| const available = artifact.functions.map(f => f.name).join(', '); | ||
| throw new Error(`Function "${functionName}" not found in artifact. Available: ${available}`); | ||
| } | ||
| if (func.is_unconstrained) { | ||
| throw new Error(`Function "${functionName}" is unconstrained and cannot be profiled`); | ||
| } | ||
|
|
||
| const outputDir = dirname(artifactPath); | ||
| const contractName = basename(artifactPath, '.json'); | ||
| const functionArtifact = join(outputDir, `${contractName}-${functionName}.json`); | ||
|
|
||
| try { | ||
| await writeFile(functionArtifact, makeFunctionArtifact(artifact, func)); | ||
|
|
||
| const profiler = process.env.PROFILER_PATH ?? 'noir-profiler'; | ||
| const bb = process.env.BB ?? 'bb'; | ||
|
|
||
| await run(profiler, [ | ||
| 'gates', | ||
| '--artifact-path', | ||
| functionArtifact, | ||
| '--backend-path', | ||
| bb, | ||
| '--backend-gates-command', | ||
| 'gates', | ||
| '--output', | ||
| outputDir, | ||
| '--scheme', | ||
| 'chonk', | ||
| '--include_gates_per_opcode', | ||
| ]); | ||
|
|
||
| // noir-profiler names the SVG using the internal function name which | ||
| // retains the __aztec_nr_internals__ prefix in the bytecode metadata. | ||
| const srcSvg = join(outputDir, `__aztec_nr_internals__${functionName}_gates.svg`); | ||
| const destSvg = join(outputDir, `${contractName}-${functionName}-flamegraph.svg`); | ||
| await rename(srcSvg, destSvg); | ||
|
|
||
| log(`Flamegraph written to ${destSvg}`); | ||
| } finally { | ||
| await rm(functionArtifact, { force: true }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { spawn } from 'child_process'; | ||
|
|
||
| /** Spawns a command with inherited stdio and rejects on non-zero exit. */ | ||
| export function run(cmd: string, args: string[]): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| const child = spawn(cmd, args, { stdio: 'inherit' }); | ||
| child.on('error', reject); | ||
| child.on('close', code => { | ||
| if (code !== 0) { | ||
| reject(new Error(`${cmd} exited with code ${code}`)); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are still keeping the old command, but we are adding a deprecation warning. Internally, it's just using the new command