Skip to content

Instantly share code, notes, and snippets.

@karthickpdy
Last active December 3, 2018 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthickpdy/a37bd7644a2dabe7b8ad8034733de167 to your computer and use it in GitHub Desktop.
Save karthickpdy/a37bd7644a2dabe7b8ad8034733de167 to your computer and use it in GitHub Desktop.
package com.benchmark;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import javax.script.ScriptException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
public class GraalBenchmark {
Context context1;
Context context2;
Context context3;
static final int FORK_NUM = 3;
static final int ACTUAL_ITERATIONS_NUM = 5;
static final int WARMUP_ITERATIONS = 0;
static final int NUM_THREADS = 1;
CharSequence script = "underscore_js_source_code";
Source source1;
Source source2;
Value eval;
@Setup(value = Level.Invocation)
public void setup() throws IOException {
source1 = Source.newBuilder("js", script, "_").cached(true).build();
source2 = Source.create("js", script);
Engine engine = Engine.create();
Engine e2 = Engine.create();
context1 = Context.newBuilder().engine(engine).allowHostAccess(true).build();
context2 = Context.newBuilder().engine(engine).allowHostAccess(true).build();
context3 = Context.newBuilder().engine(e2).allowHostAccess(true).build();
eval = context1.eval(source1);
}
@Warmup(iterations = WARMUP_ITERATIONS)
@Fork(jvmArgsPrepend = {"-XX:+UnlockExperimentalVMOptions", "-XX:+EnableJVMCI", "-XX:+UseJVMCICompiler",
"-XX:+BootstrapJVMCI", "-Dpolyglot.engine.PreinitializeContexts=js"}, value = FORK_NUM)
@Measurement(iterations = ACTUAL_ITERATIONS_NUM)
@Threads(value = NUM_THREADS)
@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void evaluateGraal1() throws ScriptException {
context2.eval(source1);
}
@Warmup(iterations = WARMUP_ITERATIONS)
@Fork(jvmArgsPrepend = {"-XX:+UnlockExperimentalVMOptions", "-XX:+EnableJVMCI", "-XX:+UseJVMCICompiler",
"-XX:+BootstrapJVMCI", "-Dpolyglot.engine.PreinitializeContexts=js"}, value = FORK_NUM)
@Measurement(iterations = ACTUAL_ITERATIONS_NUM)
@Threads(value = NUM_THREADS)
@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void evaluateGraalFullyInitialized() throws ScriptException {
context3.eval(source2);
}
}
// Benchmark Mode Cnt Score Error Units
// GraalBenchmark.evaluateGraal1 avgt 15 6.206 ± 3.073 ms/op
// GraalBenchmark.evaluateGraalFullyInitialized avgt 15 7.649 ± 3.316 ms/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment