Skip to main content

Crate mollusk_svm_bencher

Crate mollusk_svm_bencher 

Source
Expand description

The Mollusk Compute Unit Bencher can be used to benchmark the compute unit usage of Solana programs. It provides a simple API for developers to write benchmarks for their programs, or compare multiple implementations of their programs in a matrix, which can be checked while making changes to the program.

A markdown file is generated, which captures all of the compute unit benchmarks. In the case of single program if a benchmark has a previous value, the delta is also recorded. This can be useful for developers to check the implications of changes to the program on compute unit usage.

use {
    mollusk_svm_bencher::MolluskComputeUnitBencher,
    mollusk_svm::Mollusk,
    /* ... */
};

// Optionally disable logging.
solana_logger::setup_with("");

/* Instruction & accounts setup ... */

let mollusk = Mollusk::new(&program_id, "my_program");

MolluskComputeUnitBencher::new(mollusk)
    .bench(("bench0", &instruction0, &accounts0))
    .bench(("bench1", &instruction1, &accounts1))
    .bench(("bench2", &instruction2, &accounts2))
    .bench(("bench3", &instruction3, &accounts3))
    .must_pass(true)
    .out_dir("../target/benches")
    .execute();

The must_pass argument can be provided to trigger a panic if any defined benchmark tests do not pass. out_dir specifies the directory where the markdown file will be written.

Developers can invoke this benchmark test with cargo bench. They may need to add a bench to the project’s Cargo.toml.

[[bench]]
name = "compute_units"
harness = false

The markdown file will contain entries according to the defined benchmarks.

| Name   | CUs   | Delta  |
|--------|-------|--------|
| bench0 | 450   | --     |
| bench1 | 579   | -129   |
| bench2 | 1,204 | +754   |
| bench3 | 2,811 | +2,361 |

§Matrix Benchmarking

If you want to compare multiple program implementations (e.g., comparing an optimized version against a baseline), use MolluskComputeUnitMatrixBencher. This generates a table where each program is a column.

use {
    mollusk_svm_bencher::MolluskComputeUnitMatrixBencher,
    mollusk_svm::Mollusk,
    /* ... */
};

/* Instruction & accounts setup ... */

let mollusk = Mollusk::new(&program_id, "program_v1");

MolluskComputeUnitMatrixBencher::new(mollusk)
    .programs(&["program_v1", "program_v2", "program_v3"])
    .bench(("bench0", &instruction0, &accounts0))
    .bench(("bench1", &instruction1, &accounts1))
    .must_pass(true)
    .out_dir("../target/benches")
    .execute();

The matrix markdown file will contain entries comparing all provided programs.

| Name     | CU (`program_v1`) | CU (`program_v2`) | CU (`program_v3`) |
|----------|-------------------|-------------------|-------------------|
| `bench0` | 1,400             | 1,390             | 1,385             |
| `bench1` | 2,100             | 2,050             | 2,045             |

Modules§

result
Compute unit benchmarking results and checks.

Structs§

MolluskComputeUnitBencher
Mollusk’s compute unit bencher.
MolluskComputeUnitMatrixBencher
Mollusk’s matrix compute unit bencher.

Functions§

get_solana_version

Type Aliases§

Bench
A bench is a tuple of a name, an instruction, and a list of accounts.