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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use regex::{self, Regex};
use serde::Serialize;
use serde_json;
use std::error;
use wasm_bindgen::prelude::*;

mod utils;

#[cfg(target_arch = "wasm32")]
extern crate console_error_panic_hook;
#[cfg(target_arch = "wasm32")]
extern crate web_sys;

// A macro to provide `println!(..)`-style syntax for `console.log` logging.
macro_rules! log {
    ( $( $t:tt )* ) => {
        #[cfg(target_arch = "wasm32")]
        web_sys::console::log_1(&format!( $( $t )* ).into());
    }
}

#[cfg(not(target_arch = "wasm32"))]
mod c_ffi;

#[cfg(not(target_arch = "wasm32"))]
pub use c_ffi::*;

#[wasm_bindgen]
#[derive(PartialOrd, PartialEq, Debug, Serialize, Copy, Clone)]
pub enum KeyDistribution {
    UNIFORM = 0,
    ZIPFAN = 1,
}

#[wasm_bindgen]
#[derive(PartialEq, PartialOrd, Debug, Serialize, Copy, Clone)]
pub struct BenchmarkOptions {
    sampling: i32,
    latency: f32,
    key_size: i32,
    value_size: i32,
    random_seed: i32,
    read: f32,
    insert: f32,
    update: f32,
    delete: f32,
    scan: f32,
    key_distribution: KeyDistribution,
    records: i32,
    operations: i32,
    threads: i32,
}

impl BenchmarkOptions {
    pub fn from_text(text: &str) -> Result<BenchmarkOptions, Box<dyn error::Error>> {
        let re = Regex::new(
            "# Records: (?P<records>\\d+)\\s*\n\
                \\s*# Operations: (?P<operations>\\d+)\\s*\n\
               \\s*# Threads: (?P<threads>\\d+)\\s*\n\
               \\s*Sampling: (?P<sampling>\\d+) ms\\s*\n\
               \\s*Latency: (?P<latency>\\d*\\.?\\d*)\\s*\n\
               .*\n\
               \\s*Key size: (?P<key_size>\\d+)\\s*\n\
               \\s*Value size: (?P<value_size>\\d+)\\s*\n\
               \\s*Random seed: (?P<random_seed>\\d+)\\s*\n\
               \\s*Key distribution: (?P<key_distribution>\\w+)\\s*\n\
               \\s*Scan size: (?P<scan_size>\\d+)\\s*\n\
               .*\n\
               \\s*Read: (?P<read>\\d*\\.?\\d*)\\s*\n\
               \\s*Insert: (?P<insert>\\d*\\.?\\d*)\\s*\n\
               \\s*Update: (?P<update>\\d*\\.?\\d*)\\s*\n\
               \\s*Delete: (?P<delete>\\d*\\.?\\d*)\\s*\n\
               \\s*Scan: (?P<scan>\\d*\\.?\\d*)\\s*",
        )
        .unwrap();
        let caps = re.captures(text).unwrap();

        Ok(BenchmarkOptions {
            sampling: caps["sampling"].parse::<i32>()?,
            records: caps["records"].parse::<i32>()?,
            threads: caps["threads"].parse::<i32>()?,
            operations: caps["operations"].parse::<i32>()?,
            latency: caps["latency"].parse::<f32>()?,
            key_size: caps["key_size"].parse::<i32>()?,
            key_distribution: KeyDistribution::UNIFORM,
            value_size: caps["value_size"].parse::<i32>()?,
            random_seed: caps["random_seed"].parse::<i32>()?,
            read: caps["read"].parse::<f32>()?,
            insert: caps["insert"].parse::<f32>()?,
            delete: caps["delete"].parse::<f32>()?,
            update: caps["update"].parse::<f32>()?,
            scan: caps["scan"].parse::<f32>()?,
        })
    }
}

