Skip to content

feat: add credential performance benchmarks and optimize storage#111

Merged
Josie123-Dev merged 1 commit into
GuardZero144:mainfrom
nanlebenthel-web:feat/issue-100-credential-benchmarks
Jun 21, 2026
Merged

feat: add credential performance benchmarks and optimize storage#111
Josie123-Dev merged 1 commit into
GuardZero144:mainfrom
nanlebenthel-web:feat/issue-100-credential-benchmarks

Conversation

@nanlebenthel-web

Copy link
Copy Markdown
Contributor

Summary

Adds a performance benchmark suite for the credential (Verification) contract and fixes the storage scaling bottleneck the benchmarks revealed.

What's included

  • Benchmark suite (contracts/src/benchmarks.rs): measures per-operation budget cost (CPU instructions + memory bytes via env.budget()), execution time, and load behaviour. They run as normal cargo test tests and assert resource ceilings, so they double as regression guards.
  • Bottleneck identified: per-credential records were stored in instance storage, which serialises the contract's entire data set on every call → O(n) per op, O(n²) aggregate. The 100th credential cost ~51× the 1st.
  • Optimization: moved per-credential data to persistent storage, keyed individually via a typed DataKey enum, with TTL bumping; only the bounded counter stays in instance storage. Public API is unchanged.
  • Documentation (docs/PERFORMANCE.md): methodology, the native-host caveat, before/after results, and remaining recommendations. README points to it.

Measured impact (native test host)

Scenario Before (instance) After (persistent) Improvement
Create 100 credentials — CPU 291.5M 43.8M 6.6× cheaper
Create 100 credentials — memory 53.9M 13.6M 4.0× cheaper
Marginal cost 1st → 100th ~51× ~9× flatter scaling
get_verification read 69,886 51,869 −26%
is_verification_valid read 60,671 42,654 −30%

Note: figures come from the native test host, which under-estimates Wasm and adds an O(n) snapshot artifact to batch runs; on-chain the persistent path is O(1) per op. See docs/PERFORMANCE.md.

How to run

cd contracts
cargo test --features testutils benchmarks -- --nocapture --test-threads=1

Test plan

  • cargo test --features testutils — all 78 tests pass (incl. 5 benchmarks + existing suite)
  • cargo fmt -- --check clean
  • cargo clippy --all-targets -- -D warnings clean
  • cargo build --release --target wasm32-unknown-unknown succeeds
  • Existing integration/upgrade tests unchanged in behaviour (storage migration is API-transparent; tracked test snapshots updated accordingly)

Acceptance criteria

  • Gas is measured (budget CPU/memory per op)
  • Time is benchmarked (per-op + load)
  • Load is tested (N=10/50/100, 1st-vs-100th)
  • Bottlenecks are optimized (instance → persistent storage)
  • Performance is documented (docs/PERFORMANCE.md)

Closes #100

…torage

Add a benchmark suite for the credential (Verification) contract and fix the
storage scaling bottleneck it surfaced.

- contracts/src/benchmarks.rs: measure per-operation budget (CPU instructions /
  memory bytes via env.budget()), execution time, and load behaviour. The
  benchmarks run as ordinary `cargo test` tests and assert resource ceilings, so
  they double as regression guards.
- Bottleneck found: per-credential records were stored in instance storage,
  which serialises the entire contract data set on every call — O(n) per op,
  O(n^2) in aggregate (the 100th credential cost ~51x the 1st).
- Optimization: move per-credential data to persistent storage keyed
  individually via a typed DataKey enum, with TTL bumping; keep only the bounded
  counter in instance storage. Public API is unchanged. N=100 batch cost drops
  6.6x (CPU) and 4.0x (memory); reads get 26-30% cheaper.
- docs/PERFORMANCE.md: methodology, before/after results, and remaining
  recommendations. README points to it.

Closes GuardZero144#100
@vercel

vercel Bot commented Jun 21, 2026

Copy link
Copy Markdown

@nanlebenthel-web is attempting to deploy a commit to the Josie's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Josie123-Dev Josie123-Dev merged commit 97ddf80 into GuardZero144:main Jun 21, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Hard] Add credential performance benchmarks

2 participants