The minimal starting point for authoring a perfscale library — a WASM
component that provides value-generating functions to ${alias.fn(...)}
tokens in perfscale test payloads (RFC 005). Requires perfscale ≥ v0.21.0.
Use this template: click Use this template → Create a new repository
on GitHub (or gh repo create my-lib --template Perfscale/library-template),
then clone your new repo.
src/lib.rs implements two functions demonstrating the two core concepts:
greet(name)→"hello, {name}!"— a plain functiontoken([key])→ a random 16-char id from the SDK's seeded PRNG; the optional key memoizes per message (${hello.token(order)}twice in one message = one value; no key = always fresh)
plus runtime-free unit tests (cargo test) driven by the SDK's test_call
harness — no wasm runtime needed.
- Rename the package in
Cargo.toml([package] name). The crate name becomes the default token alias (-→_) and must match[a-z][a-z0-9_]*. Or keep the crate name and setas: myaliasin YAML. - Add a function:
- add a
FunctionInfoentry infunctions()(setsecret: truefor credential-minting functions — the engine masks their results in logs) - add a match arm in
call(); useargs::string/int/floatto parse arguments,ctx.prng()for all randomness,ctx.memo(&key, f)for per-message reuse
- add a
- Configure it (optional): override
init()— the YAMLwith:block arrives there as JSON. ReturningErris fatal to the run.
$ rustup target add wasm32-wasip2
$ cargo build --release --target wasm32-wasip2(Rust ≥ 1.82 emits WASI Preview 2 components natively — no cargo-component.)
# config.yaml
vus: 1
duration: 10s
libraries:
- use: ./target/wasm32-wasip2/release/hello.wasm # path relative to this file
with: # optional, passed to init()
greeting: hi# test.yaml — ${...} tokens expand in action payloads (http/ws/gRPC/GraphQL)
steps:
- use: std/http@v1
with:
method: POST
url: https://httpbin.org/post
body:
greeting: "${hello.greet(world)}"
order_id: "${hello.token(order)}"(std/log@v1 resolves ${{ }} step outputs, not ${...} generator tokens.)
$ cd examples && perfscale lint test.yaml config.yaml
$ perfscale run -f test.yaml -c config.yamlSee examples/ for a runnable version (log + http steps).
- Errors: a known alias + unknown function (or bad arguments) fails the step — typos never slip silently into a payload. An unknown alias leaves the token verbatim.
- Randomness: draw everything from
ctx.prng()(seeded per instance);wasi:randomis never provided. AvoidHashMap(useVec/BTreeMap) andprintln!(wasi:cli/*is never granted). - Capabilities:
fs/clockaccess must be granted in YAML (capabilities: [clock]under thelibraries:entry, plusallow_library_capabilities: truein the config). Importing more than granted is a hard load error. - Determinism:
seed:in the config makes runs reproducible.
Full contract: perfscale-library-sdk docs.
MIT OR Apache-2.0, same as perfscale.