Created
November 26, 2021 14:38
-
-
Save audacioustux/7d905efb14a0912badd5f58200167c48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import org.graalvm.polyglot.*; | |
import org.graalvm.polyglot.io.ByteSequence; | |
class WasmModule { | |
private Context ctx; | |
public WasmModule(Engine engine, Source wasmSource) throws IOException { | |
ctx = Context.newBuilder().engine(engine).option("wasm.Builtins", "wasi_snapshot_preview1").allowAllAccess(true) | |
.build(); | |
ctx.eval(wasmSource); | |
} | |
public Value exec() { | |
return ctx.getBindings("wasm").getMember("main").getMember("run"); | |
} | |
} | |
class PolyglotEval { | |
PolyglotEval(int n) throws IOException, InterruptedException { | |
Thread.sleep(10000); | |
TimeDiff polyCtx_timer = new TimeDiff("Engine, Source init", TimeDiff.DiffType.US); | |
byte[] wasmBinary = Files | |
// .readAllBytes(Path.of("../fromscratch1/src/main/rust/target/wasm32-wasi/release/rust.opt.wasm")); | |
.readAllBytes(Path.of("../fromscratch1/src/main/wasm/dummy.opt.wasm")); | |
Source wasmSource = Source.newBuilder("wasm", ByteSequence.create(wasmBinary), "test.wasm").build(); | |
Engine engine = Engine.newBuilder().option("engine.Mode", "throughput").build(); | |
// Context polyCtx = Context.newBuilder().engine(engine).option("engine.Mode", | |
// "throughput") | |
// .option("wasm.Builtins", | |
// "wasi_snapshot_preview1").allowAllAccess(true).build(); | |
polyCtx_timer.stop(); | |
TimeDiff module_eval_total_timer = new TimeDiff("module eval total", TimeDiff.DiffType.US); | |
ArrayList<Value> wasmModuleFns = new ArrayList<Value>(); | |
for (int i = 0; i < n; i++) { | |
Value fn = (new WasmModule(engine, wasmSource)).exec(); | |
wasmModuleFns.add(fn); | |
} | |
module_eval_total_timer.stop(); | |
TimeDiff warmup_exec_total_timer = new TimeDiff("warmup lang exec total", TimeDiff.DiffType.US); | |
for (Value fn : wasmModuleFns) { | |
for (int j = 0; j < 100; j++) { | |
fn.execute(); | |
} | |
} | |
warmup_exec_total_timer.stop(); | |
Thread.sleep(5000); | |
TimeDiff exec_total_timer = new TimeDiff("lang exec total", TimeDiff.DiffType.US); | |
for (Value fn : wasmModuleFns) { | |
fn.execute(); | |
} | |
exec_total_timer.stop(); | |
} | |
public static void main(String[] args) throws IOException, InterruptedException { | |
final var n = (args.length != 0) ? Integer.parseInt(args[0]) : 1; | |
new PolyglotEval(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment