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
#[macro_use]
extern crate log;

use std::time::{Duration, Instant};

#[derive(Debug, PartialEq)]
pub enum Precision {
    Millisecond,
    Microsecond,
    Nanosecond,
}

#[derive(Debug, PartialEq)]
pub enum Output {
    StdOut,
    Log(log::Level),
}

#[derive(Debug, PartialEq)]
pub enum Report {
    Always,
    Never,
    Gt(Duration), // total time >  durtaion
    Ge(Duration), // total time >= durtaion
    Lt(Duration), // total time <  durtaion
    Le(Duration), // total time <= durtaion
}

#[derive(Debug)]
pub struct Tag {
    pub since_begin: Duration,
    pub since_prev:  Duration,
    pub tag: String,
}

#[derive(Debug)]
pub struct HowMuch {
    total: Instant,
    diff:  Instant,
    output: Output,
    precision: Precision,
    report: Report,
    tags: Vec<Tag>,
}

impl HowMuch {
    pub fn new() -> HowMuch {
        let mut hm = HowMuch {
            total: Instant::now(),
            diff:  Instant::now(),
            output: Output::StdOut,
            precision: Precision::Microsecond,
            report: Report::Always,
            tags: Vec::new(),
        };
        hm.tag("BEGIN");
        hm
    }

    pub fn tag(&mut self, tag: &str) {
        let total = self.total.elapsed();
        let diff  = self.diff.elapsed();
        self.diff = Instant::now();

        self.tags.push(Tag{
            since_begin: total,
            since_prev: diff,
            tag: tag.to_owned()
        })
    }

    pub fn set_output(&mut self, output: Output) {
        self.output = output;
    }

    pub fn set_precision(&mut self, precision: Precision) {
        self.precision = precision;
    }

    pub fn set_report(&mut self, report: Report) {
        self.report = report;
    }
}

impl Drop for HowMuch {
    fn drop(&mut self) {
        self.tag("END");

        let to_precision = |v| {
            match self.precision {
                Precision::Millisecond => format!("{:03}", v / 1_000_000),
                Precision::Microsecond => format!("{:06}", v / 1_000),
                Precision::Nanosecond  => format!("{:09}", v),
            }
        };

        let report = || {
            // FIXME: ugly formating

            for tag in &self.tags {
                let record = format!("{:>4}.{} | {:>4}.{} | {}",
                    tag.since_begin.as_secs(),
                    to_precision(tag.since_begin.subsec_nanos()),
                    tag.since_prev.as_secs(),
                    to_precision(tag.since_prev.subsec_nanos()),
                    tag.tag);

                match self.output {
                    Output::StdOut => println!("{}", &record),
                    Output::Log(ref lvl) => log!(lvl.clone(), "{}", &record),
                }
            }
        };

        let mut total = self.total.elapsed();

        if let Some(tag) = self.tags.last() {
            total = tag.since_begin.clone();
        }

        match self.report {
            Report::Always => report(),
            Report::Gt(ref v) if &total >  v => report(),
            Report::Ge(ref v) if &total >= v => report(),
            Report::Lt(ref v) if &total <  v => report(),
            Report::Le(ref v) if &total <= v => report(),
            _ => {}
        }
    }
}