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
//!
//!
#[macro_use]
extern crate serde;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate screeps;
extern crate serde_json;

pub mod screeps_profiling;

pub use screeps_profiling::*;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ProfileTable {
    labels: Vec<String>,
    data: Vec<ProfileRow>,
}

impl ProfileTable {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn clear(&mut self) {
        self.labels.clear();
        self.data.clear();
    }

    pub fn add_entity(&mut self, name: String) -> ProfileId {
        let id = self.data.len();
        self.labels.push(name);
        self.data.push(ProfileRow::default());
        ProfileId { id }
    }

    pub fn get_label<'a>(&'a self, id: ProfileId) -> Option<&'a String> {
        self.labels.get(id.id)
    }

    pub fn get_data<'a>(&'a self, id: ProfileId) -> Option<&'a ProfileRow> {
        self.data.get(id.id)
    }

    pub fn get_data_mut<'a>(&'a mut self, id: ProfileId) -> Option<&'a mut ProfileRow> {
        self.data.get_mut(id.id)
    }
}

#[derive(Clone, Debug, Copy, Serialize, Deserialize)]
pub struct ProfileId {
    pub id: usize,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ProfileRow {
    pub cpu_per_call: Vec<f64>,
}

#[derive(Clone, Debug)]
pub struct ProfileSentinel<TCpuFun>
where
    TCpuFun: FnMut() -> f64,
{
    id: ProfileId,
    table: *mut ProfileTable,

    cpu_at_start: f64,

    get_cpu: TCpuFun,
}

impl<T> ProfileSentinel<T>
where
    T: FnMut() -> f64,
{
    pub fn new(id: ProfileId, table: &mut ProfileTable, mut get_cpu: T) -> Self {
        let cpu = get_cpu();

        Self {
            cpu_at_start: cpu,
            table: table as *mut _,
            get_cpu,
            id,
        }
    }
}

impl<T> Drop for ProfileSentinel<T>
where
    T: FnMut() -> f64,
{
    fn drop(&mut self) {
        let delta = (self.get_cpu)() - self.cpu_at_start;

        let row = unsafe {
            (*self.table).get_data_mut(self.id).expect(&format!(
                "Expected a profile row to be available by id {:?}",
                self.id
            ))
        };

        row.cpu_per_call.push(delta);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut table = ProfileTable::new();

        let call_one = table.add_entity("One".to_owned());
        let call_two = table.add_entity("Two".to_owned());
        let call_zero = table.add_entity("Zero".to_owned());

        let mut cpu = 1.;
        let get_cpu = move || {
            cpu += 2.0;
            cpu
        };

        {
            let _sentinel = ProfileSentinel::new(call_one, &mut table, get_cpu);
        }

        {
            let _sentinel = ProfileSentinel::new(call_two, &mut table, get_cpu);
            let _sentinel = ProfileSentinel::new(call_two, &mut table, get_cpu);
        }

        println!("{:#?}", table);

        assert_eq!(
            table.get_data(call_one).map(|x| x.cpu_per_call.len()),
            Some(1)
        );
        assert_eq!(
            table.get_data(call_two).map(|x| x.cpu_per_call.len()),
            Some(2)
        );

        assert_eq!(
            table.get_data(call_zero).map(|x| x.cpu_per_call.len()),
            Some(0)
        );

        let cpu = table.get_data(call_one).map(|x| x.cpu_per_call[0]).unwrap();
        assert!((2.0 - cpu).abs() < 0.0003);

        let cpu = table.get_data(call_two).map(|x| x.cpu_per_call[0]).unwrap();
        assert!((2.0 - cpu).abs() < 0.0003);
        let cpu = table.get_data(call_two).map(|x| x.cpu_per_call[1]).unwrap();
        assert!((2.0 - cpu).abs() < 0.0003);
    }
}