From 37d97529e3cee0b0c4f82adb831c427e53fb2ea3 Mon Sep 17 00:00:00 2001 From: cydparser Date: Tue, 6 Aug 2024 15:20:02 -0700 Subject: [PATCH] Fix benchmarks on Windows --- Cargo.lock | 2 +- core/Cargo.toml | 1 - core/benches/arrays.rs | 7 +++---- core/benches/functions.rs | 7 +++---- core/benches/mantis.rs | 7 +++---- core/benches/numeric.rs | 7 +++---- core/benches/records.rs | 7 +++---- core/benches/serialization.rs | 7 +++---- core/benches/stdlib.rs | 4 ++-- core/benches/typecheck-nixpkgs-lib.rs | 7 +++---- utils/Cargo.toml | 3 +++ utils/src/bench.rs | 24 +++++++++++++++++++----- 12 files changed, 46 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a2d92545e..6b838fbb1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1774,7 +1774,6 @@ dependencies = [ "nickel-lang-utils", "once_cell", "pkg-config", - "pprof", "pretty", "pretty_assertions", "regex", @@ -1848,6 +1847,7 @@ dependencies = [ "codespan", "criterion", "nickel-lang-core", + "pprof", "serde", "toml", ] diff --git a/core/Cargo.toml b/core/Cargo.toml index de6963701c..5306737551 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -86,7 +86,6 @@ strsim = "0.10.0" pretty_assertions.workspace = true assert_matches.workspace = true criterion.workspace = true -pprof = { workspace = true, features = ["criterion", "flamegraph"] } nickel-lang-utils.workspace = true similar.workspace = true test-generator.workspace = true diff --git a/core/benches/arrays.rs b/core/benches/arrays.rs index 70d16e8e17..9c8d36bf83 100644 --- a/core/benches/arrays.rs +++ b/core/benches/arrays.rs @@ -1,12 +1,11 @@ use std::rc::Rc; -use criterion::{criterion_main, Criterion}; +use criterion::criterion_main; use nickel_lang_core::term::{ array::{Array, ArrayAttrs}, Number, RichTerm, Term, }; -use nickel_lang_utils::{bench::EvalMode, ncl_bench_group}; -use pprof::criterion::{Output, PProfProfiler}; +use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_group}; use pretty::{BoxAllocator, DocBuilder, Pretty}; /// Generates a pseaudo-random Nickel array as a string. @@ -35,7 +34,7 @@ fn ncl_random_array(len: usize) -> String { ncl_bench_group! { name = benches; -config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); +config = criterion_config(); { name = "foldr strings 50", path = "arrays/fold", diff --git a/core/benches/functions.rs b/core/benches/functions.rs index 28cd5f19a7..3635bb25bc 100644 --- a/core/benches/functions.rs +++ b/core/benches/functions.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::ncl_bench_group; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "church 3", path = "functions/church", diff --git a/core/benches/mantis.rs b/core/benches/mantis.rs index 1fff0ef26f..636c5b6f91 100644 --- a/core/benches/mantis.rs +++ b/core/benches/mantis.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::{bench::EvalMode, ncl_bench_group}; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "mantis", path = "mantis/run", diff --git a/core/benches/numeric.rs b/core/benches/numeric.rs index d413c95e0c..a72c690025 100644 --- a/core/benches/numeric.rs +++ b/core/benches/numeric.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::ncl_bench_group; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "fibonacci 10", path = "numeric/fibonacci", diff --git a/core/benches/records.rs b/core/benches/records.rs index d3157d9ca2..a2edaeabb7 100644 --- a/core/benches/records.rs +++ b/core/benches/records.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::{bench::EvalMode, ncl_bench_group}; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "countLetters", path = "records/countLetters", diff --git a/core/benches/serialization.rs b/core/benches/serialization.rs index d823b9fade..2c6f26c474 100644 --- a/core/benches/serialization.rs +++ b/core/benches/serialization.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::ncl_bench_group; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "round_trip", path = "serialization/main", diff --git a/core/benches/stdlib.rs b/core/benches/stdlib.rs index e442199f18..c15d4b8f50 100644 --- a/core/benches/stdlib.rs +++ b/core/benches/stdlib.rs @@ -1,7 +1,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use pprof::criterion::{Output, PProfProfiler}; use nickel_lang_core::cache::{Cache, ErrorTolerance}; +use nickel_lang_utils::bench::criterion_config; pub fn typecheck_stdlib(c: &mut Criterion) { let mut cache = Cache::new(ErrorTolerance::Strict); @@ -18,7 +18,7 @@ pub fn typecheck_stdlib(c: &mut Criterion) { criterion_group!( name = benches; -config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); +config = criterion_config(); targets = typecheck_stdlib ); criterion_main!(benches); diff --git a/core/benches/typecheck-nixpkgs-lib.rs b/core/benches/typecheck-nixpkgs-lib.rs index ea28210952..d7181eb402 100644 --- a/core/benches/typecheck-nixpkgs-lib.rs +++ b/core/benches/typecheck-nixpkgs-lib.rs @@ -1,10 +1,9 @@ -use criterion::{criterion_main, Criterion}; -use nickel_lang_utils::{bench::EvalMode, ncl_bench_group}; -use pprof::criterion::{Output, PProfProfiler}; +use criterion::criterion_main; +use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_group}; ncl_bench_group! { name = benches; - config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + config = criterion_config(); { name = "nixpkgs lists", path = "nixpkgs/lists", diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 299881dd98..bdd3481a7a 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -19,3 +19,6 @@ criterion.workspace = true codespan.workspace = true serde = { workspace = true, features = ["derive"] } toml = { workspace = true, features = ["parse"] } + +[target.'cfg(target_family = "unix")'.dependencies] +pprof = { workspace = true, features = ["criterion", "flamegraph"] } diff --git a/utils/src/bench.rs b/utils/src/bench.rs index b275949148..d740b70a1d 100644 --- a/utils/src/bench.rs +++ b/utils/src/bench.rs @@ -71,7 +71,7 @@ impl<'b> Bench<'b> { let field_path = self.subtest.map(|s| format!(".{s}")).unwrap_or_default(); let content = format!( - "(import \"{}\"){}.run {}", + "(import {:?}){}.run {}", path.to_string_lossy(), field_path, self.args @@ -86,13 +86,15 @@ impl<'b> Bench<'b> { } else { content }; - parse(&content).unwrap() + parse(&content).unwrap_or_else(|err| panic!("Failed parsing {path:?}: {err:?}")) } pub fn path(&self) -> PathBuf { - let mut path = PathBuf::from(self.base_dir); - path.push(format!("benches/{}.ncl", self.subpath)); - path + PathBuf::from_iter([ + self.base_dir, + "benches", + format!("{}.ncl", self.subpath).as_str(), + ]) } } @@ -139,6 +141,18 @@ pub fn bench_terms<'r>(rts: Vec>) -> Box }) } +pub fn criterion_config() -> Criterion { + #[allow(unused_mut)] + let mut config = Criterion::default(); + + #[cfg(target_family = "unix")] + { + use pprof::criterion::{Output, PProfProfiler}; + config = config.with_profiler(PProfProfiler::new(100, Output::Flamegraph(None))); + } + config +} + #[macro_export] macro_rules! ncl_bench { {