1#![allow(clippy::unwrap_used, clippy::expect_used, missing_docs, clippy::indexing_slicing, clippy::panic)]
5use std::sync::Arc;
6use std::time::{Duration, Instant};
7
8use remedy_lua::{Libs, Limits, Lua};
9
10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 let setup = a.get(1).cloned().unwrap_or_default();
13 let body = a.get(2).cloned().unwrap_or_default();
14 let ms: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(10);
15 let mem: usize = a.get(4).and_then(|s| s.parse().ok()).unwrap_or(512) << 20;
16 let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
17 let mut worst = Duration::ZERO;
18 let mut last = String::new();
19 for _ in 0..5 {
20 let mut lua = Lua::new(Limits::default().memory(mem).string_len(64 << 20).fuel(1 << 40).hash_seed(7), libs);
21 lua.set_print(|_| {});
22 lua.exec(&setup, "=setup").unwrap();
23 let t0 = Instant::now();
24 let clock: remedy_lua::Clock = Arc::new(move || t0.elapsed());
25 lua.set_time_limit(Some((clock, Duration::from_millis(ms))));
26 let f = lua.load(&body, "=body").unwrap();
27 let start = Instant::now();
28 let r = lua.call(f.value(), &[]);
29 let el = start.elapsed();
30 worst = worst.max(el.saturating_sub(Duration::from_millis(ms)));
31 last = format!("{:?}", r.err());
32 }
33 println!("worst overrun {:?} past {ms} ms: {}", worst, &last[..last.len().min(120)]);
34}