Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ path = "rust/benches/normal/io_bench.rs"
harness = false
required-features = ["native"]

[[bench]]
name = "function_call_bench"
path = "rust/benches/normal/function_call_bench.rs"
harness = false
required-features = ["native"]

# Optimize Benches
[[bench]]
name = "add_bench_optimize"
Expand Down Expand Up @@ -101,6 +107,12 @@ path = "rust/benches/optimize/io_bench.rs"
harness = false
required-features = ["native"]

[[bench]]
name = "function_call_bench_optimize"
path = "rust/benches/optimize/function_call_bench.rs"
harness = false
required-features = ["native"]

[dependencies]
napi = { version = "3.9", features = ["napi4", "serde", "serde-json"], optional = true }
napi-derive = { version = "3.5", optional = true }
Expand Down
51 changes: 51 additions & 0 deletions rust/benches/normal/function_call_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

use lightvm::{
LightVM,
types::{
capability::Capability, runtime_config::RuntimeConfig, vmconfig::VmConfig,
},
};

fn config() -> VmConfig {
VmConfig {
caps: vec![Capability::Control, Capability::Debug, Capability::Observe],
runtime_config: Some(RuntimeConfig { nightly: true }),
..Default::default()
}
}

fn main() {
let mut vm = LightVM::new(config());
let raw = r#"[
["jump", 7],
["func", "add", 2, 2, 6, "a", "b"],
["get", "a"],
["get", "b"],
["add", "int"],
["return"],
["stop"],
["export", "add"]
]"#;
let benchmark = vm
.tools()
.bench("function_call_bench")
.expect("benchmark requires debug capability");
benchmark.run(
|| {
let mut vm = LightVM::new(config());
vm.load(raw);
let function = vm.export("add".to_string());
(vm, function)
},
|(vm, function)| function.call(vm, vec![5.into(), 6.into()]),
);
}
52 changes: 52 additions & 0 deletions rust/benches/optimize/function_call_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

use lightvm::{
LightVM,
types::{
capability::Capability, runtime_config::RuntimeConfig, vmconfig::VmConfig,
},
};

fn config() -> VmConfig {
VmConfig {
caps: vec![Capability::Control, Capability::Debug, Capability::Observe],
runtime_config: Some(RuntimeConfig { nightly: true }),
..Default::default()
}
}

fn main() {
let mut vm = LightVM::new(config());
let raw = r#"[
["jump", 7],
["func", "add", 2, 2, 6, "a", "b"],
["get", "a"],
["get", "b"],
["add", "int"],
["return"],
["stop"],
["export", "add"]
]"#;
let tools = vm.tools();
let optimized = tools.optimize_bytecode(raw);
let benchmark = tools
.bench("function_call_bench")
.expect("benchmark requires debug capability");
benchmark.run(
|| {
let mut vm = LightVM::new(config());
vm.load(optimized.clone());
let function = vm.export("add".to_string());
(vm, function)
},
|(vm, function)| function.call(vm, vec![5.into(), 6.into()]),
);
}
9 changes: 6 additions & 3 deletions scripts/bench/ts-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ console.log(
);
execSync('cargo build --release --features node', {
stdio: 'inherit',
cwd: path.resolve(__dirname, '..'),
cwd: path.resolve(__dirname, '../..'),
});
console.log(
`${s.bold}${s.green}✔${s.reset} ${s.bold}Rust build success!${s.reset}\n`,
Expand Down Expand Up @@ -67,8 +67,11 @@ console.log(
console.log(
`${s.bold}${s.cyan}⚡${s.reset} ${s.bold}Running benchmarks...${s.reset}`,
);
const targetDir = path.join(__dirname, '../ts', 'benchmarks');
const files = fs.readdirSync(targetDir).filter((file) => file.endsWith('.ts'));
const targetDir = path.join(rootDir, 'ts', 'benchmarks');
const files = fs
.readdirSync(targetDir, { recursive: true })
.filter((file) => file.endsWith('.ts'))
.sort();

files.forEach((file) => {
console.log(
Expand Down
31 changes: 31 additions & 0 deletions ts/benchmarks/normal/add_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
});

const vm = new LightVM(config());
const raw = [
['val', 'x'],
['push', 5],
['push', 8],
['add', 'i16'],
['set', 'x'],
];

vm.tools()
.bench('add_bench')
.run(
() => new LightVM(config()).load(raw),
(benchmarkVm) => benchmarkVm.run(),
);
29 changes: 29 additions & 0 deletions ts/benchmarks/normal/assign_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
});

const vm = new LightVM(config());
const raw = [
['val', 'x'],
['push', 5],
['set', 'x'],
];

vm.tools()
.bench('assign_bench')
.run(
() => new LightVM(config()).load(raw),
(benchmarkVm) => benchmarkVm.run(),
);
30 changes: 30 additions & 0 deletions ts/benchmarks/normal/concat_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
});