#[wasm_bindgen]
#[derive(Eq, PartialEq, PartialOrd, Debug, Serialize, Copy, Clone)]
pub struct LatencyResults {
    min: i32,
    p_50: i32,
    p_90: i32,
    p_99: i32,
    p_99_9: i32,
    p_99_99: i32,
    p_99_999: i32,
    max: i32,
}

impl LatencyResults {
    pub fn from_text(text: &str) -> Result<Option<LatencyResults>, Box<dyn error::Error>> {
        let re = Regex::new(
            "Latencies .*\n\
            \\s*min: (?P<min>\\d+)\\s*\n\
            \\s*50%: (?P<p_50>\\d+)\\s*\n\
            \\s*90%: (?P<p_90>\\d+)\\s*\n\
            \\s*99%: (?P<p_99>\\d+)\\s*\n\
            \\s*99.9%: (?P<p_99_9>\\d+)\\s*\n\
            \\s*99.99%: (?P<p_99_99>\\d+)\\s*\n\
            \\s*99.999%: (?P<p_99_999>\\d+)\\s*\n\
            \\s*max: (?P<max>\\d+)\\s*",
        )?;
        let caps = match re.captures(text) {
            Some(caps) => caps,
            None => return Ok(None),
        };
        Ok(Some(LatencyResults {
            min: caps["min"].parse::<i32>()?,
            p_50: caps["p_50"].parse::<i32>()?,
            p_90: caps["p_90"].parse::<i32>()?,
            p_99: caps["p_99"].parse::<i32>()?,
            p_99_9: caps["p_99_9"].parse::<i32>()?,
            p_99_99: caps["p_99_99"].parse::<i32>()?,
            p_99_999: caps["p_99_999"].parse::<i32>()?,
            max: caps["max"].parse::<i32>()?,
        }))
    }
}

#[wasm_bindgen]
#[derive(PartialOrd, Eq, PartialEq, Debug, Clone, Serialize)]
pub struct PCMResults {
    l3_misses: u64,
    dram_reads: u64,
    dram_writes: u64,
    nvm_reads: u64,
    nvm_writes: u64,
}

impl PCMResults {
    pub fn from_text(text: &str) -> Result<Option<PCMResults>, Box<dyn error::Error>> {
        let regex_raw = "\\s*L3 misses: (?P<l3_misses>\\d+)\\s*\n\
            \\s*DRAM Reads \\(bytes\\): (?P<dram_reads>\\d+)\\s*\n\
            \\s*DRAM Writes \\(bytes\\): (?P<dram_writes>\\d+)\\s*\n\
            \\s*NVM Reads \\(bytes\\): (?P<nvm_reads>\\d+)\\s*\n\
            \\s*NVM Writes \\(bytes\\): (?P<nvm_writes>\\d+)\\s*";
        let caps = match Regex::new(&regex_raw)?.captures(text) {
            Some(caps) => caps,
            None => return Ok(None),
        };

        Ok(Some(PCMResults {
            l3_misses: caps["l3_misses"].parse::<u64>()?,
            dram_reads: caps["dram_reads"].parse::<u64>()?,
            dram_writes: caps["dram_writes"].parse::<u64>()?,
            nvm_reads: caps["nvm_reads"].parse::<u64>()?,
            nvm_writes: caps["nvm_writes"].parse::<u64>()?,
        }))
    }
}

#[wasm_bindgen]
#[derive(PartialEq, PartialOrd, Debug, Serialize, Clone)]
pub struct BenchmarkResults {
    load_time: f32,
    run_time: f32,
    throughput: f32,
    pcm: Option<PCMResults>,
    latency: Option<LatencyResults>,
    samples: Option<Vec<u32>>,
}

