Files

84 lines
2.9 KiB
Rust

//! Run in release with --features resource-diagnostics; query setup is excluded.
#[allow(dead_code)]
#[path = "../tests/support/callables.rs"]
mod callables;
use prometeu_bytecode::model::ConstantPoolEntry;
use prometeu_vm::string_provenance_qualification::Fixture;
use std::{
alloc::{GlobalAlloc, Layout, System},
sync::atomic::{AtomicUsize, Ordering},
time::Instant,
};
struct Counting;
static ALLOC: AtomicUsize = AtomicUsize::new(0);
static REALLOC: AtomicUsize = AtomicUsize::new(0);
static FREE: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
ALLOC.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
FREE.fetch_add(1, Ordering::Relaxed);
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
REALLOC.fetch_add(1, Ordering::Relaxed);
unsafe { System.realloc(p, l, n) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn counts() -> [usize; 3] {
[ALLOC.load(Ordering::Relaxed), REALLOC.load(Ordering::Relaxed), FREE.load(Ordering::Relaxed)]
}
fn main() {
for n in [1024, 16384, 65536] {
let constants = vec![ConstantPoolEntry::String("same".into()); n];
let start = Instant::now();
let image = callables::image_constants(
&[("RET", callables::sig(&[], &[]), vec![])],
vec![],
&constants,
)
.unwrap();
let image_ns = start.elapsed().as_nanos();
let payload = image.payload_bytes();
let before = counts();
let start = Instant::now();
let mut fixture = Fixture::new(image, n);
let creation_ns = start.elapsed().as_nanos();
let after = counts();
println!(
"setup n={n} image_load_ns={image_ns} image_payload_bytes={payload} session_ns={creation_ns} session_allocs={} auxiliary_bytes={} work={:?}",
after[0] - before[0],
fixture.auxiliary_bytes(),
fixture.work()
);
for name in [
"image_hit",
"image_miss",
"session_hit",
"session_miss",
"import_hit",
"snapshot_mixed",
"import_miss",
] {
let before = counts();
let start = Instant::now();
for _ in 0..1000 {
fixture.run(name);
}
let ns = start.elapsed().as_nanos();
let after = counts();
let delta = std::array::from_fn::<_, 3, _>(|i| after[i] - before[i]);
if name.ends_with("hit") || name == "image_miss" || name == "session_miss" {
assert_eq!(delta, [0; 3]);
}
println!(
"query n={n} name={name} iterations=1000 ns={ns} alloc_realloc_free={delta:?}"
);
}
}
}