From 9ed3f9b7a008173942ee411ddcfb393b289284f7 Mon Sep 17 00:00:00 2001 From: Kai Mast Date: Thu, 4 Jul 2024 11:55:52 +0200 Subject: [PATCH] Add speed test --- .github/workflows/ci.yml | 10 +++--- README.md | 7 ++++ src/network/mod.rs | 4 +++ tests/speed_test.rs | 74 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 tests/speed_test.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8753190..26fb8e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,19 +14,19 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Install rustc and clippy nightly + - name: "Install rustc and clippy nightly" uses: dtolnay/rust-toolchain@stable with: toolchain: nightly-2024-07-03 # this has to match rust-toolchain.toml components: cargo, rustc, clippy, rustfmt - - name: Setup just + - name: "Setup just" run: cargo install just - - name: Setup rust dependency caching + - name: "Setup rust dependency caching" uses: Swatinem/rust-cache@v2 - - name: Unit Tests + - name: "Unit Tests" run: just test timeout-minutes: 5 - name: "Lint checks" - run: just lint-cmd + run: just lint - name: "Formatting checks" run: cargo fmt --check diff --git a/README.md b/README.md index d5d4a3b..c9ad099 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ This is achieved by providing an API similar to that of the standard library or This crate provides the simulator itself, a timer that allows an asynchronous task to sleep, and synchronization primitives. Additionally, it includes basic primitives for processes and links that can be used to jump start your process. +## Usage +Take a look at the tests and examples to get an idea how this library is intended to be used. + +Feel free to open an issue if some parts of this crate lack documentation. + ## Project Status This project is still in early development and many parts of the API are subject to change. @@ -22,3 +27,5 @@ The following crates that also implement event simulation exist. Both of them ar * [sim](https://docs.rs/sim/latest/sim/) * [desim](https://docs.rs/desim/latest/desim/) + +I am sure this list is incomplete. Please let me know of any other crates, and I will extend it. diff --git a/src/network/mod.rs b/src/network/mod.rs index a9859c0..161e7d7 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -21,6 +21,10 @@ pub type Latency = Duration; pub struct Bandwidth(u64); impl Bandwidth { + pub fn from_megabytes_per_second(mbps: u64) -> Self { + Self::from_megabits_per_second(8 * mbps) + } + pub fn from_megabits_per_second(mbps: u64) -> Self { Self(mbps * 1024 * 1024) } diff --git a/tests/speed_test.rs b/tests/speed_test.rs new file mode 100644 index 0000000..8ccb3f1 --- /dev/null +++ b/tests/speed_test.rs @@ -0,0 +1,74 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use asim::network::{Bandwidth, Latency, NetworkMessage, ObjectId}; +use asim::sync::oneshot; +use asim::time::Duration; + +#[derive(Clone, Debug)] +struct Message {} + +struct NodeCallback {} + +struct NodeData { + notifier: RefCell>>, +} +impl asim::network::NodeData for NodeData {} + +type Node = asim::network::Node; + +impl NetworkMessage for Message { + /// Every message is 1kb + fn get_size(&self) -> u64 { + 20 * 1024 * 1024 + } +} + +#[async_trait::async_trait(?Send)] +impl asim::network::NodeCallback for NodeCallback { + async fn handle_message(&self, node: &Rc, _source: ObjectId, _message: Message) { + node.get_data() + .notifier + .borrow_mut() + .take() + .unwrap() + .send(()) + .unwrap(); + } +} + +#[derive(Default)] +pub struct LinkCallback {} +impl asim::network::LinkCallback for LinkCallback {} + +#[asim::test] +async fn main() { + let latency = Latency::from_seconds(3); + let bandwidth = Bandwidth::from_megabytes_per_second(2); + + let (notify_sender, notify_receiver) = oneshot::channel(); + + let sender_data = NodeData { + notifier: Default::default(), + }; + let receiver_data = NodeData { + notifier: RefCell::new(Some(notify_sender)), + }; + + // Create two nodes and connect them + let sender = Node::new(bandwidth, sender_data, Box::new(NodeCallback {})); + let receiver = Node::new(bandwidth, receiver_data, Box::new(NodeCallback {})); + + Node::connect(sender.clone(), receiver, latency, Box::new(LinkCallback {})); + + let start = asim::time::now(); + sender.broadcast(Message {}, None); + + notify_receiver.await.unwrap(); + + let elapsed = asim::time::now() - start; + + // Transfer should take 10 seconds + // and latency adds another 3 seconds + assert_eq!(elapsed, Duration::from_seconds(13)); +}