|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use super::path::{Dirs, RelPath}; |
| 6 | +use super::prepare::GitRepo; |
| 7 | +use super::rustc_info::{get_file_name, get_wrapper_file_name}; |
| 8 | +use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject}; |
| 9 | + |
| 10 | +pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( |
| 11 | + "ebobby", |
| 12 | + "simple-raytracer", |
| 13 | + "804a7a21b9e673a482797aa289a18ed480e4d813", |
| 14 | + "<none>", |
| 15 | +); |
| 16 | + |
| 17 | +pub(crate) static SIMPLE_RAYTRACER: CargoProject = |
| 18 | + CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); |
| 19 | + |
| 20 | +pub(crate) fn benchmark(dirs: &Dirs) { |
| 21 | + benchmark_simple_raytracer(dirs); |
| 22 | +} |
| 23 | + |
| 24 | +fn benchmark_simple_raytracer(dirs: &Dirs) { |
| 25 | + if std::process::Command::new("hyperfine").output().is_err() { |
| 26 | + eprintln!("Hyperfine not installed"); |
| 27 | + eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine"); |
| 28 | + std::process::exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + let run_runs = env::var("RUN_RUNS") |
| 32 | + .unwrap_or(if is_ci() { "2" } else { "10" }.to_string()) |
| 33 | + .parse() |
| 34 | + .unwrap(); |
| 35 | + |
| 36 | + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); |
| 37 | + let cargo_clif = RelPath::DIST.to_path(dirs).join(get_wrapper_file_name("cargo-clif", "bin")); |
| 38 | + let manifest_path = SIMPLE_RAYTRACER.manifest_path(dirs); |
| 39 | + let target_dir = SIMPLE_RAYTRACER.target_dir(dirs); |
| 40 | + |
| 41 | + let clean_cmd = format!( |
| 42 | + "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}", |
| 43 | + manifest_path = manifest_path.display(), |
| 44 | + target_dir = target_dir.display(), |
| 45 | + ); |
| 46 | + let llvm_build_cmd = format!( |
| 47 | + "cargo build --manifest-path {manifest_path} --target-dir {target_dir}", |
| 48 | + manifest_path = manifest_path.display(), |
| 49 | + target_dir = target_dir.display(), |
| 50 | + ); |
| 51 | + let clif_build_cmd = format!( |
| 52 | + "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}", |
| 53 | + cargo_clif = cargo_clif.display(), |
| 54 | + manifest_path = manifest_path.display(), |
| 55 | + target_dir = target_dir.display(), |
| 56 | + ); |
| 57 | + |
| 58 | + let bench_compile = |
| 59 | + hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); |
| 60 | + |
| 61 | + spawn_and_wait(bench_compile); |
| 62 | + |
| 63 | + eprintln!("[BENCH RUN] ebobby/simple-raytracer"); |
| 64 | + fs::copy( |
| 65 | + target_dir.join("debug").join(get_file_name("main", "bin")), |
| 66 | + RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_clif", "bin")), |
| 67 | + ) |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + let mut bench_run = hyperfine_command( |
| 71 | + 0, |
| 72 | + run_runs, |
| 73 | + None, |
| 74 | + Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(), |
| 75 | + Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(), |
| 76 | + ); |
| 77 | + bench_run.current_dir(RelPath::BUILD.to_path(dirs)); |
| 78 | + spawn_and_wait(bench_run); |
| 79 | +} |
0 commit comments