Skip to content

Commit

Permalink
Add speed test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Mast committed Jul 4, 2024
1 parent a0fffba commit 9ed3f9b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
4 changes: 4 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
74 changes: 74 additions & 0 deletions tests/speed_test.rs
Original file line number Diff line number Diff line change
@@ -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<Option<oneshot::Sender<()>>>,
}
impl asim::network::NodeData for NodeData {}

type Node = asim::network::Node<Message, NodeData>;

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<Message, NodeData> for NodeCallback {
async fn handle_message(&self, node: &Rc<Node>, _source: ObjectId, _message: Message) {
node.get_data()
.notifier
.borrow_mut()
.take()
.unwrap()
.send(())
.unwrap();
}
}

#[derive(Default)]
pub struct LinkCallback {}
impl asim::network::LinkCallback<Message, NodeData> 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));
}

0 comments on commit 9ed3f9b

Please sign in to comment.