Skip to content

Commit b656c7d

Browse files
authored
Benchmarking Compute Units (#4)
1 parent 82d4cfb commit b656c7d

File tree

9 files changed

+149
-3
lines changed

9 files changed

+149
-3
lines changed

.github/workflows/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ jobs:
109109
- name: Test Programs
110110
run: pnpm programs:test
111111

112+
bench_program_compute_units:
113+
name: Benchmark Program Compute Units
114+
runs-on: ubuntu-latest
115+
needs: build_programs # Cargo Bench won't build the SBPF binary...
116+
steps:
117+
- name: Git Checkout
118+
uses: actions/checkout@v4
119+
120+
- name: Setup Environment
121+
uses: ./.github/actions/setup
122+
with:
123+
cargo-cache-key: cargo-program-benches
124+
cargo-cache-fallback-key: cargo-programs
125+
solana: true
126+
127+
- name: Restore Program Builds
128+
uses: actions/cache/restore@v4
129+
with:
130+
path: ./**/*.so
131+
key: ${{ runner.os }}-builds-${{ github.sha }}
132+
133+
- name: Benchmark Compute Units
134+
run: pnpm programs:bench
135+
136+
- name: Check Working Directory
137+
run: |
138+
if [ -n "$(git status --porcelain)" ]; then
139+
test -z "$(git status --porcelain)"
140+
echo "CU usage has changed. Please run `cargo bench` and commit the new results.";
141+
exit 1;
142+
fi
143+
112144
generate_idls:
113145
name: Check IDL Generation
114146
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"scripts": {
44
"programs:build": "zx ./scripts/program/build.mjs",
55
"programs:test": "zx ./scripts/program/test.mjs",
6+
"programs:bench": "zx ./scripts/program/bench.mjs",
67
"programs:clean": "zx ./scripts/program/clean.mjs",
78
"programs:format": "zx ./scripts/program/format.mjs",
89
"programs:lint": "zx ./scripts/program/lint.mjs",

program/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ spl-program-error = "0.5.0"
2323

2424
[dev-dependencies]
2525
mollusk-svm = { version = "0.0.5", features = ["fuzz"] }
26+
mollusk-svm-bencher = "0.0.5"
2627
solana-sdk = "2.0.1"
2728

2829
[lib]
2930
crate-type = ["cdylib", "lib"]
31+
32+
[[bench]]
33+
name = "compute_units"
34+
harness = false
35+
36+
[lints.rust.unexpected_cfgs]
37+
level = "warn"
38+
check-cfg = [
39+
'cfg(target_os, values("solana"))',
40+
]

program/benches/compute_units.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#### Compute Units: 2024-10-22 17:38:11.836151 UTC
2+
3+
| Name | CUs | Delta |
4+
|------|------|-------|
5+
| revoke_pending_activation | 2781 | - new - |
6+

program/benches/compute_units.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Feature Gate program compute unit benchmark testing.
2+
3+
use {
4+
mollusk_svm::{program::keyed_account_for_system_program, Mollusk},
5+
mollusk_svm_bencher::{Bench, MolluskComputeUnitBencher},
6+
solana_feature_gate_program::instruction::revoke_pending_activation,
7+
solana_sdk::{account::AccountSharedData, feature::Feature, incinerator, pubkey::Pubkey},
8+
};
9+
10+
fn main() {
11+
std::env::set_var("SBF_OUT_DIR", "../target/deploy");
12+
let mollusk = Mollusk::new(&solana_sdk::feature::id(), "solana_feature_gate_program");
13+
14+
let feature = Pubkey::new_unique();
15+
16+
let bench: Bench = (
17+
"revoke_pending_activation",
18+
&revoke_pending_activation(&feature),
19+
&[
20+
(
21+
feature,
22+
AccountSharedData::new_data(
23+
42,
24+
&Feature { activated_at: None },
25+
&solana_sdk::feature::id(),
26+
)
27+
.unwrap(),
28+
),
29+
(incinerator::id(), AccountSharedData::default()),
30+
keyed_account_for_system_program(),
31+
],
32+
);
33+
34+
MolluskComputeUnitBencher::new(mollusk)
35+
.bench(bench)
36+
.must_pass(true)
37+
.out_dir("./benches")
38+
.execute();
39+
}

scripts/program/bench.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import {
4+
cliArguments,
5+
getProgramFolders,
6+
workingDirectory,
7+
} from '../utils.mjs';
8+
9+
// Save external programs binaries to the output directory.
10+
import './dump.mjs';
11+
12+
// Configure additional arguments here, e.g.:
13+
// ['--arg1', '--arg2', ...cliArguments()]
14+
const benchArgs = cliArguments();
15+
16+
const hasSolfmt = await which('solfmt', { nothrow: true });
17+
18+
// Test the programs.
19+
await Promise.all(
20+
getProgramFolders().map(async (folder) => {
21+
const manifestPath = path.join(workingDirectory, folder, 'Cargo.toml');
22+
23+
if (hasSolfmt) {
24+
await $`RUST_LOG=error cargo bench --manifest-path ${manifestPath} ${benchArgs} 2>&1 | solfmt`;
25+
} else {
26+
await $`RUST_LOG=error cargo bench --manifest-path ${manifestPath} ${benchArgs}`;
27+
}
28+
})
29+
);

scripts/program/build.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import './dump.mjs';
1111

1212
// Configure additional arguments here, e.g.:
1313
// ['--arg1', '--arg2', ...cliArguments()]
14-
const buildArgs = cliArguments();
14+
const buildArgs = [
15+
'--features',
16+
'bpf-entrypoint',
17+
...cliArguments()
18+
];
1519

1620
// Build the programs.
1721
await Promise.all(

scripts/program/lint.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
// ['--arg1', '--arg2', ...cliArguments()]
1313
const lintArgs = [
1414
'-Zunstable-options',
15-
'--features',
16-
'bpf-entrypoint,test-sbf',
15+
'--all-targets',
16+
'--all-features',
1717
'--',
1818
'--deny=warnings',
1919
'--deny=clippy::arithmetic_side_effects',

0 commit comments

Comments
 (0)