impl BenchmarkResults {
    pub fn from_text(text: &str) -> Result<BenchmarkResults, Box<dyn error::Error>> {
        const FLOATING_REGEX: &str = "[+\\-]?(?:0|[1-9]\\d*)(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?";
        let regex_raw = format!(
            "Load time: (?P<load_time>{floating}) milliseconds\\s*\n\
            \\s*Run time: (?P<run_time>{floating}) milliseconds\\s*\n\
            \\s*Throughput: (?P<throughput>{floating}) ops/s",
            floating = FLOATING_REGEX
        );
        let re = Regex::new(&regex_raw)?;
        let caps = re.captures(text).unwrap();

        let latency_results = LatencyResults::from_text(text)?;
        let pcm_results = PCMResults::from_text(text)?;
        let samples = BenchmarkResults::capture_samples(text)?;

        Ok(BenchmarkResults {
            load_time: caps["load_time"].parse::<f32>()?,
            run_time: caps["run_time"].parse::<f32>()?,
            throughput: caps["throughput"].parse::<f32>()?,
            pcm: pcm_results,
            latency: latency_results,
            samples,
        })
    }

    pub fn capture_samples(text: &str) -> Result<Option<Vec<u32>>, Box<dyn error::Error>> {
        let regex_raw = r"Samples:\s*(?P<samples>(\s*\d+\n*)*)";
        let caps = match Regex::new(&regex_raw)?.captures(text) {
            Some(caps) => caps,
            None => {
                return Ok(None);
            }
        };
        let samples = caps["samples"]
            .split("\n")
            .map(|item| item.trim())
            .filter(|item| !item.is_empty())
            .map(|item| item.parse::<u32>())
            .collect::<Result<Vec<_>, _>>();
        Ok(Some(samples?))
    }
}

#[wasm_bindgen]
#[derive(Serialize, Debug, Eq, PartialEq)]
struct BenchmarkEnv {
    time: String,
    cpu: String,
    cache: String,
    kernel: String,
}

impl BenchmarkEnv {
    pub fn from_text(text: &str) -> Result<Self, Box<dyn error::Error>> {
        let regex_raw = "\\s*Time: (?P<time>.*)\\s*\n\
                                \\s*CPU: (?P<cpu>.*)\\s*\n\
                                \\s*CPU Cache: (?P<cache>.*)\\s*\n\
                                \\s*Kernel: (?P<kernel>.*)\\s*\n";
        let caps = Regex::new(&regex_raw)?.captures(text).unwrap();

        Ok(BenchmarkEnv {
            time: caps["time"].to_string(),
            cpu: caps["cpu"].to_string(),
            cache: caps["cache"].to_string(),
            kernel: caps["kernel"].to_string(),
        })
    }
}

#[wasm_bindgen]
#[derive(Serialize)]
pub struct PiBenchData {
    options: BenchmarkOptions,
    results: BenchmarkResults,
    env: BenchmarkEnv,
}

impl PiBenchData {
    pub fn to_json(&self) -> String {
        serde_json::to_string(&self).unwrap()
    }
}

