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
use chrono::{DateTime, Local};

use crate::utils::time_util::print_each_time;

pub struct Bencher {
    total: i32,
    now: i64,
}

impl Bencher {
    pub fn new(total: i32) -> Self {
        return Self {
            total,
            now: 0,
        };
    }

    pub fn iter(&mut self, func: fn()) {
        let mut current = 0;
        self.now = Local::now().timestamp_millis();
        loop {
            func();
            if current == self.total - 1 {
                let end = Local::now().timestamp_millis();
                use_time(self.total, self.now, end);
                use_tps(self.total, self.now, end);
                break;
            } else {
                current = current + 1;
            }
        }
    }
}

fn use_tps(total: i32, start: i64, end: i64) {
    let mut time = (end - start) as f64;
    time = time / 1000.0;
    println!("use TPS: {} TPS/s", total as f64 / time);
}

//计算每个操作耗时nano纳秒
fn use_time(total: i32, start: i64, end: i64) {
    let mut time = (end - start) as f64;
    time = time / 1000.0;
    println!("use Time: {} s,each:{} nano/op", time, time * 1000000000.0 / (total as f64));
}