|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +use std::{fs::File, os::raw::c_int, path::Path}; |
| 8 | + |
| 9 | +use criterion::{criterion_group, criterion_main, profiler::Profiler, Criterion}; |
| 10 | +use pprof::ProfilerGuard; |
| 11 | + |
| 12 | +const N_LINES: usize = 1000; |
| 13 | +fn get_query_isa() -> String { |
| 14 | + let mut query: String = "match\n".to_owned(); |
| 15 | + (0..N_LINES).for_each(|_| query.push_str("$v_abc123 isa t_abc123 == 123456;\n")); |
| 16 | + query |
| 17 | +} |
| 18 | + |
| 19 | +fn get_query_var_sub() -> String { |
| 20 | + let mut query: String = "match\n".to_owned(); |
| 21 | + (0..N_LINES).for_each(|_| query.push_str("$v_abc123 sub t_abc123;\n")); |
| 22 | + query |
| 23 | +} |
| 24 | + |
| 25 | +fn get_query_label_sub() -> String { |
| 26 | + let mut query: String = "match\n".to_owned(); |
| 27 | + (0..N_LINES).for_each(|_| query.push_str("t_pqr456 sub t_abc123;\n")); |
| 28 | + query |
| 29 | +} |
| 30 | + |
| 31 | +fn get_query_expression() -> String { |
| 32 | + let mut query: String = "match\n".to_owned(); |
| 33 | + (0..N_LINES).for_each(|_| query.push_str("let $volume = (4/3) * 3.14 * pow($r, 3) ;\n")); |
| 34 | + query |
| 35 | +} |
| 36 | + |
| 37 | +fn get_query_disjunctions() -> String { |
| 38 | + let stmt = "$v_abc123 isa t_abc123 == 123456;\n"; |
| 39 | + let line = format!("{{ {stmt} }} or\n"); |
| 40 | + |
| 41 | + let mut query: String = "match\n".to_owned(); |
| 42 | + (0..(N_LINES - 1)).for_each(|_| query.push_str(line.as_str())); |
| 43 | + query.push_str(format!("{{ {stmt} }};").as_str()); |
| 44 | + query |
| 45 | +} |
| 46 | + |
| 47 | +fn get_query_many_inserts() -> String { |
| 48 | + let mut query: String = "".to_owned(); |
| 49 | + (0..N_LINES).for_each(|_| query.push_str("insert $_ isa person;\n")); |
| 50 | + query |
| 51 | +} |
| 52 | + |
| 53 | +fn get_define_label_sub() -> String { |
| 54 | + let mut query: String = "define\n".to_owned(); |
| 55 | + (0..N_LINES).for_each(|_| query.push_str("t_pqr456 sub t_abc123;\n")); |
| 56 | + query |
| 57 | +} |
| 58 | + |
| 59 | +fn criterion_benchmark(c: &mut Criterion) { |
| 60 | + println!("In criterion benchmark"); |
| 61 | + let query_isa = get_query_isa(); |
| 62 | + let query_label_sub = get_query_isa(); |
| 63 | + let query_var_sub = get_query_var_sub(); |
| 64 | + let query_expression = get_query_expression(); |
| 65 | + let query_disjunctions = get_query_disjunctions(); |
| 66 | + let query_many_inserts = get_query_many_inserts(); |
| 67 | + let define_label_sub = get_define_label_sub(); |
| 68 | + c.bench_function("parse-isa", |b| b.iter(|| typeql::parse_query(query_isa.as_str()).unwrap())); |
| 69 | + c.bench_function("parse-var-sub", |b| b.iter(|| typeql::parse_query(query_var_sub.as_str()).unwrap())); |
| 70 | + c.bench_function("parse-label-sub", |b| b.iter(|| typeql::parse_query(query_label_sub.as_str()).unwrap())); |
| 71 | + c.bench_function("parse-expression", |b| b.iter(|| typeql::parse_query(query_expression.as_str()).unwrap())); |
| 72 | + c.bench_function("parse-disjunctions", |b| b.iter(|| typeql::parse_query(query_disjunctions.as_str()).unwrap())); |
| 73 | + c.bench_function("parse-many-inserts", |b| b.iter(|| typeql::parse_query(query_many_inserts.as_str()).unwrap())); |
| 74 | + c.bench_function("parse-define-sub", |b| b.iter(|| typeql::parse_query(define_label_sub.as_str()).unwrap())); |
| 75 | +} |
| 76 | +// --- Code to generate flamegraphs copied from https://www.jibbow.com/posts/criterion-flamegraphs/ --- |
| 77 | +// This causes a SIGBUS on (mac) arm64 if the frequency is set too high. |
| 78 | + |
| 79 | +pub struct FlamegraphProfiler<'a> { |
| 80 | + frequency: c_int, |
| 81 | + active_profiler: Option<ProfilerGuard<'a>>, |
| 82 | +} |
| 83 | + |
| 84 | +impl<'a> FlamegraphProfiler<'a> { |
| 85 | + #[allow(dead_code)] |
| 86 | + pub fn new(frequency: c_int) -> Self { |
| 87 | + FlamegraphProfiler { frequency, active_profiler: None } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<'a> Profiler for FlamegraphProfiler<'a> { |
| 92 | + fn start_profiling(&mut self, _benchmark_id: &str, _benchmark_dir: &Path) { |
| 93 | + self.active_profiler = Some(ProfilerGuard::new(self.frequency).unwrap()); |
| 94 | + } |
| 95 | + |
| 96 | + fn stop_profiling(&mut self, _benchmark_id: &str, benchmark_dir: &Path) { |
| 97 | + std::fs::create_dir_all(benchmark_dir).unwrap(); |
| 98 | + let flamegraph_path = benchmark_dir.join("flamegraph.svg"); |
| 99 | + let flamegraph_file = File::create(flamegraph_path).expect("File system error while creating flamegraph.svg"); |
| 100 | + if let Some(profiler) = self.active_profiler.take() { |
| 101 | + profiler.report().build().unwrap().flamegraph(flamegraph_file).expect("Error writing flamegraph"); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn profiled() -> Criterion { |
| 107 | + Criterion::default().with_profiler(FlamegraphProfiler::new(100)) |
| 108 | +} |
| 109 | + |
| 110 | +criterion_group!( |
| 111 | + name = benches; |
| 112 | + config = profiled(); |
| 113 | + targets = criterion_benchmark |
| 114 | +); |
| 115 | + |
| 116 | +criterion_main!(benches); |
0 commit comments