#[wasm_bindgen]
impl PiBenchData {
    pub fn from_text(input: &str) -> Option<PiBenchData> {
        utils::set_panic_hook();
        let benchmark_options = BenchmarkOptions::from_text(input);
        let benchmark_results = BenchmarkResults::from_text(input);
        let benchmark_env = BenchmarkEnv::from_text(input);

        if benchmark_options.is_err() || benchmark_results.is_err() || benchmark_env.is_err() {
            return None;
        }

        return Some(PiBenchData {
            options: benchmark_options.unwrap(),
            results: benchmark_results.unwrap(),
            env: benchmark_env.unwrap(),
        });
    }
    pub fn to_js_value(&self) -> JsValue {
        JsValue::from_serde(&self).unwrap()
    }
}

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

    #[test]
    fn parse_benchmark_results() {
        let sample_string = "Overview:
                                    Load time: 90801.3 milliseconds
                                    Run time: 79192.3672 milliseconds
                                    Throughput: 126274.7969 ops/s
                                PCM Metrics:
                                    L3 misses: 133342466
                                    DRAM Reads (bytes): 4197345472
                                    DRAM Writes (bytes): 3685394624
                                    NVM Reads (bytes): 60347831872
                                    NVM Writes (bytes): 11408209856
                                Samples:
                                	135452
	                                126077
                                    109243
                                Latencies (994788 operations
                                ";
        let gt = BenchmarkResults {
            load_time: 90801.3,
            run_time: 79192.3672,
            throughput: 126274.7969,
            pcm: Some(PCMResults {
                l3_misses: 133342466,
                dram_reads: 4197345472,
                dram_writes: 3685394624,
                nvm_reads: 60347831872,
                nvm_writes: 11408209856,
            }),
            latency: None,
            samples: Some(vec![135452, 126077, 109243]),
        };
        let result = BenchmarkResults::from_text(sample_string);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), gt);
    }

    #[test]
    fn parse_latency_results() {
        let sample_string = "Latencies (998141 operations observed):
                                    min: 882
                                    50%: 7481
                                    90%: 9121
                                    99%: 43233
                                    99.9%: 51150
                                    99.99%: 69460
                                    99.999%: 16985300
                                    max: 22247728
                                ";
        let gt = LatencyResults {
            min: 882,
            p_50: 7481,
            p_90: 9121,
            p_99: 43233,
            p_99_9: 51150,
            p_99_99: 69460,
            p_99_999: 16985300,
            max: 22247728,
        };
        let latency = LatencyResults::from_text(sample_string);
        assert!(latency.is_ok());
        assert_eq!(latency.unwrap().unwrap(), gt);
    }

    #[test]
    fn parse_benchmark_options() {
        let sample_string = " Environment:
                                    Time: Sat May  9 13:39:56 2020
                                    CPU: 96 * Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz
                                    CPU Cache: 36608 KB
                                    Kernel: Linux 5.5.4-arch1-1
                                    Benchmark Options:
                                    Target: /home/hao/coding/bztree/release/libbztree_pibench_wrapper.so
                                    # Records: 10000000
                                    # Operations: 10000000
                                    # Threads: 1
                                    Sampling: 1000 ms
                                    Latency: 0.1
                                    Key prefix: 
                                    Key size: 8
                                    Value size: 8
                                    Random seed: 1729
                                    Key distribution: UNIFORM
                                    Scan size: 100
                                    Operations ratio:
                                        Read: 0.2
                                        Insert: 0.8
                                        Update: 0
                                        Delete: 0
                                        Scan: 0
                                creating new tree on pool.
                                ";
        let gt = BenchmarkOptions {
            records: 10000000,
            operations: 10000000,
            threads: 1,
            sampling: 1000,
            latency: 0.1,
            key_size: 8,
            value_size: 8,
            random_seed: 1729,
            key_distribution: KeyDistribution::UNIFORM,
            scan: 0.,
            read: 0.2,
            insert: 0.8,
            update: 0.,
            delete: 0.,
        };
        let options = BenchmarkOptions::from_text(sample_string);
        assert!(options.is_ok());
        assert_eq!(options.unwrap(), gt);
    }

    #[test]
    fn parse_benchmark_env() {
        let sample_string=" Environment:
                                    Time: Sat May  9 13:39:56 2020
                                    CPU: 96 * Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz
                                    CPU Cache: 36608 KB
                                    Kernel: Linux 5.5.4-arch1-1
                                    Benchmark Options:
                                    Target: /home/hao/coding/bztree/release/libbztree_pibench_wrapper.so
                                    # Records: 10000000
                                    # Operations: 10000000
                                    # Threads: 1
                                    Sampling: 1000 ms";

        let gt = BenchmarkEnv {
            time: "Sat May  9 13:39:56 2020".to_string(),
            cpu: "96 * Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz".to_string(),
            cache: "36608 KB".to_string(),
            kernel: "Linux 5.5.4-arch1-1".to_string(),
        };

        let env = BenchmarkEnv::from_text(sample_string);
        assert!(env.is_ok());
        assert_eq!(env.unwrap(), gt);
    }
}