odem-rs 0.3.0

Object-based Discrete-Event Modelling in Rust using async/await
Documentation
//! Very simple simulation scenario that simulates a clock ticking once every
//! second.
//!
//! This example is mostly interesting for its benchmark: measuring execution
//! speed essentially measures the costs of context switches, since the model
//! doesn't do anything other than that.

use odem_rs::prelude::*;

#[derive(Default, Config)]
#[time(Time<f64>)]
struct ClockSim;

#[odem_rs::main]
async fn clock(sim: &Sim<ClockSim>, duration: Time<f64>) -> Time<f64> {
	while sim.now() < duration {
		sim.advance(second::new(1.0)).await;
	}

	sim.now()
}

#[cfg(not(test))]
fn main() {
	println!("{}", clock(year::new(10.0)).display());
}

#[cfg(test)]
criterion::criterion_main!(bench::benches);

#[cfg(test)]
mod bench {
	use super::*;
	use criterion::{AxisScale, BenchmarkId, Criterion, PlotConfiguration, criterion_group};
	use std::time::Duration;

	const RANGE: u32 = 10;
	const STEP: f64 = 1000.0;

	fn clock_bench(c: &mut Criterion) {
		let mut group = c.benchmark_group("Clock");

		// set up the benchmark parameters
		group
			.confidence_level(0.99)
			.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic))
			.measurement_time(Duration::from_secs(5));

		// vary in the length of the simulation run
		for duration in (0..RANGE).map(|c| f64::from(1 << c) * STEP) {
			// benchmark the Rust implementation
			group.bench_function(BenchmarkId::new("clock", duration), |b| {
				b.iter(|| clock(minute::new(duration)))
			});
		}

		group.finish();
	}

	criterion_group!(benches, clock_bench);
}