-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.rs
138 lines (117 loc) · 3.61 KB
/
benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Simple benchmark test, test ping time and duplex bandwidth
use tokio::time::timeout;
use crazyflie_link::{LinkContext, Packet};
use futures::Future;
use std::{
ops::Div,
time::{Duration, Instant},
};
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "bandwidth")]
struct Opt {
#[structopt(short, default_value = "1000")]
n_packets: u32,
#[structopt(short, default_value = "28")]
size_packet: usize,
#[structopt(name = "URI")]
link_uri: String,
}
async fn bench<F: Future<Output = anyhow::Result<()>>>(function: F) -> anyhow::Result<Duration> {
let start = Instant::now();
function.await?;
Ok(Instant::now() - start)
}
async fn purge_crazyflie_queues(link: &crazyflie_link::Connection) {
// Purge crazyflie queues
loop {
if timeout(std::time::Duration::from_millis(10), link.recv_packet())
.await
.is_err()
{
break;
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
let link_context = LinkContext::new();
let link = link_context.open_link(&opt.link_uri).await?;
println!("Test ping:");
purge_crazyflie_queues(&link).await;
let mut ping_times = Vec::new();
for i in 0..opt.n_packets {
let mut packet = Packet::new(0xF, 0, vec![i as u8]);
let ping_time = bench(async {
link.send_packet(packet).await?;
loop {
packet = timeout(std::time::Duration::from_secs(10), link.recv_packet())
.await?
.unwrap();
if packet.get_header() == 0xf0 {
break;
}
}
let data = packet.get_data();
if data[0] != (i as u8) {
println!(
"Communication error! Expected {}, received {}.",
i as u8, data[0]
);
panic!();
}
Ok(())
})
.await?;
ping_times.push(ping_time);
}
let ping_mean = ping_times
.iter()
.fold(Duration::from_nanos(0), |acc, v| acc + *v)
.div(ping_times.len() as u32);
let min_ping = ping_times.iter().min().unwrap();
let max_ping = ping_times.iter().max().unwrap();
println!(
"Mean ping time: {:?}, min: {:?}, max: {:?}",
ping_mean, min_ping, max_ping
);
println!("Test bandwidth:");
purge_crazyflie_queues(&link).await;
let runtime = bench(async {
for i in (0..opt.n_packets).into_iter() {
let packet = Packet::new(0xF, 0, vec![i as u8]);
link.send_packet(packet).await?;
}
for i in (0..opt.n_packets).into_iter() {
let mut packet;
loop {
packet = timeout(std::time::Duration::from_secs(10), link.recv_packet())
.await?
.unwrap();
if packet.get_header() == 0xf0 {
break;
}
}
let data = packet.get_data();
if data[0] != (i as u8) {
println!(
"Communication error! Expected {}, received {}.",
i as u8, data[0]
);
panic!();
}
}
Ok(())
})
.await?;
let packet_rate = (opt.n_packets as f64) / runtime.as_secs_f64();
let bandwidth = packet_rate * (opt.size_packet as f64);
println!(
"Runtime: {:?}, packet rate: {:.4} pk/s, bandwidth: {:.4} kB/s",
runtime,
packet_rate,
bandwidth / 1024.
);
Ok(())
}