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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#[macro_use]
extern crate lazy_static;

use std::sync::Mutex;
use std::collections::HashMap;
use std::collections::hash_map::{Entry};

struct Profile {
    times: HashMap<Vec<&'static str>, std::time::Duration>,
    stack: Vec<&'static str>,
    started: std::time::Instant,
}

fn add_to_map<K: std::hash::Hash + std::cmp::Eq>(m: &mut HashMap<K, std::time::Duration>,
                                                 k: K, d: std::time::Duration) {
    match m.entry(k) {
        Entry::Occupied(mut o) => {
            *o.get_mut() += d;
        },
        Entry::Vacant(v) => {
            v.insert(d);
        },
    }
}

impl Profile {
    fn new() -> Profile {
        Profile {
            times: HashMap::new(),
            started: std::time::Instant::now(),
            stack: Vec::new(),
        }
    }
    fn add_time(&mut self, now: std::time::Instant) {
        if now > self.started {
            let d = now.duration_since(self.started);
            add_to_map(&mut self.times, self.stack.clone(), d);
        }
    }
}

lazy_static! {
    static ref PROFILE: Mutex<Profile> = Mutex::new(Profile::new());
}

/// A `Guard` causes a task to end when it is dropped.
pub struct Guard {
}

impl Drop for Guard {
    fn drop(&mut self) {
        let now = std::time::Instant::now();
        let mut m = PROFILE.lock().unwrap();
        m.add_time(now);
        m.stack.pop();
    }
}

/// Push a task to the stack of tasks.  The task will continue until
/// the `Guard` is dropped.
pub fn push(task: &'static str) -> Guard {
    let now = std::time::Instant::now();
    let mut m = PROFILE.lock().unwrap();
    m.add_time(now);
    m.stack.push(task);
    Guard {}
}

/// Replace the last task pushed with a new one.
pub fn replace(task: &'static str) -> Guard {
    let now = std::time::Instant::now();
    let mut m = PROFILE.lock().unwrap();
    m.add_time(now);
    m.stack = vec![task];
    Guard {}
}

/// Forget any prior timings.
pub fn clear() {
    let mut m = PROFILE.lock().unwrap();
    m.times = HashMap::new();
    m.stack = Vec::new();
    m.started = std::time::Instant::now();
}

fn pretty_stack(v: &Vec<&'static str>) -> String {
    let mut out = String::new();
    for s in v {
        out.push_str(s);
        out.push_str(":");
    }
    out
}

fn duration_to_f64(t: std::time::Duration) -> f64 {
    t.as_secs() as f64 + (t.subsec_nanos() as f64)*1e-9
}

/// Create a string that holds a report of time used.
pub fn report() -> String {
    let now = std::time::Instant::now();
    let mut m = PROFILE.lock().unwrap();
    m.add_time(now);
    let mut out = String::new();
    let mut total_time = std::time::Duration::from_secs(0);
    for &v in m.times.values() {
        total_time += v;
    }
    let mut keys: Vec<_> = m.times.keys().collect();
    keys.sort();
    let mut cum: HashMap<&'static str, std::time::Duration> = HashMap::new();
    for &k in keys.iter() {
        for &s in k.iter() {
            add_to_map(&mut cum, s, m.times[k]);
        }
    }
    let mut shortkeys: Vec<_> = cum.keys().collect();
    shortkeys.sort();
    let total_f64 = duration_to_f64(total_time);
    for s in shortkeys {
        let percent = 100.0*duration_to_f64(cum[s])/total_f64;
        out.push_str(&format!("{:.1}% {}: {}\n", percent, &s, duration_to_f64(cum[s])));
        let mut ways: HashMap<Vec<&'static str>, std::time::Duration> = HashMap::new();
        for &k in keys.iter().filter(|&k| k.contains(s)) {
            let mut vv = Vec::from(k.split(|&ss| ss == *s).next().unwrap());
            vv.push(s);
            add_to_map(&mut ways, vv, m.times[k]);
        }
        let mut waykeys: Vec<_> = ways.keys().collect();
        waykeys.sort();
        if waykeys.len() > 1 {
            for &k in waykeys.iter().filter(|&k| k.contains(s)) {
                let percent = 100.0*duration_to_f64(ways[k])/total_f64;
                out.push_str(&format!("    {:.1}% {} {}\n",
                                      percent, &pretty_stack(k),
                                      duration_to_f64(ways[k])));
            }
            out.push_str("\n");
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_works() {
        clear();
        push("hello world");
        let rep = report();
        println!("\n{}", rep);
        assert!(rep.contains("hello world"));
    }
    #[test]
    fn nesting() {
        clear();
        let _a = push("hello");
        let _b = push("world");
        let rep = report();
        println!("\n{}", rep);
        assert!(rep.contains("hello:world"));
    }
    #[test]
    fn replace_works() {
        clear();
        let _a = push("hello");
        let _b = replace("world");
        let rep = report();
        println!("\n{}", rep);
        assert!(!rep.contains("hello:world"));
        assert!(rep.contains("world"));
        assert!(rep.contains("hello"));
    }
}