#speed #future #race #benchmark #async

speedracer

A crate for racing futures and ranking the results

2 releases

0.1.2 Dec 11, 2022
0.1.1 Nov 18, 2022
0.1.0 Nov 17, 2022

#902 in Asynchronous

MIT license

10KB
144 lines

A crate for racing Futures and getting ranked results back.

Example

use tokio::time::sleep;
use std::time::Duration;

use speedracer::RaceTrack;

let mut race_track = RaceTrack::disqualify_after(Duration::from_millis(500));

race_track.add_racer("Racer #1", async move {
    println!("Racer #1 is starting");
    sleep(std::time::Duration::from_millis(100)).await;
    println!("Racer #1 is ending");

    Ok(())
});
race_track.add_racer("Racer #2", async move {
    println!("Racer #2 is starting");
    sleep(std::time::Duration::from_secs(200)).await;
    println!("Racer #2 is ending");

    Ok(())
});
race_track.add_racer("Racer #3", async move {
    println!("Racer #3 is starting");
    sleep(std::time::Duration::from_secs(700)).await;
    println!("Racer #3 is ending");

    Ok(())
});

race_track.run().await;
let rankings = race_track.rankings();

println!("Rankings: {:?}", rankings);

assert_eq!(rankings[0].name, "Racer #1");
assert_eq!(rankings[1].name, "Racer #2");
assert_eq!(rankings[2].name, "Racer #3");
assert_eq!(rankings[2].disqualified, true);

Dependencies

~2.8–8.5MB
~64K SLoC