This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
use std::fs;
|
||||
use std::hint::black_box;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use prometeu_bytecode::SourceSpan;
|
||||
use prometeu_bytecode::assembler::assemble;
|
||||
use prometeu_bytecode::model::{BytecodeModule, DebugInfo, Export, FunctionMeta};
|
||||
use prometeu_host_headless::{AdvanceFramesStop, HeadlessHost};
|
||||
|
||||
const SAMPLE_FRAMES: u64 = 300;
|
||||
const SAMPLE_COUNT: u32 = 10;
|
||||
|
||||
fn main() {
|
||||
let cartridge = benchmark_cartridge();
|
||||
let mut samples = Vec::with_capacity(SAMPLE_COUNT as usize);
|
||||
|
||||
for _ in 0..SAMPLE_COUNT {
|
||||
let mut host = HeadlessHost::new();
|
||||
host.boot(cartridge.to_string_lossy()).expect("boot benchmark cartridge");
|
||||
|
||||
let started = Instant::now();
|
||||
let outcome =
|
||||
black_box(host.advance_frames(SAMPLE_FRAMES)).expect("advance benchmark frames");
|
||||
let elapsed = started.elapsed();
|
||||
|
||||
assert_eq!(outcome.completed, SAMPLE_FRAMES);
|
||||
assert_eq!(outcome.stop, AdvanceFramesStop::Completed);
|
||||
samples.push(elapsed.as_secs_f64());
|
||||
}
|
||||
|
||||
samples.sort_by(f64::total_cmp);
|
||||
let median_seconds = samples[samples.len() / 2];
|
||||
let frames_per_second = SAMPLE_FRAMES as f64 / median_seconds;
|
||||
println!(
|
||||
"headless advance_frames: median={median_seconds:.6}s frames={SAMPLE_FRAMES} throughput={frames_per_second:.2} frames/s samples={SAMPLE_COUNT}"
|
||||
);
|
||||
|
||||
fs::remove_dir_all(&cartridge).expect("remove benchmark cartridge");
|
||||
}
|
||||
|
||||
fn benchmark_cartridge() -> PathBuf {
|
||||
let directory = std::env::temp_dir().join(format!(
|
||||
"prometeu-headless-benchmark-{}-{}",
|
||||
std::process::id(),
|
||||
SystemTime::now().duration_since(UNIX_EPOCH).expect("system clock").as_nanos()
|
||||
));
|
||||
fs::create_dir_all(&directory).expect("create benchmark cartridge");
|
||||
|
||||
let code = assemble("FRAME_SYNC\nRET\n").expect("assemble benchmark program");
|
||||
let module = BytecodeModule {
|
||||
version: 0,
|
||||
const_pool: vec![],
|
||||
functions: vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: code.len() as u32,
|
||||
param_slots: 0,
|
||||
local_slots: 0,
|
||||
return_slots: 0,
|
||||
max_stack_slots: 1,
|
||||
}],
|
||||
code,
|
||||
debug_info: Some(DebugInfo {
|
||||
pc_to_span: vec![(0, SourceSpan { file_id: 1, start: 0, end: 10 })],
|
||||
function_names: vec![(0, "main".into())],
|
||||
}),
|
||||
exports: vec![Export { symbol: "main".into(), func_idx: 0 }],
|
||||
syscalls: vec![],
|
||||
}
|
||||
.serialize();
|
||||
|
||||
fs::write(directory.join("program.pbx"), module).expect("write benchmark program");
|
||||
write_manifest(&directory);
|
||||
directory
|
||||
}
|
||||
|
||||
fn write_manifest(directory: &Path) {
|
||||
fs::write(
|
||||
directory.join("manifest.json"),
|
||||
"{\n \"magic\": \"PMTU\",\n \"cartridge_version\": 1,\n \"app_id\": 90,\n \"title\": \"Headless Temporal Benchmark\",\n \"app_version\": \"0.1.0\",\n \"app_mode\": \"Game\",\n \"capabilities\": []\n}\n",
|
||||
)
|
||||
.expect("write benchmark manifest");
|
||||
}
|
||||
Reference in New Issue
Block a user