Skip to content

Instantly share code, notes, and snippets.

@karthickpdy
Last active December 7, 2018 12:08
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/40c263addcabf6a469e62b67d7c96d74 to your computer and use it in GitHub Desktop.
Save karthickpdy/40c263addcabf6a469e62b67d7c96d74 to your computer and use it in GitHub Desktop.
package com.benchmark;
import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.graalvm.polyglot.Context;
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.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
public class FunctionBenchmark {
Context evaluateGraalWithoutJVMargsContext;
Context evaluateGraalContexInitializedContext;
Context evaluateGraalFullyInitializedContext;
Value evaluateGraalWithoutJVMargsCallable;
Value evaluateGraalContexInitializedCallable;
Value evaluateGraalFullyInitializedCallable;
ScriptEngine nashornEngine;
ScriptObjectMirror nashornCallable;
V8 v8runtime;
V8Array v8Params;
static final int FORK_NUM = 2;
static final int ACTUAL_ITERATIONS_NUM = 5;
static final int WARMUP_ITERATIONS = 1;
static final int NUM_THREADS = 1;
@Setup(value = Level.Trial)
public void setup() {
evaluateGraalWithoutJVMargsContext = Context.newBuilder().allowHostAccess(true).build();
evaluateGraalContexInitializedContext = Context.newBuilder().allowHostAccess(true).build();
evaluateGraalFullyInitializedContext = Context.newBuilder().allowHostAccess(true).build();
nashornEngine = new ScriptEngineManager().getEngineByName("Nashorn");
String function = "function(x){ var sum = 0; for(var i=0;i<x;i++){ if(i%2 == 0) {sum += i;} else { sum += 2 }" +
" } \n" +
"return sum;}";
String v8Function = "function increment(x){ var sum = 0; for(var i=0;i<x;i++){ if(i%2 == 0) {sum += i;} else { sum += 2 }" +
" } \n" +
"return sum;}";
evaluateGraalWithoutJVMargsCallable = evaluateGraalWithoutJVMargsContext.eval("js", function);
evaluateGraalContexInitializedCallable = evaluateGraalContexInitializedContext.eval("js", function);
evaluateGraalFullyInitializedCallable = evaluateGraalFullyInitializedContext.eval("js", function);
try {
nashornCallable = (ScriptObjectMirror) nashornEngine.eval(function);
} catch (ScriptException e) {
e.printStackTrace();
}
v8runtime = V8.createV8Runtime();
v8runtime.executeScript(v8Function);
v8Params = new V8Array(v8runtime).push(100);
}
@Warmup(iterations = WARMUP_ITERATIONS)
@Measurement(iterations = ACTUAL_ITERATIONS_NUM)
@Fork(value = FORK_NUM)
@Threads(value = NUM_THREADS)
@org.openjdk.jmh.annotations.Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void evaluateNashorn() throws ScriptException {
nashornCallable.call(null, 100);
}
@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 {
evaluateGraalFullyInitializedCallable.execute(100);
}
}
// Using Graal RC-9
// WARNING: An illegal reflective access operation has occurred
// WARNING: Illegal reflective access by org.openjdk.jmh.util.Utils (file:/Users/srnivc/.m2/repository/org/openjdk/jmh/jmh-core/1.21/jmh-core-1.21.jar) to field java.io.PrintStream.charOut
// WARNING: Please consider reporting this to the maintainers of org.openjdk.jmh.util.Utils
// WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
// WARNING: All illegal access operations will be denied in a future release
// # JMH version: 1.21
// # VM version: JDK 11, OpenJDK 64-Bit Server VM, 11+28
// # VM invoker: /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home/bin/java
// # VM options: -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+BootstrapJVMCI -Dpolyglot.engine.PreinitializeContexts=js -Dfile.encoding=UTF-8
// # Warmup: 1 iterations, 10 s each
// # Measurement: 5 iterations, 10 s each
// # Timeout: 10 min per iteration
// # Threads: 1 thread, will synchronize iterations
// # Benchmark mode: Average time, time/op
// # Benchmark: com.benchmark.FunctionBenchmark.evaluateGraalFullyInitialized
// # Run progress: 0.00% complete, ETA 00:04:00
// # Fork: 1 of 2
// Bootstrapping JVMCI.......................... in 14692 ms (compiled 2613 methods)
// # Warmup Iteration 1: Warning: Nashorn engine is planned to be removed from a future JDK release
// 0.005 ms/op
// Iteration 1: 0.009 ms/op
// Iteration 2: 0.007 ms/op
// Iteration 3: 0.006 ms/op
// Iteration 4: 0.005 ms/op
// Iteration 5: 0.005 ms/op
// # Run progress: 25.00% complete, ETA 00:03:50
// # Fork: 2 of 2
// Bootstrapping JVMCI........................ in 14539 ms (compiled 2415 methods)
// # Warmup Iteration 1: Warning: Nashorn engine is planned to be removed from a future JDK release
// 0.005 ms/op
// Iteration 1: 0.005 ms/op
// Iteration 2: 0.005 ms/op
// Iteration 3: 0.005 ms/op
// Iteration 4: 0.005 ms/op
// Iteration 5: 0.005 ms/op
// Result "com.benchmark.FunctionBenchmark.evaluateGraalFullyInitialized":
// 0.006 ±(99.9%) 0.002 ms/op [Average]
// (min, avg, max) = (0.005, 0.006, 0.009), stdev = 0.001
// CI (99.9%): [0.004, 0.007] (assumes normal distribution)
// # JMH version: 1.21
// # VM version: JDK 11, OpenJDK 64-Bit Server VM, 11+28
// # VM invoker: /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home/bin/java
// # VM options: -Dfile.encoding=UTF-8
// # Warmup: 1 iterations, 10 s each
// # Measurement: 5 iterations, 10 s each
// # Timeout: 10 min per iteration
// # Threads: 1 thread, will synchronize iterations
// # Benchmark mode: Average time, time/op
// # Benchmark: com.benchmark.FunctionBenchmark.evaluateNashorn
// # Run progress: 50.00% complete, ETA 00:02:33
// # Fork: 1 of 2
// # Warmup Iteration 1: Warning: Nashorn engine is planned to be removed from a future JDK release
// ≈ 10⁻⁴ ms/op
// Iteration 1: ≈ 10⁻⁴ ms/op
// Iteration 2: ≈ 10⁻⁴ ms/op
// Iteration 3: ≈ 10⁻⁴ ms/op
// Iteration 4: ≈ 10⁻⁴ ms/op
// Iteration 5: ≈ 10⁻⁴ ms/op
// # Run progress: 75.00% complete, ETA 00:01:12
// # Fork: 2 of 2
// # Warmup Iteration 1: Warning: Nashorn engine is planned to be removed from a future JDK release
// ≈ 10⁻⁴ ms/op
// Iteration 1: ≈ 10⁻⁴ ms/op
// Iteration 2: ≈ 10⁻⁴ ms/op
// Iteration 3: ≈ 10⁻⁴ ms/op
// Iteration 4: ≈ 10⁻⁴ ms/op
// Iteration 5: ≈ 10⁻⁴ ms/op
// Result "com.benchmark.FunctionBenchmark.evaluateNashorn":
// ≈ 10⁻⁴ ms/op
// # Run complete. Total time: 00:04:38
// REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
// why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
// experiments, perform baseline and negative tests that provide experimental control, make sure
// the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
// Do not assume the numbers tell you what you want them to tell.
// Benchmark Mode Cnt Score Error Units
// FunctionBenchmark.evaluateGraalFullyInitialized avgt 10 0.006 ± 0.002 ms/op
// FunctionBenchmark.evaluateNashorn avgt 10 ≈ 10⁻⁴ ms/op
// Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment