Skip to content

Instantly share code, notes, and snippets.

@chriswhocodes
Created May 8, 2015 17:00
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 chriswhocodes/c5bf3632fb7272e994e9 to your computer and use it in GitHub Desktop.
Save chriswhocodes/c5bf3632fb7272e994e9 to your computer and use it in GitHub Desktop.
package org.sample;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import javax.script.Invocable;
import javax.script.ScriptEngineManager;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import jdk.nashorn.api.scripting.NashornScriptEngine;
// svn checkout http://octane-benchmark.googlecode.com/svn/trunk/ octane-benchmark
@State(Scope.Thread)
public class NashornTest
{
private static final String OCTANE_DIR = "octane-benchmark";
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testRichards() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "richards.js"))));
nashorn.eval("runRichards();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testDeltablue() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "deltablue.js"))));
nashorn.eval("deltaBlue();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testCrypto() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "crypto.js"))));
nashorn.eval("encrypt();");
nashorn.eval("decrypt();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testRaytrace() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "raytrace.js"))));
nashorn.eval("renderScene();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testEarleyBoyer() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "earley-boyer.js"))));
nashorn.eval("BgL_earleyzd2benchmarkzd2();");
nashorn.eval("BgL_nboyerzd2benchmarkzd2();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testRegExp() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "regexp.js"))));
nashorn.eval("BenchmarkSuite.ResetRNG();");
nashorn.eval("RegExpSetup();");
nashorn.eval("RegExpRun();");
nashorn.eval("RegExpTearDown();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testSplay() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "splay.js"))));
nashorn.eval("SplaySetup();");
nashorn.eval("SplayRun();");
nashorn.eval("SplayTearDown();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testNavierStokes() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "navier-stokes.js"))));
nashorn.eval("setupNavierStokes();");
nashorn.eval("runNavierStokes();");
nashorn.eval("tearDownNavierStokes();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testPdfJS() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "pdfjs.js"))));
nashorn.eval("setupPdfJS();");
nashorn.eval("runPdfJS();");
nashorn.eval("tearDownPdfJS();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testMandreel() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "mandreel.js"))));
nashorn.eval("setupMandreel();");
nashorn.eval("runMandreel();");
nashorn.eval("tearDownMandreel();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testGBEmuPart1() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "gbemu-part1.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "gbemu-part2.js"))));
nashorn.eval("setupGameboy();");
nashorn.eval("runGameboy();");
nashorn.eval("tearDownGameboy();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testCodeLoad() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "code-load.js"))));
nashorn.eval("setupCodeLoad();");
nashorn.eval("runCodeLoadClosure();");
nashorn.eval("tearDownCodeLoad();");
nashorn.eval("setupCodeLoad();");
nashorn.eval("runCodeLoadJQuery();");
nashorn.eval("tearDownCodeLoad();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testBox2D() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "box2d.js"))));
nashorn.eval("setupBox2D();");
nashorn.eval("runBox2D();");
nashorn.eval("tearDownBox2D();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testZLib() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "zlib.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "zlib-data.js"))));
nashorn.eval("runZlib();");
nashorn.eval("tearDownZlib();");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testTypescript() throws Exception
{
NashornScriptEngine nashorn = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "base.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "typescript.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "typescript-input.js"))));
nashorn.eval(new String(Files.readAllBytes(Paths.get(OCTANE_DIR, "typescript-compiler.js"))));
nashorn.eval("setupTypescript();");
nashorn.eval("runTypescript();");
nashorn.eval("tearDownTypescript();");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment