Skip to content

Commit fd0c5aa

Browse files
committed
bring back flamegraph
1 parent d64a22b commit fd0c5aa

File tree

4 files changed

+69
-126
lines changed

4 files changed

+69
-126
lines changed

noir-projects/noir-contracts/scripts/flamegraph.sh

Lines changed: 0 additions & 100 deletions
This file was deleted.

yarn-project/aztec/scripts/aztec.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ case $cmd in
4545

4646
aztec start "$@"
4747
;;
48-
compile|new|init)
48+
compile|new|init|flamegraph)
4949
$script_dir/${cmd}.sh "$@"
5050
;;
5151
fmt|check|lsp)
@@ -55,12 +55,4 @@ case $cmd in
5555
*)
5656
aztec $cmd "$@"
5757
;;
58-
# flamegraph)
59-
# docker run -it \
60-
# --entrypoint /usr/src/noir-projects/noir-contracts/scripts/flamegraph.sh \
61-
# --env SERVE=${SERVE:-0} \
62-
# $([ "${SERVE:-0}" == "1" ] && echo "-p 8000:8000" || echo "") \
63-
# -v $(realpath $(dirname $2))/:/tmp \
64-
# $DOCKER_REPO:$VERSION /tmp/$(basename $2) $3
65-
# ;;
6658
esac

noir-projects/noir-contracts/scripts/extractFunctionAsNoirArtifact.js renamed to yarn-project/aztec/scripts/extract_function.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
const path = require("path");
2-
const fs = require("fs").promises;
1+
#!/usr/bin/env node
2+
import fs from 'fs/promises';
3+
import path from 'path';
34

45
// Simple script to extract a contract function as a separate Noir artifact.
56
// We need to use this since the transpiling that we do on public functions make the contract artifacts
67
// unreadable by noir tooling, since they are no longer following the noir artifact format.
78
async function main() {
89
let [contractArtifactPath, functionName] = process.argv.slice(2);
910
if (!contractArtifactPath || !functionName) {
10-
console.log(
11-
"Usage: node extractFunctionAsNoirArtifact.js <contractArtifactPath> <functionName>"
12-
);
11+
console.log('Usage: node extractFunctionAsNoirArtifact.js <contractArtifactPath> <functionName>');
1312
return;
1413
}
1514

16-
const contractArtifact = JSON.parse(
17-
await fs.readFile(contractArtifactPath, "utf8")
18-
);
19-
const func = contractArtifact.functions.find((f) => f.name === functionName);
15+
const contractArtifact = JSON.parse(await fs.readFile(contractArtifactPath, 'utf8'));
16+
const func = contractArtifact.functions.find(f => f.name === functionName);
2017
if (!func) {
21-
console.error(
22-
`Function ${functionName} not found in ${contractArtifactPath}`
23-
);
18+
console.error(`Function ${functionName} not found in ${contractArtifactPath}`);
2419
return;
2520
}
2621

@@ -39,17 +34,14 @@ async function main() {
3934
};
4035

4136
const outputDir = path.dirname(contractArtifactPath);
42-
const outputName =
43-
path.basename(contractArtifactPath, ".json") + `-${functionName}.json`;
37+
const outputName = path.basename(contractArtifactPath, '.json') + `-${functionName}.json`;
4438

4539
const outPath = path.join(outputDir, outputName);
4640

47-
console.log(`Writing to ${outPath}`);
48-
4941
await fs.writeFile(outPath, JSON.stringify(artifact, null, 2));
5042
}
5143

52-
main().catch((err) => {
44+
main().catch(err => {
5345
console.error(err);
5446
process.exit(1);
5547
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
# If first arg is -h or --help, print usage.
5+
if [ $# -lt 2 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
6+
cat << 'EOF'
7+
Aztec Flamegraph - Generate a gate count flamegraph for an aztec contract function.
8+
9+
Usage: aztec flamegraph <contract_artifact> <function>
10+
11+
Options:
12+
-h, --help Print help
13+
14+
Will output an svg at <artifact_path>/<contract>-<function>-flamegraph.svg.
15+
You can open it in your browser to view it.
16+
17+
EOF
18+
exit 0
19+
fi
20+
21+
cleanup() {
22+
set +e
23+
if [ -f "$function_artifact" ]; then
24+
rm -f "$function_artifact"
25+
fi
26+
}
27+
28+
trap cleanup EXIT
29+
30+
# Get the directory of the script
31+
script_dir=$(realpath $(dirname $0))
32+
33+
PROFILER=${PROFILER_PATH:-noir-profiler}
34+
BB=${BB:-bb}
35+
36+
# first console arg is contract name in camel case or path to contract artifact
37+
contract=$1
38+
39+
# second console arg is the contract function
40+
function=$2
41+
42+
if [ ! -f "$contract" ]; then
43+
echo "Error: Contract artifact not found at: $contract"
44+
exit 1
45+
fi
46+
artifact_path=$contract
47+
function_artifact="${artifact_path%%.json}-${function}.json"
48+
output_dir=$(dirname "$artifact_path")
49+
50+
# Extract artifact for the specific function.
51+
node $script_dir/extract_function.js "$artifact_path" $function
52+
53+
# Generate the flamegraph
54+
$PROFILER gates --artifact-path "$function_artifact" --backend-path "$BB" --backend-gates-command "gates" --output "$output_dir" --scheme chonk --include_gates_per_opcode
55+
56+
# Save as $artifact_name-$function-flamegraph.svg
57+
output_file="${function_artifact%%.json}-flamegraph.svg"
58+
mv "$output_dir/__aztec_nr_internals__${function}_gates.svg" "$output_file"
59+
echo "Flamegraph generated at: $output_file"

0 commit comments

Comments
 (0)