add bench input crate

generate const strings to use as input in benchmarks
This commit is contained in:
Noah Hellman 2023-02-09 21:57:11 +01:00
parent a3351d28b7
commit 6ce49e1a71
3 changed files with 65 additions and 0 deletions

7
bench/input/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "bench-input"
version = "0.1.0"
edition = "2021"
[lib]
path = "lib.rs"

57
bench/input/build.rs Normal file
View file

@ -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::<Vec<_>>();
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::<String>(),
)
.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::<String>(),
)
.unwrap();
println!("cargo:rerun-if-change=always_rerun");
}

1
bench/input/lib.rs Normal file
View file

@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/lib.rs"));