2023-01-05 11:57:35 -06:00
|
|
|
use std::env;
|
2023-07-16 07:33:39 -05:00
|
|
|
use std::io::Write;
|
2023-01-05 11:57:35 -06:00
|
|
|
use std::path::Path;
|
2024-08-10 10:18:04 -05:00
|
|
|
use std::process::Command;
|
2023-01-05 11:57:35 -06:00
|
|
|
|
2023-07-24 08:27:17 -05:00
|
|
|
use crate::path::{Dirs, RelPath};
|
|
|
|
use crate::prepare::GitRepo;
|
|
|
|
use crate::rustc_info::get_file_name;
|
2024-08-10 10:18:04 -05:00
|
|
|
use crate::utils::{spawn_and_wait, Compiler};
|
2023-01-05 11:57:35 -06:00
|
|
|
|
2023-01-27 12:44:19 -06:00
|
|
|
static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github(
|
2023-01-05 11:57:35 -06:00
|
|
|
"ebobby",
|
|
|
|
"simple-raytracer",
|
|
|
|
"804a7a21b9e673a482797aa289a18ed480e4d813",
|
2023-04-14 08:49:41 -05:00
|
|
|
"ad6f59a2331a3f56",
|
2023-01-05 11:57:35 -06:00
|
|
|
"<none>",
|
|
|
|
);
|
|
|
|
|
2023-02-10 03:45:50 -06:00
|
|
|
pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
|
|
|
|
benchmark_simple_raytracer(dirs, bootstrap_host_compiler);
|
2023-01-05 11:57:35 -06:00
|
|
|
}
|
|
|
|
|
2023-02-10 03:45:50 -06:00
|
|
|
fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
|
2023-01-05 11:57:35 -06:00
|
|
|
if std::process::Command::new("hyperfine").output().is_err() {
|
|
|
|
eprintln!("Hyperfine not installed");
|
|
|
|
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2023-04-14 08:49:41 -05:00
|
|
|
SIMPLE_RAYTRACER_REPO.fetch(dirs);
|
|
|
|
SIMPLE_RAYTRACER_REPO.patch(dirs);
|
2023-01-27 12:44:19 -06:00
|
|
|
|
2023-01-31 11:49:08 -06:00
|
|
|
let bench_runs = env::var("BENCH_RUNS").unwrap_or_else(|_| "10".to_string()).parse().unwrap();
|
2023-01-05 11:57:35 -06:00
|
|
|
|
2023-07-16 07:33:39 -05:00
|
|
|
let mut gha_step_summary = if let Ok(file) = std::env::var("GITHUB_STEP_SUMMARY") {
|
|
|
|
Some(std::fs::OpenOptions::new().append(true).open(file).unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-01-05 11:57:35 -06:00
|
|
|
eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
|
2023-02-10 03:45:50 -06:00
|
|
|
let cargo_clif = RelPath::DIST
|
|
|
|
.to_path(dirs)
|
|
|
|
.join(get_file_name(&bootstrap_host_compiler.rustc, "cargo_clif", "bin").replace('_', "-"));
|
2023-02-12 07:10:24 -06:00
|
|
|
let manifest_path = SIMPLE_RAYTRACER_REPO.source_dir().to_path(dirs).join("Cargo.toml");
|
|
|
|
let target_dir = RelPath::BUILD.join("simple_raytracer").to_path(dirs);
|
2023-01-05 11:57:35 -06:00
|
|
|
|
|
|
|
let clean_cmd = format!(
|
2023-01-31 11:44:29 -06:00
|
|
|
"RUSTC=rustc cargo clean --manifest-path {manifest_path} --target-dir {target_dir}",
|
2023-01-05 11:57:35 -06:00
|
|
|
manifest_path = manifest_path.display(),
|
|
|
|
target_dir = target_dir.display(),
|
|
|
|
);
|
|
|
|
let llvm_build_cmd = format!(
|
2023-02-12 07:10:24 -06:00
|
|
|
"RUSTC=rustc cargo build --manifest-path {manifest_path} --target-dir {target_dir} && (rm build/raytracer_cg_llvm || true) && ln build/simple_raytracer/debug/main build/raytracer_cg_llvm",
|
2023-01-05 11:57:35 -06:00
|
|
|
manifest_path = manifest_path.display(),
|
|
|
|
target_dir = target_dir.display(),
|
|
|
|
);
|
|
|
|
let clif_build_cmd = format!(
|
2023-02-12 07:10:24 -06:00
|
|
|
"RUSTC=rustc {cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir} && (rm build/raytracer_cg_clif || true) && ln build/simple_raytracer/debug/main build/raytracer_cg_clif",
|
|
|
|
cargo_clif = cargo_clif.display(),
|
|
|
|
manifest_path = manifest_path.display(),
|
|
|
|
target_dir = target_dir.display(),
|
|
|
|
);
|
|
|
|
let clif_build_opt_cmd = format!(
|
|
|
|
"RUSTC=rustc {cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir} --release && (rm build/raytracer_cg_clif_opt || true) && ln build/simple_raytracer/release/main build/raytracer_cg_clif_opt",
|
2023-01-05 11:57:35 -06:00
|
|
|
cargo_clif = cargo_clif.display(),
|
|
|
|
manifest_path = manifest_path.display(),
|
|
|
|
target_dir = target_dir.display(),
|
|
|
|
);
|
|
|
|
|
2023-07-16 07:33:39 -05:00
|
|
|
let bench_compile_markdown = RelPath::DIST.to_path(dirs).join("bench_compile.md");
|
|
|
|
|
2023-02-12 07:10:24 -06:00
|
|
|
let bench_compile = hyperfine_command(
|
|
|
|
1,
|
|
|
|
bench_runs,
|
|
|
|
Some(&clean_cmd),
|
2023-07-16 07:54:48 -05:00
|
|
|
&[
|
|
|
|
("cargo build", &llvm_build_cmd),
|
|
|
|
("cargo-clif build", &clif_build_cmd),
|
|
|
|
("cargo-clif build --release", &clif_build_opt_cmd),
|
|
|
|
],
|
2023-07-16 07:33:39 -05:00
|
|
|
&bench_compile_markdown,
|
2023-02-12 07:10:24 -06:00
|
|
|
);
|
2023-01-05 11:57:35 -06:00
|
|
|
|
|
|
|
spawn_and_wait(bench_compile);
|
|
|
|
|
2023-07-16 07:33:39 -05:00
|
|
|
if let Some(gha_step_summary) = gha_step_summary.as_mut() {
|
2023-07-16 07:54:48 -05:00
|
|
|
gha_step_summary.write_all(b"## Compile ebobby/simple-raytracer\n\n").unwrap();
|
2023-07-16 07:33:39 -05:00
|
|
|
gha_step_summary.write_all(&std::fs::read(bench_compile_markdown).unwrap()).unwrap();
|
|
|
|
gha_step_summary.write_all(b"\n").unwrap();
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:57:35 -06:00
|
|
|
eprintln!("[BENCH RUN] ebobby/simple-raytracer");
|
|
|
|
|
2023-07-16 07:33:39 -05:00
|
|
|
let bench_run_markdown = RelPath::DIST.to_path(dirs).join("bench_run.md");
|
|
|
|
|
2023-07-16 07:54:48 -05:00
|
|
|
let raytracer_cg_llvm = Path::new(".").join(get_file_name(
|
|
|
|
&bootstrap_host_compiler.rustc,
|
|
|
|
"raytracer_cg_llvm",
|
|
|
|
"bin",
|
|
|
|
));
|
|
|
|
let raytracer_cg_clif = Path::new(".").join(get_file_name(
|
|
|
|
&bootstrap_host_compiler.rustc,
|
|
|
|
"raytracer_cg_clif",
|
|
|
|
"bin",
|
|
|
|
));
|
|
|
|
let raytracer_cg_clif_opt = Path::new(".").join(get_file_name(
|
|
|
|
&bootstrap_host_compiler.rustc,
|
|
|
|
"raytracer_cg_clif_opt",
|
|
|
|
"bin",
|
|
|
|
));
|
2023-01-05 11:57:35 -06:00
|
|
|
let mut bench_run = hyperfine_command(
|
|
|
|
0,
|
2023-01-31 11:49:08 -06:00
|
|
|
bench_runs,
|
2023-01-05 11:57:35 -06:00
|
|
|
None,
|
2023-02-11 07:13:38 -06:00
|
|
|
&[
|
2023-07-16 07:54:48 -05:00
|
|
|
("", raytracer_cg_llvm.to_str().unwrap()),
|
|
|
|
("", raytracer_cg_clif.to_str().unwrap()),
|
|
|
|
("", raytracer_cg_clif_opt.to_str().unwrap()),
|
2023-02-11 07:13:38 -06:00
|
|
|
],
|
2023-07-16 07:33:39 -05:00
|
|
|
&bench_run_markdown,
|
2023-01-05 11:57:35 -06:00
|
|
|
);
|
|
|
|
bench_run.current_dir(RelPath::BUILD.to_path(dirs));
|
|
|
|
spawn_and_wait(bench_run);
|
2023-07-16 07:33:39 -05:00
|
|
|
|
|
|
|
if let Some(gha_step_summary) = gha_step_summary.as_mut() {
|
2023-07-16 07:54:48 -05:00
|
|
|
gha_step_summary.write_all(b"## Run ebobby/simple-raytracer\n\n").unwrap();
|
2023-07-16 07:33:39 -05:00
|
|
|
gha_step_summary.write_all(&std::fs::read(bench_run_markdown).unwrap()).unwrap();
|
|
|
|
gha_step_summary.write_all(b"\n").unwrap();
|
|
|
|
}
|
2023-01-05 11:57:35 -06:00
|
|
|
}
|
2024-08-10 10:18:04 -05:00
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
fn hyperfine_command(
|
|
|
|
warmup: u64,
|
|
|
|
runs: u64,
|
|
|
|
prepare: Option<&str>,
|
|
|
|
cmds: &[(&str, &str)],
|
|
|
|
markdown_export: &Path,
|
|
|
|
) -> Command {
|
|
|
|
let mut bench = Command::new("hyperfine");
|
|
|
|
|
|
|
|
bench.arg("--export-markdown").arg(markdown_export);
|
|
|
|
|
|
|
|
if warmup != 0 {
|
|
|
|
bench.arg("--warmup").arg(warmup.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if runs != 0 {
|
|
|
|
bench.arg("--runs").arg(runs.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(prepare) = prepare {
|
|
|
|
bench.arg("--prepare").arg(prepare);
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(name, cmd) in cmds {
|
|
|
|
if name != "" {
|
|
|
|
bench.arg("-n").arg(name);
|
|
|
|
}
|
|
|
|
bench.arg(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bench
|
|
|
|
}
|