Skip to content

Commit 09042d2

Browse files
authored
Merge pull request #4 from DragonRuby/bytecode-bench
Run VM benchmarks via bytecode
2 parents f18e554 + 8473259 commit 09042d2

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ repos:
2020
rev: 'v19.1.4'
2121
hooks:
2222
- id: clang-format
23+
- repo: https://github.com/psf/black-pre-commit-mirror
24+
rev: 24.10.0
25+
hooks:
26+
- id: black

tests/benchmarks/CMakeLists.txt

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
function(add_bench_executable ruby)
2+
set(full_ruby_path ${CMAKE_CURRENT_LIST_DIR}/${ruby})
3+
set(host_c ${CMAKE_CURRENT_BINARY_DIR}/bytecode_${ruby}.c)
4+
set(target_name bench_${ruby}.exe)
5+
add_custom_command(
6+
OUTPUT ${host_c}
7+
COMMAND $<TARGET_FILE:mrbc_binary> -Blightstorm_bench -o ${host_c}
8+
${full_ruby_path}
9+
DEPENDS ${full_ruby_path})
10+
add_executable(${target_name} benchmarks_bytecode_main.c ${host_c})
11+
target_include_directories(
12+
${target_name}
13+
PRIVATE ${CMAKE_SOURCE_DIR}/third_party/mruby/include
14+
${CMAKE_SOURCE_DIR}/third_party/mruby/build/host/include)
15+
target_compile_options(${target_name} PRIVATE -g)
16+
target_link_libraries(${target_name} PRIVATE mruby_static)
17+
add_dependencies(${target_name} mruby_static)
18+
endfunction()
19+
120
file(
221
GLOB files
322
RELATIVE ${CMAKE_CURRENT_LIST_DIR}
423
"${CMAKE_CURRENT_LIST_DIR}/*.rb")
24+
525
foreach(file ${files})
626
add_lightstorm_executable(${file})
27+
add_bench_executable(${file})
728
set(bench_targets ${bench_targets} $<TARGET_FILE:${file}.exe>
8-
${CMAKE_CURRENT_LIST_DIR}/${file})
29+
$<TARGET_FILE:bench_${file}.exe>)
930
endforeach()
31+
1032
add_custom_target(
1133
run-benchmarks COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/bench.py
12-
$<TARGET_FILE:mruby_binary> ${bench_targets})
34+
${bench_targets})

tests/benchmarks/bench.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33

44
mruby_binary = sys.argv[1]
55

6-
for idx in range(2, len(sys.argv), 2):
6+
for idx in range(1, len(sys.argv), 2):
77
bin = sys.argv[idx]
8-
rb_full = sys.argv[idx + 1]
9-
rb_name = rb_full.split('/')[-1]
8+
bin_bytecode = sys.argv[idx + 1]
9+
rb_name = bin_bytecode.split("/")[-1].split(".")[0]
1010
print("Benchmarking " + rb_name)
11-
r = subprocess.run([
12-
'hyperfine',
13-
'--warmup', '1',
14-
# Calling compiled binary
15-
'-n', 'lightstorm ' + rb_name, bin,
16-
# Calling mruby against the original file
17-
'-n', 'mruby ' + rb_name, mruby_binary + ' ' + rb_full
18-
], capture_output=True)
19-
summary = r.stdout.decode('utf8').split("Summary")[-1].splitlines()
11+
r = subprocess.run(
12+
[
13+
"hyperfine",
14+
"--warmup",
15+
"1",
16+
# Calling compiled binary
17+
"-n",
18+
"lightstorm " + rb_name,
19+
bin,
20+
# Calling compiled binary (mruby bytecode)
21+
"-n",
22+
"mruby " + rb_name,
23+
bin_bytecode,
24+
],
25+
capture_output=True,
26+
)
27+
summary = r.stdout.decode("utf8").split("Summary")[-1].splitlines()
2028
summary = [s.strip() for s in summary]
2129
print(" ".join(summary).strip())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <mruby.h>
2+
#include <mruby/irep.h>
3+
#include <mruby/proc.h>
4+
5+
extern const uint8_t lightstorm_bench[];
6+
7+
int main() {
8+
mrb_state *mrb = mrb_open();
9+
mrb_load_irep(mrb, lightstorm_bench);
10+
mrb_close(mrb);
11+
return 0;
12+
}

0 commit comments

Comments
 (0)