mybatis_sql/
bencher.rs

1pub trait QPS {
2    fn qps(&self, total: u64);
3    fn time(&self, total: u64);
4    fn cost(&self);
5}
6
7impl QPS for std::time::Instant {
8    fn qps(&self, total: u64) {
9        let time = self.elapsed();
10        println!(
11            "use QPS: {} QPS/s",
12            (total as u128 * 1000000000 as u128 / time.as_nanos() as u128)
13        );
14    }
15
16    fn time(&self, total: u64) {
17        let time = self.elapsed();
18        println!(
19            "use Time: {:?} ,each:{} ns/op",
20            &time,
21            time.as_nanos() / (total as u128)
22        );
23    }
24
25    fn cost(&self) {
26        let time = self.elapsed();
27        println!("cost:{:?}", time);
28    }
29}
30
31#[macro_export]
32macro_rules! bench {
33    ($total:expr,$body:block) => {{
34        use mybatis_sql::bencher::QPS;
35        let now = std::time::Instant::now();
36        for _ in 0..$total {
37            $body;
38        }
39        now.time($total);
40        now.qps($total);
41    }};
42}