xmachines-runtime 6.0.0

XState v6 semantics in Rust: a port of XState's interpreter and actor runtime; not affiliated with Stately
Documentation
//! A loop of `Actor::persist`, for profilers:
//!   cargo build --release -p xmachines-runtime --example persist_loop
//!   valgrind --tool=callgrind target/release/examples/persist_loop

// A profiler's loop reads its count of the command line, times itself and prints; the
// crate's own code may not (clippy.toml).
#![allow(clippy::disallowed_macros, clippy::disallowed_methods, clippy::disallowed_types)]

use std::sync::Arc;

use serde_json::{json, Value};
use xmachines_runtime::{Actor, Machine, Registry};

fn main() {
    let definition = json!({
        "id": "order",
        "initial": "open",
        "context": { "items": 0, "customer": "c-1", "tags": ["web", "eu"] },
        "states": {
            "open": {
                "on": { "add": { "context": { "items": 1 } }, "pay": { "target": "paying" } },
                "after": { "86400000": { "target": "expired" } }
            },
            "paying": { "on": { "ok": { "target": "paid" } } },
            "paid": { "type": "final" },
            "expired": { "type": "final" }
        }
    });
    let n: u32 = std::env::args().nth(1).and_then(|a| a.parse().ok()).unwrap_or(20_000);
    let machine: Machine<Value, Value> = Machine::from_json(&definition, &Registry::new()).unwrap();
    let mut actor = Actor::start(Arc::new(machine), 0).unwrap();
    actor.send_json(json!({ "type": "add" }), 1).unwrap();
    let started = std::time::Instant::now();
    let mut size = 0;
    for _ in 0..n {
        size += actor.persist().as_object().map_or(0, |o| o.len());
    }
    println!("{n} persists, {:?} each ({size})", started.elapsed() / n);
}