@alejoamiras/aztec-benchmark 0.0.0-canary.gb8d1485

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Wonderland
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # Aztec Benchmark
2
+ [![npm version](https://badge.fury.io/js/%40defi-wonderland%2Faztec-benchmark.svg)](https://www.npmjs.com/package/@alejoamiras/aztec-benchmark)
3
+
4
+ **CLI tool and reusable CI workflows for running Aztec contract benchmarks.**
5
+
6
+ Use the CLI to execute benchmark files written in TypeScript. For CI integration, this repository provides **reusable GitHub workflows** that handle the full benchmark-and-compare cycle — including environment setup, baseline management, and PR commenting — so consumer repos can integrate with a single `uses:` line.
7
+
8
+ ## Table of Contents
9
+
10
+ - [Installation](#installation)
11
+ - [CLI Usage](#cli-usage)
12
+ - [Configuration (`Nargo.toml`)](#configuration-nargotoml)
13
+ - [Options](#options)
14
+ - [Examples](#examples)
15
+ - [Writing Benchmarks](#writing-benchmarks)
16
+ - [Benchmark Output](#benchmark-output)
17
+ - [Reusable Workflows](#reusable-workflows)
18
+ - [PR Benchmark (`pr-benchmark.yml`)](#pr-benchmark-pr-benchmarkyml)
19
+ - [Update Baseline (`update-baseline.yml`)](#update-baseline-update-baselineyml)
20
+ - [How Baselines Work](#how-baselines-work)
21
+ - [Action Usage (Advanced)](#action-usage-advanced)
22
+ - [Inputs](#inputs)
23
+ - [Outputs](#outputs)
24
+
25
+ ---
26
+
27
+ ## Installation
28
+
29
+ ```sh
30
+ yarn add --dev @alejoamiras/aztec-benchmark
31
+ # or
32
+ npm install --save-dev @alejoamiras/aztec-benchmark
33
+ ```
34
+
35
+ ---
36
+
37
+ ## CLI Usage
38
+
39
+ After installing, run the CLI using `npx aztec-benchmark`. By default, it looks for a `Nargo.toml` file in the current directory and runs benchmarks defined within it.
40
+
41
+ ```sh
42
+ npx aztec-benchmark [options]
43
+ ```
44
+
45
+ ### Configuration (`Nargo.toml`)
46
+
47
+ Define which contracts have associated benchmark files in your `Nargo.toml` under the `[benchmark]` section:
48
+
49
+ ```toml
50
+ [benchmark]
51
+ token = "benchmarks/token_contract.benchmark.ts"
52
+ another_contract = "path/to/another.benchmark.ts"
53
+ ```
54
+
55
+ The paths to the `.benchmark.ts` files are relative to the `Nargo.toml` file.
56
+
57
+ ### Options
58
+
59
+ - `-c, --contracts <names...>`: Specify which contracts (keys from the `[benchmark]` section) to run. If omitted, runs all defined benchmarks.
60
+ - `--config <path>`: Path to your `Nargo.toml` file (default: `./Nargo.toml`).
61
+ - `-o, --output-dir <path>`: Directory to save benchmark JSON reports (default: `./benchmarks`).
62
+ - `-s, --suffix <suffix>`: Optional suffix to append to report filenames (e.g., `_pr` results in `token_pr.benchmark.json`).
63
+ - `--skip-proving`: Skip proving transactions. Only measures gate counts and gas; proving time will be `0` in reports. When enabled, the `wallet` is not required in the benchmark context.
64
+
65
+ ### Examples
66
+
67
+ Run all benchmarks defined in `./Nargo.toml`:
68
+ ```sh
69
+ npx aztec-benchmark
70
+ ```
71
+
72
+ Run only the `token` benchmark:
73
+ ```sh
74
+ npx aztec-benchmark --contracts token
75
+ ```
76
+
77
+ Run `token` and `another_contract` benchmarks, saving reports with a suffix:
78
+ ```sh
79
+ npx aztec-benchmark --contracts token another_contract --output-dir ./benchmark_results --suffix _v2
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Writing Benchmarks
85
+
86
+ Benchmarks are TypeScript classes extending `BenchmarkBase` from this package.
87
+ Each entry in the array returned by `getMethods` can either be a plain `ContractFunctionInteractionCallIntent`
88
+ (in which case the benchmark name is auto-derived) or a `NamedBenchmarkedInteraction` object
89
+ (which includes the `interaction` and a custom `name` for reporting).
90
+
91
+ ### Fee Payment
92
+
93
+ By default, every benchmarked account must hold Fee Juice (FJ) to pay for transaction fees. If your accounts don't have pre-existing FJ (e.g. freshly-created accounts on sandbox), you can return a `feePaymentMethod` from `setup()` inside the `BenchmarkContext`. The profiler will pass it to every `send()` and `proveInteraction()` call automatically.
94
+
95
+ The sandbox ships with a canonical `SponsoredFPC` contract that has FJ and can sponsor fees for any account — making it the easiest way to get benchmarks running without bridging from L1.
96
+
97
+ ```ts
98
+ import {
99
+ Benchmark, // Alias for BenchmarkBase
100
+ type BenchmarkContext,
101
+ type NamedBenchmarkedInteraction
102
+ } from '@alejoamiras/aztec-benchmark';
103
+ import type { PXE } from '@aztec/pxe/server';
104
+ import type { Contract } from '@aztec/aztec.js/contracts'; // Generic Contract type from Aztec.js
105
+ import type { AztecAddress } from '@aztec/aztec.js/addresses';
106
+ import type { ContractFunctionInteractionCallIntent } from '@aztec/aztec.js/authorization';
107
+ import type { FeePaymentMethod } from '@aztec/aztec.js/fee';
108
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
109
+ import { createPXE, getPXEConfig } from '@aztec/pxe/server';
110
+ import { createAztecNodeClient, waitForNode } from '@aztec/aztec.js/node';
111
+ import { EmbeddedWallet } from '@aztec/wallets/embedded';
112
+ import { registerInitialLocalNetworkAccountsInWallet } from '@aztec/wallets/testing';
113
+ // import { YourSpecificContract } from '../artifacts/YourSpecificContract.js'; // Replace with your actual contract artifact
114
+
115
+ // 1. Define a specific context for your benchmark (optional but good practice)
116
+ interface MyBenchmarkContext extends BenchmarkContext {
117
+ pxe: PXE;
118
+ wallet: EmbeddedWallet;
119
+ deployer: AztecAddress;
120
+ contract: Contract; // Use the generic Contract type or your specific contract type
121
+ feePaymentMethod?: FeePaymentMethod;
122
+ }
123
+
124
+ export default class MyContractBenchmark extends Benchmark {
125
+ // Runs once before all benchmark methods.
126
+ async setup(): Promise<MyBenchmarkContext> {
127
+ console.log('Setting up benchmark environment...');
128
+
129
+ const { NODE_URL = 'http://localhost:8080' } = process.env;
130
+ const node = createAztecNodeClient(NODE_URL);
131
+ await waitForNode(node);
132
+ const l1Contracts = await node.getL1ContractAddresses();
133
+ const config = getPXEConfig();
134
+ const fullConfig = { ...config, l1Contracts };
135
+ // IMPORTANT: true enables proof generation for the benchmark, set it to false when using --skip-proving
136
+ fullConfig.proverEnabled = true;
137
+ const pxeVersion = 2;
138
+ const store = await createStore('pxe', pxeVersion, {
139
+ dataDirectory: 'store',
140
+ dataStoreMapSizeKb: 1e6,
141
+ });
142
+
143
+ const pxe: PXE = await createPXE(node, fullConfig, { store });
144
+ // `EmbeddedWalletOptions` uses a unified `pxe` field for PXE config and dependency overrides.
145
+ const wallet: EmbeddedWallet = await EmbeddedWallet.create(node, { pxe: fullConfig });
146
+ const accounts: AztecAddress[] = await registerInitialLocalNetworkAccountsInWallet(wallet);
147
+ const [deployer] = accounts;
148
+
149
+ // Deploy your contract (replace YourSpecificContract with your actual contract class).
150
+ // `DeployMethod.send()` now always returns `{ contract, receipt, instance }`.
151
+ const { contract } = await YourSpecificContract
152
+ .deploy(wallet, /* constructor args */)
153
+ .send({ from: deployer });
154
+ console.log('Contract deployed at:', contract.address.toString());
155
+
156
+ // Optional: use SponsoredFPC so accounts don't need pre-existing Fee Juice.
157
+ // The sandbox ships with a canonical SponsoredFPC pre-deployed at a deterministic address.
158
+ //
159
+ // import { SponsoredFeePaymentMethod } from '@aztec/aztec.js/fee/testing';
160
+ // import { SponsoredFPCContract } from '@aztec/noir-contracts.js/SponsoredFPC';
161
+ // import { getContractInstanceFromInstantiationParams } from '@aztec/aztec.js/contracts';
162
+ //
163
+ // const instance = await getContractInstanceFromInstantiationParams(
164
+ // SponsoredFPCContract.artifact,
165
+ // { salt: new Fr(0n) },
166
+ // );
167
+ // await wallet.registerContract(instance, SponsoredFPCContract.artifact);
168
+ // const feePaymentMethod = new SponsoredFeePaymentMethod(instance.address);
169
+
170
+ return { pxe, wallet, deployer, contract /*, feePaymentMethod */ };
171
+ }
172
+
173
+ // Returns an array of interactions to benchmark.
174
+ getMethods(context: MyBenchmarkContext): Promise<Array<ContractFunctionInteractionCallIntent | NamedBenchmarkedInteraction>> {
175
+ // Ensure context is available (it should be if setup ran correctly)
176
+ if (!context || !context.contract) {
177
+ // In a real scenario, setup() must initialize the context properly.
178
+ // Throwing an error or returning an empty array might be appropriate here if setup failed.
179
+ console.error("Benchmark context or contract not initialized in setup(). Skipping getMethods.");
180
+ return [];
181
+ }
182
+
183
+ const { contract, deployer } = context;
184
+ const recipient = deployer; // Example recipient
185
+
186
+ // Replace `contract.methods.someMethodName` with actual methods from your contract.
187
+ const interactionPlain = { caller: deployer, action: contract.methods.transfer(recipient, 100n) }
188
+ const interactionNamed1 = { caller: deployer, action: contract.methods.someOtherMethod("test_value_1") };
189
+ const interactionNamed2 = { caller: deployer, action: contract.methods.someOtherMethod("test_value_2") };
190
+
191
+ return [
192
+ // Example of a plain interaction - name will be auto-derived
193
+ interactionPlain,
194
+ // Example of a named interaction
195
+ { interaction: interactionNamed1, name: "Some Other Method (value 1)" },
196
+ // Another named interaction
197
+ { interaction: interactionNamed2, name: "Some Other Method (value 2)" },
198
+ ];
199
+ }
200
+
201
+ // Optional cleanup phase
202
+ async teardown(context: MyBenchmarkContext): Promise<void> {
203
+ console.log('Cleaning up benchmark environment...');
204
+ if (context && context.pxe) {
205
+ await context.pxe.stop();
206
+ }
207
+ }
208
+ }
209
+ ```
210
+
211
+ **Note:** Your benchmark code needs a valid Aztec project setup to interact with contracts.
212
+ Your `BenchmarkBase` implementation is responsible for constructing the `ContractFunctionInteractionCallIntent` objects.
213
+ If you provide a `NamedBenchmarkedInteraction` object, its `name` field will be used in reports.
214
+ If you provide a plain `ContractFunctionInteractionCallIntent`, the tool will attempt to derive a name from the interaction (e.g., the method name).
215
+ If you return a `feePaymentMethod` in the `BenchmarkContext`, it is automatically passed to every transaction the profiler sends — no changes to `getMethods` are needed.
216
+
217
+ ### Wonderland's Usage Example
218
+
219
+ You can find how we use this tool for benchmarking our Aztec contracts in [`aztec-standards`](https://github.com/defi-wonderland/aztec-standards/tree/dev/benchmarks).
220
+
221
+ ---
222
+
223
+ ## Benchmark Output
224
+
225
+ Your `BenchmarkBase` implementation is responsible for measuring and outputting performance data (e.g., as JSON). The comparison action uses this output.
226
+ Each entry in the output will be identified by the custom `name` you provided (if any) or the auto-derived name.
227
+
228
+ ---
229
+
230
+ ## Reusable Workflows
231
+
232
+ This repository ships two **reusable GitHub workflows** (`workflow_call`) that handle the full CI benchmark cycle. Consumer repos call them with a single `uses:` line — no need to copy workflow YAML or wire up artifact management manually.
233
+
234
+ ### PR Benchmark (`pr-benchmark.yml`)
235
+
236
+ Runs benchmarks on the PR head, downloads the baseline from the base branch, generates a comparison report, comments it on the PR (hiding any previous benchmark comments as outdated), and uploads the new results as a baseline artifact for the PR branch.
237
+
238
+ **Usage:**
239
+
240
+ ```yaml
241
+ # .github/workflows/pr-checks.yml
242
+ name: PR Checks
243
+
244
+ on:
245
+ pull_request:
246
+ branches: [dev, main]
247
+
248
+ jobs:
249
+ benchmark:
250
+ uses: defi-wonderland/aztec-benchmark/.github/workflows/pr-benchmark.yml@v0
251
+ permissions:
252
+ pull-requests: write
253
+ issues: write
254
+ actions: read
255
+ ```
256
+
257
+ **Inputs:**
258
+
259
+ | Input | Type | Default | Description |
260
+ |---|---|---|---|
261
+ | `runner` | `string` | `ubuntu-latest-m` | GitHub runner label |
262
+ | `timeout` | `number` | `120` | Job timeout in minutes |
263
+ | `bench-dir` | `string` | `./benchmarks` | Directory for benchmark files |
264
+
265
+ **With custom inputs:**
266
+
267
+ ```yaml
268
+ jobs:
269
+ benchmark:
270
+ uses: defi-wonderland/aztec-benchmark/.github/workflows/pr-benchmark.yml@v0
271
+ permissions:
272
+ pull-requests: write
273
+ issues: write
274
+ actions: read
275
+ with:
276
+ runner: ubuntu-latest-l
277
+ timeout: 180
278
+ bench-dir: ./my-benchmarks
279
+ ```
280
+
281
+ ### Update Baseline (`update-baseline.yml`)
282
+
283
+ Runs benchmarks on the current branch and uploads the results as a baseline artifact. This should be triggered on pushes to your default branches so that PR benchmarks have a baseline to compare against.
284
+
285
+ **Usage:**
286
+
287
+ ```yaml
288
+ # .github/workflows/update-baseline.yml
289
+ name: Update Baseline
290
+
291
+ on:
292
+ push:
293
+ branches: [dev, main]
294
+
295
+ jobs:
296
+ update-baseline:
297
+ uses: defi-wonderland/aztec-benchmark/.github/workflows/update-baseline.yml@v0
298
+ permissions:
299
+ contents: read
300
+ actions: write
301
+ ```
302
+
303
+ **Inputs:**
304
+
305
+ | Input | Type | Default | Description |
306
+ |---|---|---|---|
307
+ | `runner` | `string` | `ubuntu-latest-m` | GitHub runner label |
308
+ | `timeout` | `number` | `120` | Job timeout in minutes |
309
+ | `bench-dir` | `string` | `./benchmarks` | Directory for benchmark files |
310
+
311
+ ### How Baselines Work
312
+
313
+ The workflows use GitHub Actions artifacts to store and retrieve baseline benchmark results:
314
+
315
+ 1. **`update-baseline.yml`** runs benchmarks with the `_latest` suffix and uploads the results as `benchmark-baseline-<branch>`.
316
+ 2. **`pr-benchmark.yml`** runs benchmarks with the `_new` suffix on the PR head, then downloads the `benchmark-baseline-<base-branch>` artifact to get the `_latest` files. It compares `_latest` (baseline) vs `_new` (PR) and comments a Markdown diff table on the PR.
317
+ 3. Before posting the new comment, the workflow finds all previous benchmark comments on the PR (identified by a unique marker in the comment body) and hides them as **Outdated** via the GitHub GraphQL API, so the PR timeline stays clean.
318
+ 4. After comparison, the PR workflow renames `_new` files to `_latest` and uploads them as `benchmark-baseline-<head-branch>`, so stacked PRs can also compare against each other.
319
+
320
+ Artifacts are retained for **90 days** by default.
321
+
322
+ ---
323
+
324
+ ## Action Usage (Advanced)
325
+
326
+ > **Note:** For most projects, the [reusable workflows](#reusable-workflows) above are the recommended approach. The action below is a lower-level building block for projects that need a custom CI setup.
327
+
328
+ This repository also includes a GitHub Action (defined in `action/action.yml`) that runs `aztec-benchmark` and compares results. It automatically finds benchmark reports (named with `_base` and `_latest` suffixes) and produces a Markdown comparison report.
329
+
330
+ ### Inputs
331
+
332
+ - `threshold`: Regression threshold percentage (default: `2.5`).
333
+ - `output_markdown_path`: Path to save the generated Markdown comparison report (default: `benchmark-comparison.md`).
334
+
335
+ ### Outputs
336
+
337
+ - `comparison_markdown`: The generated Markdown report content.
338
+ - `markdown_file_path`: Path to the saved Markdown file.
339
+
340
+ Refer to the `action/action.yml` file for the definitive inputs and description.
@@ -0,0 +1,44 @@
1
+ name: 'Generate and Compare Aztec Benchmarks'
2
+ description: 'Generates _latest benchmark reports for all contracts using `aztec-benchmark`, then compares them to _base reports and outputs markdown.'
3
+ inputs:
4
+ # Inputs for Comparison
5
+ threshold:
6
+ description: 'Regression threshold percentage (e.g., 2.5 for 2.5%). Changes above this are marked red.'
7
+ required: false
8
+ default: '2.5' # Default 2.5%
9
+ output_markdown_path:
10
+ description: 'Path where the comparison markdown report will be saved.'
11
+ required: false
12
+ default: 'benchmark-comparison.md'
13
+ base_suffix:
14
+ description: 'Suffix used to identify the base benchmark report files (e.g., contract_name<base_suffix>.benchmark.json).'
15
+ required: false
16
+ default: '_base'
17
+ current_suffix:
18
+ description: 'Suffix used for the current benchmark report files, generated by this action (e.g., contract_name<current_suffix>.benchmark.json).'
19
+ required: false
20
+ default: '_latest'
21
+ config_path:
22
+ description: 'Path to the Nargo.toml configuration file.'
23
+ required: false
24
+ default: './Nargo.toml'
25
+ reports_dir:
26
+ description: 'Directory where benchmark JSON reports are stored and will be generated.'
27
+ required: false
28
+ default: './benchmarks'
29
+ only_report:
30
+ description: 'If true, skips executing the aztec-benchmark CLI and just runs the comparison.'
31
+ required: false
32
+ default: 'false'
33
+ circuit_details:
34
+ description: 'If true, includes per-circuit gate count breakdown in an expandable section below each contract table.'
35
+ required: false
36
+ default: 'false'
37
+ outputs:
38
+ comparison_markdown:
39
+ description: "The generated comparison report in Markdown format."
40
+ markdown_file_path:
41
+ description: "The path to the generated markdown file."
42
+ runs:
43
+ using: 'node20'
44
+ main: 'dist/index.cjs' # Entry point is within the action directory, in the dist subfolder