tpchgen_cli/
statistics.rs

1//! Statistics reporter for TPCH data generation.
2
3use log::{debug, info};
4use std::time::Instant;
5
6/// Statistics for writing data to a file
7///
8/// Reports the statistics on drop
9#[derive(Clone, Debug)]
10pub struct WriteStatistics {
11    /// Time at which the writer was created
12    start: Instant,
13    /// User defined "chunks" (e.g. buffers or row_groups)
14    num_chunks: usize,
15    chunk_label: String,
16    /// total bytes written
17    num_bytes: usize,
18}
19
20impl WriteStatistics {
21    /// Create a new statistics reporter
22    pub fn new(chunk_label: impl Into<String>) -> Self {
23        Self {
24            start: Instant::now(),
25            num_chunks: 0,
26            chunk_label: chunk_label.into(),
27            num_bytes: 0,
28        }
29    }
30
31    /// Increment chunk count
32    pub fn increment_chunks(&mut self, num_chunks: usize) {
33        self.num_chunks += num_chunks;
34    }
35
36    /// Increment byte count
37    pub fn increment_bytes(&mut self, num_bytes: usize) {
38        self.num_bytes += num_bytes;
39    }
40}
41
42impl Drop for WriteStatistics {
43    fn drop(&mut self) {
44        let duration = self.start.elapsed();
45        let mb_per_chunk = self.num_bytes as f64 / (1024.0 * 1024.0) / self.num_chunks as f64;
46        let bytes_per_second = (self.num_bytes as f64 / duration.as_secs_f64()) as u64;
47        let gb_per_second = bytes_per_second as f64 / (1024.0 * 1024.0 * 1024.0);
48        let gb = self.num_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
49
50        info!("Created {gb:.02} GB in {duration:?} ({gb_per_second:.02} GB/sec)");
51        debug!(
52            "Wrote {} bytes in {} {}  {mb_per_chunk:.02} MB/{}",
53            self.num_bytes, self.num_chunks, self.chunk_label, self.chunk_label
54        );
55    }
56}