fn context<'rt>(runtime: &'rt quickjs::Runtime) -> quickjs::Context<'rt> {
quickjs::Context::new(runtime)
}
fn measure(source: &str) -> (quickjs::MemoryUsage, quickjs::MemoryUsage) {
let rt = quickjs::Runtime::new();
let mut ctx = context(&rt);
let (before, _) = ctx.memory_usage();
ctx.eval(source)
.unwrap_or_else(|error| panic!("{source:?} failed: {error}"));
let (after, _) = ctx.memory_usage();
(before, after)
}
#[test]
fn objects_and_properties_are_counted_exactly() {
let (before, after) = measure("let a = {}; let b = {}; a.x = 1; a.y = 2; b.z = 3;");
assert_eq!(
after.obj_count - before.obj_count,
2,
"two objects were created; report says {}",
after.obj_count - before.obj_count
);
assert_eq!(
after.prop_count - before.prop_count,
3,
"three properties were defined; report says {}",
after.prop_count - before.prop_count
);
}
#[test]
fn a_top_level_var_is_a_global_property() {
let (before, with_var) = measure("var only_var = 1;");
let (_, with_let) = measure("let only_let = 1;");
assert_eq!(
with_var.prop_count - before.prop_count,
1,
"`var` in a script defines a property on the global object"
);
assert_eq!(
with_let.prop_count - before.prop_count,
0,
"`let` in a script defines a lexical binding, which is not an object property"
);
}
#[test]
fn array_elements_are_not_properties() {
let (before, after) = measure("let a = [1, 2, 3];");
assert_eq!(after.array_count - before.array_count, 1);
assert_eq!(after.fast_array_count - before.fast_array_count, 1);
assert_eq!(
after.fast_array_elements - before.fast_array_elements,
3,
"three elements were stored; report says {}",
after.fast_array_elements - before.fast_array_elements
);
assert_eq!(
after.prop_count - before.prop_count,
0,
"a dense array's elements must not appear as properties"
);
}
#[test]
fn strings_are_counted_with_their_bytes() {
let (before, after) = measure("var s = '0123456789';");
assert_eq!(after.str_count - before.str_count, 1);
let grew = after.str_size - before.str_size;
assert!(
grew >= 10,
"a ten-character string must add at least ten bytes, added {grew}"
);
}
#[test]
fn bytecode_and_native_functions_are_told_apart() {
let (before, after) = measure("function f(a, b) { return a + b; } f(1, 2);");
assert_eq!(
after.js_func_count - before.js_func_count,
1,
"one function was compiled; report says {}",
after.js_func_count - before.js_func_count
);
assert!(after.js_func_code_size > before.js_func_code_size);
assert_eq!(
after.c_func_count, before.c_func_count,
"a script that defines a function must not create C functions"
);
assert!(
after.c_func_count > 100,
"the realm has hundreds of builtins; report says {}",
after.c_func_count
);
}
#[test]
fn a_buffers_bytes_are_counted_once() {
let (before, after) = measure("var b = new ArrayBuffer(1024); var v = new Uint8Array(b);");
assert_eq!(
after.binary_object_count - before.binary_object_count,
2,
"the buffer and its view are two binary objects"
);
assert_eq!(
after.binary_object_size - before.binary_object_size,
1024,
"only the buffer owns bytes; the view must add nothing"
);
}
#[test]
fn the_rows_add_up_to_the_total() {
let rt = quickjs::Runtime::new();
let mut ctx = context(&rt);
ctx.eval("let o = { a: 1, b: 2 }; let v = [1, 2, 3]; let s = 'x'.repeat(100);")
.unwrap();
let (usage, _) = ctx.memory_usage();
assert!(
usage.malloc_size >= usage.prop_size + usage.obj_size,
"objects ({}) plus their property tables ({}) cannot exceed the total ({})",
usage.obj_size,
usage.prop_size,
usage.malloc_size
);
let named = usage.obj_size
+ usage.prop_size
+ usage.str_size
+ usage.js_func_size
+ usage.js_func_code_size
+ usage.binary_object_size;
assert!(
named <= usage.malloc_size,
"categorised rows sum to {named}, more than the total {}",
usage.malloc_size
);
assert!(
named * 4 > usage.malloc_size,
"the rows should name most of the footprint: {named} of {}",
usage.malloc_size
);
}
#[test]
fn the_report_has_the_reference_shape() {
let rt = quickjs::Runtime::new();
let mut ctx = context(&rt);
ctx.eval("var a = [1, 2, 3], o = { p: 1 };").unwrap();
let report = ctx.dump_memory_usage();
assert!(
report.starts_with("QuickJS memory usage -- "),
"the header line is what a side-by-side diff aligns on:\n{report}"
);
assert!(report.contains(", 64-bit, malloc limit: -1"));
assert!(report.contains("JSObject classes"));
assert!(report.contains("NAME COUNT SIZE"));
for row in [
"memory allocated",
"strings",
"objects",
" properties",
" shapes",
"bytecode functions",
" bytecode",
"C functions",
"arrays",
" fast arrays",
" elements",
] {
assert!(
report.contains(row),
"the report is missing the `{row}` row:\n{report}"
);
}
assert!(
report.contains("not accounted for by this engine"),
"a row this engine does not have must be named, not silently absent:\n{report}"
);
}
#[test]
fn the_header_reports_the_memory_limit() {
let mut rt = quickjs::Runtime::new();
rt.set_memory_limit(Some(64 * 1024 * 1024));
let mut ctx = context(&rt);
let report = ctx.dump_memory_usage();
assert!(
report.contains("malloc limit: 67108864"),
"the limit has to reach the header:\n{report}"
);
}
#[test]
fn a_zero_limit_is_not_an_absent_limit() {
let mut rt = quickjs::Runtime::new();
rt.set_memory_limit(Some(0));
let (usage, census) = quickjs::memory::compute(rt.heap());
assert_eq!(usage.malloc_limit, 0);
let report = quickjs::memory::dump(&usage, &census);
assert!(
report.contains(", malloc limit: 0\n"),
"zero means zero, not -1:\n{report}"
);
}