Skip to content

Commit

Permalink
Benchmarking Compute Units (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Oct 25, 2024
1 parent 82d4cfb commit b656c7d
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 3 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ jobs:
- name: Test Programs
run: pnpm programs:test

bench_program_compute_units:
name: Benchmark Program Compute Units
runs-on: ubuntu-latest
needs: build_programs # Cargo Bench won't build the SBPF binary...
steps:
- name: Git Checkout
uses: actions/checkout@v4

- name: Setup Environment
uses: ./.github/actions/setup
with:
cargo-cache-key: cargo-program-benches
cargo-cache-fallback-key: cargo-programs
solana: true

- name: Restore Program Builds
uses: actions/cache/restore@v4
with:
path: ./**/*.so
key: ${{ runner.os }}-builds-${{ github.sha }}

- name: Benchmark Compute Units
run: pnpm programs:bench

- name: Check Working Directory
run: |
if [ -n "$(git status --porcelain)" ]; then
test -z "$(git status --porcelain)"
echo "CU usage has changed. Please run `cargo bench` and commit the new results.";
exit 1;
fi
generate_idls:
name: Check IDL Generation
runs-on: ubuntu-latest
Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"programs:build": "zx ./scripts/program/build.mjs",
"programs:test": "zx ./scripts/program/test.mjs",
"programs:bench": "zx ./scripts/program/bench.mjs",
"programs:clean": "zx ./scripts/program/clean.mjs",
"programs:format": "zx ./scripts/program/format.mjs",
"programs:lint": "zx ./scripts/program/lint.mjs",
Expand Down
11 changes: 11 additions & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ spl-program-error = "0.5.0"

[dev-dependencies]
mollusk-svm = { version = "0.0.5", features = ["fuzz"] }
mollusk-svm-bencher = "0.0.5"
solana-sdk = "2.0.1"

[lib]
crate-type = ["cdylib", "lib"]

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

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(target_os, values("solana"))',
]
6 changes: 6 additions & 0 deletions program/benches/compute_units.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#### Compute Units: 2024-10-22 17:38:11.836151 UTC

| Name | CUs | Delta |
|------|------|-------|
| revoke_pending_activation | 2781 | - new - |

39 changes: 39 additions & 0 deletions program/benches/compute_units.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Feature Gate program compute unit benchmark testing.
use {
mollusk_svm::{program::keyed_account_for_system_program, Mollusk},
mollusk_svm_bencher::{Bench, MolluskComputeUnitBencher},
solana_feature_gate_program::instruction::revoke_pending_activation,
solana_sdk::{account::AccountSharedData, feature::Feature, incinerator, pubkey::Pubkey},
};

fn main() {
std::env::set_var("SBF_OUT_DIR", "../target/deploy");
let mollusk = Mollusk::new(&solana_sdk::feature::id(), "solana_feature_gate_program");

let feature = Pubkey::new_unique();

let bench: Bench = (
"revoke_pending_activation",
&revoke_pending_activation(&feature),
&[
(
feature,
AccountSharedData::new_data(
42,
&Feature { activated_at: None },
&solana_sdk::feature::id(),
)
.unwrap(),
),
(incinerator::id(), AccountSharedData::default()),
keyed_account_for_system_program(),
],
);

MolluskComputeUnitBencher::new(mollusk)
.bench(bench)
.must_pass(true)
.out_dir("./benches")
.execute();
}
29 changes: 29 additions & 0 deletions scripts/program/bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env zx
import 'zx/globals';
import {
cliArguments,
getProgramFolders,
workingDirectory,
} from '../utils.mjs';

// Save external programs binaries to the output directory.
import './dump.mjs';

// Configure additional arguments here, e.g.:
// ['--arg1', '--arg2', ...cliArguments()]
const benchArgs = cliArguments();

const hasSolfmt = await which('solfmt', { nothrow: true });

// Test the programs.
await Promise.all(
getProgramFolders().map(async (folder) => {
const manifestPath = path.join(workingDirectory, folder, 'Cargo.toml');

if (hasSolfmt) {
await $`RUST_LOG=error cargo bench --manifest-path ${manifestPath} ${benchArgs} 2>&1 | solfmt`;
} else {
await $`RUST_LOG=error cargo bench --manifest-path ${manifestPath} ${benchArgs}`;
}
})
);
6 changes: 5 additions & 1 deletion scripts/program/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import './dump.mjs';

// Configure additional arguments here, e.g.:
// ['--arg1', '--arg2', ...cliArguments()]
const buildArgs = cliArguments();
const buildArgs = [
'--features',
'bpf-entrypoint',
...cliArguments()
];

// Build the programs.
await Promise.all(
Expand Down
4 changes: 2 additions & 2 deletions scripts/program/lint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
// ['--arg1', '--arg2', ...cliArguments()]
const lintArgs = [
'-Zunstable-options',
'--features',
'bpf-entrypoint,test-sbf',
'--all-targets',
'--all-features',
'--',
'--deny=warnings',
'--deny=clippy::arithmetic_side_effects',
Expand Down

0 comments on commit b656c7d

Please sign in to comment.