From 464c20c33400f3830331a578924c06356b6698bc Mon Sep 17 00:00:00 2001 From: bytedream Date: Sat, 1 Mar 2025 16:16:44 +0100 Subject: [PATCH] add benchmark script --- .gitignore | 3 + benchmarks/bench.sh | 120 +++++++++ benchmarks/fengari/main.js | 28 ++ benchmarks/fengari/package-lock.json | 62 +++++ benchmarks/fengari/package.json | 10 + benchmarks/wasm/Cargo.lock | 373 +++++++++++++++++++++++++++ benchmarks/wasm/Cargo.toml | 10 + benchmarks/wasm/build.rs | 25 ++ benchmarks/wasm/main.js | 32 +++ benchmarks/wasm/src/lib.rs | 25 ++ 10 files changed, 688 insertions(+) create mode 100755 benchmarks/bench.sh create mode 100644 benchmarks/fengari/main.js create mode 100644 benchmarks/fengari/package-lock.json create mode 100644 benchmarks/fengari/package.json create mode 100644 benchmarks/wasm/Cargo.lock create mode 100644 benchmarks/wasm/Cargo.toml create mode 100644 benchmarks/wasm/build.rs create mode 100644 benchmarks/wasm/main.js create mode 100644 benchmarks/wasm/src/lib.rs diff --git a/.gitignore b/.gitignore index 0fe3645..a1d8806 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ book +/node_modules/ /target/ + +benchmarks/benches diff --git a/benchmarks/bench.sh b/benchmarks/bench.sh new file mode 100755 index 0000000..0ed12c4 --- /dev/null +++ b/benchmarks/bench.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash + +if [ ! -d benches ]; then + echo "Benchmark sources not found, downloading..." + mkdir benches + curl -L -o benches/benchmarksgame-sourcecode.zip https://salsa.debian.org/benchmarksgame-team/benchmarksgame/-/raw/3889723f05b20fe9008d7c987f832fe5d5db5bf1/public/download/benchmarksgame-sourcecode.zip + unzip -d benches/ benches/benchmarksgame-sourcecode.zip + echo "Downloaded benchmark sources" +fi + +if [ ! -d "fengari/node_modules" ]; then + echo "Node modules not pulled, pulling" + npm --prefix ./fengari install + echo "Node modules pulled" +fi + +if [ ! -f "wasm/target/wasm32-unknown-emscripten/release/wasm.js" ]; then + echo "Wasm module not built, building it..." + cargo build --manifest-path ./wasm/Cargo.toml --release --target wasm32-unknown-emscripten + echo "Wasm module built" +fi + +bench() { + target="$1" + file="$2" + args=${@:3} + + secs="" + cpu_secs="" + mem="" + + for _ in {1..5}; do + output=$(/usr/bin/time -f '%U %S %e %M' node "$target/main.js" "$(cat $file)" $args 2>&1> /dev/null) + if [ $? != 0 ]; then + echo "Benchmark exited unexpectedly: $output" + return 1; + fi + secs="${secs} $(echo "$output" | awk '{ print $1 + $2 }')" + cpu_secs="${cpu_secs} $(echo "$output" | awk '{ print $3 }')" + mem="${mem} $(echo "$output" | awk '{ print $4 }')" + done + + mid_secs=$(echo "$secs" | awk '{ printf "%.3f", ($1 + $2 + $3 + $4 + $5) / 5 }') + mid_cpu_secs=$(echo "$cpu_secs" | awk '{ printf "%.3f", ($1 + $2 + $3 + $4 + $5) / 5 }') + mid_mem=$(echo "$mem" | awk '{ printf "%.0f", ($1 + $2 + $3 + $4 + $5) / 5 }') + + echo "$mid_secs $mid_cpu_secs $mid_mem" +} + +bench_and_print() { + name="$1" + directory="$2" + files="$3" + args=${@:4} + + # "array" of [#num] + bench_results="" + for file in $files; do + num=$(echo $file | grep -oP '(?<=lua-)\d(?=.lua)') + + for target in fengari wasm; do + result=$(bench "$target" "$directory/$file" $args) + + if [ $? != 0 ]; then + echo "$result" + return 1; + fi + + if [ -z "${num}" ]; then + bench_results="${bench_results}\n$result $target" + else + bench_results="${bench_results}\n$result $target $num" + fi + done + done + + bench_results=$(printf "$bench_results" | sed 1d | sort -k1 -n) + best_bench_secs=$(printf "$bench_results" | head -1 | awk '{ print $1 }') + + echo "#### $name" + echo "| x | source | secs | mem(kb) | cpu secs |" + echo "| - | ------ | ---- | --- | -------- |" + printf "$bench_results\n" | while read line; do + secs=$(echo "$line" | awk '{ print $1 }') + cpu_secs=$(echo "$line" | awk '{ print $2 }') + mem=$(echo "$line" | awk '{ print $3 }') + target=$(echo "$line" | awk '{ print $4 }') + number=$(echo "$line" | awk '{ print $5 }') + + secs_ratio=$(echo "$secs $best_bench_secs" | awk '{ printf "%.3f\n", $1 / $2 }') + + if [ -z "$number" ]; then + echo "| $secs_ratio | $target | $secs | $mem | $cpu_secs |" + else + echo "| $secs_ratio | $target #$number | $secs | $mem | $cpu_secs |" + fi + done + + if [ -z "${args}" ]; then + echo "###### [$name](https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/$(basename $directory)) with default arguments" + else + echo "###### [$name](https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/$(basename $directory)) with argument(s): $args" + fi +} + +echo "Starting benchmarks" + +bench_and_print "binary-trees" benches/binarytrees "binarytrees.lua-2.lua" # binarytrees.lua-3.lua and binarytrees.lua-4.lua are using io.popen for multiprocessing +# bench_and_print "fannkuch-redux" benches/fannkuchredux "fannkuchredux.lua" # broken +bench_and_print "fasta" benches/fasta "fasta.lua-2.lua fasta.lua-3.lua" +# bench_and_print "knucleotide" benches/knucleotide "knucleotide.lua-2.lua" # needs to read from stdin +bench_and_print "mandelbrot" benches/mandelbrot "mandelbrot.lua mandelbrot.lua-2.lua mandelbrot.lua-3.lua" # mandelbrot.lua-6.lua is using io.popen for multiprocessing +bench_and_print "n-body" benches/nbody "nbody.lua nbody.lua-2.lua nbody.lua-4.lua" +# bench_and_print "nbody-inprocess" benches/nbodyinprocess "" # has no lua implementation +# bench_and_print "pidigits" benches/pidigits "pidigits.lua pidigits.lua-5.lua pidigits.lua-7.lua" # all need external 'c-gmp' module +# bench_and_print "regex-redux" benches/regexredux "regexredux.lua" # needs external 'rex_pcre2' module +# bench_and_print "reverse-complement" benches/revcomp "revcomp.lua-2.lua revcomp.lua-4.lua revcomp.lua-5.lua" # all need to read from stdin +bench_and_print "spectral-norm" benches/spectralnorm "spectralnorm.lua" + +echo "Benchmarks finished" diff --git a/benchmarks/fengari/main.js b/benchmarks/fengari/main.js new file mode 100644 index 0000000..c79bd47 --- /dev/null +++ b/benchmarks/fengari/main.js @@ -0,0 +1,28 @@ +import fengari from 'fengari'; + +if (process.argv.length < 3) { + console.error('Invalid number of arguments'); + process.exit(1); +} + +const program = process.argv[2]; +const args = process.argv.slice(3); + +const lauxlib = fengari.lauxlib; +const lua = fengari.lua; +const lualib = fengari.lualib; + +const L = lauxlib.luaL_newstate(); +lualib.luaL_openlibs(L); + +// set 'arg' (argv) value, this allows to manipulate the behavior of the scripts +lauxlib.luaL_newlibtable(L); +lua.lua_pushstring(L, 'lua'); +lua.lua_rawseti(L, -2, 0); +for (let i = 0; i < args.length; i++) { + lua.lua_pushstring(L, args[i]); + lua.lua_rawseti(L, -2, i + 1); +} +lua.lua_setglobal(L, 'arg') + +lauxlib.luaL_dostring(L, fengari.to_luastring(program)); diff --git a/benchmarks/fengari/package-lock.json b/benchmarks/fengari/package-lock.json new file mode 100644 index 0000000..4f3e8b6 --- /dev/null +++ b/benchmarks/fengari/package-lock.json @@ -0,0 +1,62 @@ +{ + "name": "fengari", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "fengari", + "version": "1.0.0", + "dependencies": { + "fengari": "=0.1.4" + } + }, + "node_modules/fengari": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/fengari/-/fengari-0.1.4.tgz", + "integrity": "sha512-6ujqUuiIYmcgkGz8MGAdERU57EIluGGPSUgGPTsco657EHa+srq0S3/YUl/r9kx1+D+d4rGfYObd+m8K22gB1g==", + "license": "MIT", + "dependencies": { + "readline-sync": "^1.4.9", + "sprintf-js": "^1.1.1", + "tmp": "^0.0.33" + } + }, + "node_modules/os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/readline-sync": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "license": "MIT", + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/sprintf-js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", + "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==", + "license": "BSD-3-Clause" + }, + "node_modules/tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", + "license": "MIT", + "dependencies": { + "os-tmpdir": "~1.0.2" + }, + "engines": { + "node": ">=0.6.0" + } + } + } +} diff --git a/benchmarks/fengari/package.json b/benchmarks/fengari/package.json new file mode 100644 index 0000000..b91aae6 --- /dev/null +++ b/benchmarks/fengari/package.json @@ -0,0 +1,10 @@ +{ + "name": "fengari", + "version": "1.0.0", + "description": "", + "type": "module", + "dependencies": { + "fengari": "=0.1.4" + }, + "private": true +} diff --git a/benchmarks/wasm/Cargo.lock b/benchmarks/wasm/Cargo.lock new file mode 100644 index 0000000..4dbc1e3 --- /dev/null +++ b/benchmarks/wasm/Cargo.lock @@ -0,0 +1,373 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "bstr" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "cc" +version = "1.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "either" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d" + +[[package]] +name = "env_home" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" + +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "libc" +version = "0.2.170" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "lua-src" +version = "547.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edaf29e3517b49b8b746701e5648ccb5785cde1c119062cbabbc5d5cd115e42" +dependencies = [ + "cc", +] + +[[package]] +name = "luajit-src" +version = "210.5.12+a4f56a4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a8e7962a5368d5f264d045a5a255e90f9aa3fc1941ae15a8d2940d42cac671" +dependencies = [ + "cc", + "which", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mlua" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3f763c1041eff92ffb5d7169968a327e1ed2ebfe425dac0ee5a35f29082534b" +dependencies = [ + "bstr", + "either", + "mlua-sys", + "num-traits", + "parking_lot", + "rustc-hash", +] + +[[package]] +name = "mlua-sys" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1901c1a635a22fe9250ffcc4fcc937c16b47c2e9e71adba8784af8bca1f69594" +dependencies = [ + "cc", + "cfg-if", + "lua-src", + "luajit-src", + "pkg-config", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" + +[[package]] +name = "wasm" +version = "0.1.0" +dependencies = [ + "mlua", +] + +[[package]] +name = "which" +version = "7.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2774c861e1f072b3aadc02f8ba886c26ad6321567ecc294c935434cad06f1283" +dependencies = [ + "either", + "env_home", + "rustix", + "winsafe", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" diff --git a/benchmarks/wasm/Cargo.toml b/benchmarks/wasm/Cargo.toml new file mode 100644 index 0000000..f109379 --- /dev/null +++ b/benchmarks/wasm/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "wasm" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +mlua = { version = "=0.10.3", features = ["lua53", "vendored"] } diff --git a/benchmarks/wasm/build.rs b/benchmarks/wasm/build.rs new file mode 100644 index 0000000..6f856af --- /dev/null +++ b/benchmarks/wasm/build.rs @@ -0,0 +1,25 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let pkg_name = env::var("CARGO_PKG_NAME").unwrap(); + let target_path = PathBuf::from(out_dir) + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .to_path_buf(); + + println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall','setValue','stringToUTF8']"); + println!("cargo:rustc-link-arg=-sEXPORTED_FUNCTIONS=['_execute','_malloc','_free']"); + println!("cargo:rustc-link-arg=-sEXPORT_ES6=1"); + println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0"); + println!("cargo:rustc-link-arg=--no-entry"); + println!( + "cargo:rustc-link-arg=-o{}.js", + target_path.join(pkg_name).to_string_lossy() + ); +} diff --git a/benchmarks/wasm/main.js b/benchmarks/wasm/main.js new file mode 100644 index 0000000..8ea48a3 --- /dev/null +++ b/benchmarks/wasm/main.js @@ -0,0 +1,32 @@ +if (process.argv.length < 3) { + console.error('Invalid number of arguments'); + process.exit(1); +} + +const program = process.argv[2]; +const args = process.argv.slice(3); + +const wasm = await import('./target/wasm32-unknown-emscripten/release/wasm.js'); +const module = { + print: (str) => console.log(str), + printErr: (str) => console.error(str), +} + +const Module = await wasm.default(module); + +// allocate strings to be able to pass them +const arg_pointers = args.map(a => { + const arg_pointer = Module._malloc(a.length + 1); // +1 for \0 + Module.stringToUTF8(a, arg_pointer, a.length + 1); + return arg_pointer; +}) +const args_pointer = Module._malloc(args.length * 4); // 4 bytes for each pointer +arg_pointers.forEach((pointer, i) => { + Module.setValue(args_pointer + i * 4, pointer, 'i32') +}); + +Module.ccall('execute', null, ['string', 'number', 'array'], [program, args.length, arg_pointers]) + +// free allocated strings +arg_pointers.forEach(pointer => Module._free(pointer)) +Module._free(arg_pointers) diff --git a/benchmarks/wasm/src/lib.rs b/benchmarks/wasm/src/lib.rs new file mode 100644 index 0000000..61fe43f --- /dev/null +++ b/benchmarks/wasm/src/lib.rs @@ -0,0 +1,25 @@ +#![warn(clippy::missing_safety_doc)] + +use mlua::Lua; +use std::ffi::{c_char, c_int, CStr}; + +#[no_mangle] +pub unsafe extern "C" fn execute(program: *const c_char, argc: c_int, argv: *const *const c_char) { + let lua = Lua::new(); + let lua_globals = lua.globals(); + + // set 'arg' (argv) value, this allows to manipulate the behavior of the scripts + let arg = lua.create_table().unwrap(); + arg.set(-1, "mlua").unwrap(); + arg.set(0, "execute").unwrap(); + for i in 0..argc { + let argv_arg = CStr::from_ptr(*argv.offset(i as isize)); + arg.push(argv_arg.to_str().unwrap()).unwrap(); + } + lua_globals.set("arg", arg).unwrap(); + + let exec = CStr::from_ptr(program).to_str().unwrap(); + if let Err(err) = lua.load(exec).exec() { + eprintln!("{}", err) + } +}