1#![allow(clippy::unwrap_used, missing_docs, clippy::print_stdout)]
5
6use std::time::{Duration, Instant};
7
8use remedy_lua::{Libs, Limits, Lua};
9
10fn source(case: &str) -> String {
11 match case {
12 "do" => format!("{}{}", "do ".repeat(850_000), "end ".repeat(850_000)),
13 "dots" => format!("return a{}", ".b".repeat(3_000_000)),
14 "flat" => format!("return {{{}}}", "1,".repeat(3_000_000)),
15 "locals" => (0..2000).map(|j| format!("local {}\n", (0..200).map(|i| format!("v{}", j * 200 + i)).collect::<Vec<_>>().join(","))).collect(),
16 "keys" => format!("return {{{}}}", (0..500_000).map(|i| format!("a{i}=1,")).collect::<String>()),
17 "strs" => format!("return {{{}}}", (0..600_000).map(|i| format!("\"s{i}\",")).collect::<String>()),
18 "bigstr" => format!("return \"{}\"", "a".repeat(16 << 20)),
19 _ => panic!("unknown case {case}"),
20 }
21}
22
23fn main() {
24 let mut args = std::env::args().skip(1);
25 let cases: Vec<String> = match args.next() {
26 Some(c) if c != "all" => vec![c],
27 _ => ["do", "dots", "flat", "locals", "keys", "strs"].iter().map(|s| (*s).to_owned()).collect(),
28 };
29 let points: u32 = args.next().and_then(|a| a.parse().ok()).unwrap_or(24);
30 let only: Option<u64> = args.next().and_then(|a| a.parse().ok());
32 for case in cases {
33 let src = source(&case);
34 let full = {
35 let mut lua = Lua::new(Limits::unmetered().memory(8 << 30), Libs::NONE);
36 let t = Instant::now();
37 let r = lua.load(&src, "=t").map(|_| ());
38 let e = t.elapsed();
39 assert!(r.is_ok(), "{case}: {r:?}");
40 e
41 };
42 let mut worst = (Duration::ZERO, Duration::ZERO);
43 for k in 1..=points {
44 let at = only.map_or(full * k / (points + 1), Duration::from_millis);
45 let mut lua = Lua::new(Limits::unmetered().memory(8 << 30), Libs::NONE);
46 let h = lua.interrupt_handle();
47 let t = std::thread::spawn(move || {
48 std::thread::sleep(at);
49 h.interrupt();
50 Instant::now()
51 });
52 let started = Instant::now();
53 let r = lua.load(&src, "=t").map(|_| ());
54 let end = Instant::now();
55 let fired = t.join().unwrap();
56 if r.is_err() {
57 let late = end.saturating_duration_since(fired);
58 if std::env::var("FRONTSTOP_VERBOSE").is_ok() {
59 println!(" at {at:?}: {late:?} late, {:?} into the load", fired.saturating_duration_since(started));
60 }
61 if late > worst.0 {
62 worst = (late, at);
63 }
64 }
65 lua.interrupt_handle().reset();
66 let _ = lua.load("return 1", "=ok");
67 }
68 println!("{case:8} {:.1} MB, load {full:?}: worst {:?} late (interrupted at {:?})", src.len() as f64 / 1e6, worst.0, worst.1);
69 }
70}