v86/tests/benchmark/arch-python.js
Fabian 23411f337e use emulator.destroy() over emulator.stop() to properly remove v86 instances
emulator.stop sometimes works (when the GC can figure out that .start()
is unreachable and nothing can call into the instance). However, some
resources, such as any WebSocket connection, need to be closed manually.
2024-12-20 10:41:09 -07:00

72 lines
1.7 KiB
JavaScript
Executable file

#!/usr/bin/env node
"use strict";
const BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;
const { V86, print_stats } = require(`../../build/${BENCH_COLLECT_STATS ? "libv86-debug" : "libv86"}.js`);
const path = require("path");
const V86_ROOT = path.join(__dirname, "../..");
const emulator = new V86({
bios: { url: path.join(V86_ROOT, "/bios/seabios.bin") },
vga_bios: { url: path.join(V86_ROOT, "/bios/vgabios.bin") },
autostart: true,
memory_size: 512 * 1024 * 1024,
vga_memory_size: 8 * 1024 * 1024,
network_relay_url: "<UNUSED>",
initial_state: { url: path.join(V86_ROOT, "/images/arch_state.bin") },
filesystem: { baseurl: path.join(V86_ROOT, "/images/arch/") },
disable_jit: +process.env.DISABLE_JIT,
log_level: 0,
});
emulator.bus.register("emulator-started", function()
{
emulator.create_file("/bench.py", Buffer.from(`
def fib(n):
if n < 2:
return n
return fib(n-2) + fib(n-1)
n = 30
print("fib(", n, ")= ", fib(n))
`));
setTimeout(() => {
emulator.serial0_send(`python3 /bench.py > /dev/null && python /bench.py > /dev/null && time python /bench.py\n`);
}, 1000);
});
var line = "";
emulator.add_listener("serial0-output-byte", function(byte)
{
var chr = String.fromCharCode(byte);
if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
{
return;
}
if(chr === "\n")
{
console.log("%s", line);
if(line.startsWith("sys"))
{
emulator.destroy();
if(BENCH_COLLECT_STATS)
{
const cpu = emulator.v86.cpu;
console.log(print_stats.stats_to_string(cpu));
}
}
line = "";
}
else
{
line += chr;
}
});