robopoker/clustering/
progress.rs

1use tokio::time::Instant;
2
3/// A struct to track and display progress of a long-running operation.
4pub struct Progress {
5    total: usize,
6    check: usize,
7    ticks: usize,
8    begin: Instant,
9    delta: Instant,
10}
11impl Progress {
12    pub fn new(total: usize, n: usize) -> Self {
13        let check = total / n;
14        let now = Instant::now();
15        Self {
16            total,
17            check,
18            ticks: 0,
19            begin: now,
20            delta: now,
21        }
22    }
23    pub fn tick(&mut self) {
24        self.ticks += 1;
25        if self.ticks % self.check == 0 {
26            let now = Instant::now();
27            let total_t = now.duration_since(self.begin);
28            let delta_t = now.duration_since(self.delta);
29            self.delta = now;
30            log::info!(
31                "progress: {:8.0?} {:>10} {:6.2}%   mean {:6.0}   last {:6.0}",
32                total_t,
33                self.ticks,
34                self.ticks as f32 / self.total as f32 * 100f32,
35                self.ticks as f32 / total_t.as_secs_f32(),
36                self.check as f32 / delta_t.as_secs_f32(),
37            );
38        }
39    }
40}