const vm = new LightVM(config());
const raw = [
['val', 'x'],
['push', 'Hello from '],
['push', 'LightVM!'],
['set', 'x'],
];

vm.tools()
.bench('concat_bench')
.run(
() => new LightVM(config()).load(raw),
(benchmarkVm) => benchmarkVm.run(),
);
32 changes: 32 additions & 0 deletions ts/benchmarks/normal/dead_code_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
});

const vm = new LightVM(config());
const raw = [
['push', 5],
['push', 8],
['add', 'i16'],
['val', 'x'],
['push', 9],
['set', 'x'],
];

vm.tools()
.bench('dead_code_bench')
.run(
() => new LightVM(config()).load(raw),
(benchmarkVm) => benchmarkVm.run(),
);
37 changes: 37 additions & 0 deletions ts/benchmarks/normal/function_call_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
runtimeConfig: { nightly: true },
});
const raw = [
['jump', 7],
['func', 'add', 2, 2, 6, 'a', 'b'],
['get', 'a'],
['get', 'b'],
['add', 'int'],
['return'],
['stop'],
['export', 'add'],
];
const vm = new LightVM(config());

vm.tools()
.bench('function_call_bench')
.run(
() => {
const benchmarkVm = new LightVM(config()).load(raw);
return { vm: benchmarkVm, function: benchmarkVm.export('add') };
},
(state) => state.function.call(5, 6),
);
25 changes: 25 additions & 0 deletions ts/benchmarks/normal/io_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2026 SoTeen Studio
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { Capability, LightVM } from '../../../dist/index.min.mjs';

const config = () => ({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
});

const vm = new LightVM(config());
const raw = [['push', 'Hello from LightVM!'], ['println']];

vm.tools()
.bench('io_bench')
.run(
() => new LightVM(config()).load(raw),
(benchmarkVm) => benchmarkVm.run(),
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { LightVM, Capability } from '../../dist/index.min.mjs';
import { LightVM, Capability } from '../../../dist/index.min.mjs';
function runBenchmark() {
const vm = new LightVM({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { LightVM, Capability } from '../../dist/index.min.mjs';
import { LightVM, Capability } from '../../../dist/index.min.mjs';
function runBenchmark() {
const vm = new LightVM({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { LightVM, Capability } from '../../dist/index.min.mjs';
import { LightVM, Capability } from '../../../dist/index.min.mjs';
function runBenchmark() {
const vm = new LightVM({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { LightVM, Capability } from '../../dist/index.min.mjs';
import { LightVM, Capability } from '../../../dist/index.min.mjs';
function runBenchmark() {
const vm = new LightVM({
caps: [Capability.Control, Capability.Debug, Capability.Observe],
Expand All @@ -17,6 +17,9 @@ function runBenchmark() {
['push', 5],
['push', 8],
['add', 'i16'],
['val', 'x'],
['push', 9],
['set', 'x'],
];
const tools = vm.tools();
const optimized = tools.optimizeBytecode(raw);
Expand Down
Loading
Loading