From 6ce49e1a711ee2b3348236c2cd77971b86d69ee7 Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Thu, 9 Feb 2023 21:57:11 +0100 Subject: [PATCH] add bench input crate generate const strings to use as input in benchmarks --- bench/input/Cargo.toml | 7 ++++++ bench/input/build.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ bench/input/lib.rs | 1 + 3 files changed, 65 insertions(+) create mode 100644 bench/input/Cargo.toml create mode 100644 bench/input/build.rs create mode 100644 bench/input/lib.rs diff --git a/bench/input/Cargo.toml b/bench/input/Cargo.toml new file mode 100644 index 0000000..3be5b20 --- /dev/null +++ b/bench/input/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bench-input" +version = "0.1.0" +edition = "2021" + +[lib] +path = "lib.rs" diff --git a/bench/input/build.rs b/bench/input/build.rs new file mode 100644 index 0000000..8b0f3b1 --- /dev/null +++ b/bench/input/build.rs @@ -0,0 +1,57 @@ +use std::io::Write; + +fn main() { + let inputs = std::fs::read_dir("..") + .unwrap() + .filter_map(|entry| { + let entry = entry.ok()?; + if let Some(name) = entry.file_name().to_str() { + if let Some(name) = name.strip_suffix(".dj") { + if entry.file_type().map_or(false, |ty| !ty.is_dir()) { + let name_snake = name.replace('-', "_"); + let input = std::fs::read_to_string( + std::path::Path::new("..").join(entry.file_name()), + ) + .ok()?; + return Some((name_snake, input)); + } + } + } + None + }) + .collect::>(); + + let out_dir = std::env::var_os("OUT_DIR").unwrap(); + let mut out = std::fs::File::create(std::path::Path::new(&out_dir).join("lib.rs")).unwrap(); + + inputs.iter().for_each(|(name, input)| { + write!( + out, + "#[allow(dead_code)]\nconst {}: &str = r###\"{}\"###;", + name.to_uppercase(), + input, + ) + .unwrap() + }); + + write!( + out, + "#[allow(dead_code)]\npub const ALL: &str = r###\"{}\"###;", + inputs.iter().map(|(_, s)| s.as_str()).collect::(), + ) + .unwrap(); + + write!( + out, + "#[allow(dead_code)]\npub const INPUTS: &[(&str, &str)] = &[{}];", + inputs + .iter() + .map(|(n, _)| n.as_ref()) + .chain(std::iter::once("all")) + .map(|n| format!("(\"{}\", {}),", n, n.to_uppercase())) + .collect::(), + ) + .unwrap(); + + println!("cargo:rerun-if-change=always_rerun"); +} diff --git a/bench/input/lib.rs b/bench/input/lib.rs new file mode 100644 index 0000000..f7072f6 --- /dev/null +++ b/bench/input/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/lib.rs"));