Skip to content

Instantly share code, notes, and snippets.

@axt
Created Sep 25, 2011
Embed
What would you like to do?
Runninc sunspider benchmarks to compare Rhino and V8 performance
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import lu.flier.script.V8Array;
import org.apache.commons.io.FileUtils;
public class V8Test {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine v8xx = factory.getEngineByName("jav8");
ScriptEngine v8engine = factory.getEngineByName("jav8");
ScriptEngine rhinoengine = factory.getEngineByName("rhino");
System.err.println("V8 :" + v8engine);
System.err.println("Rhino:" + rhinoengine);
String input1 = FileUtils.readFileToString(new File("sunspider-test-prefix.js"));
String input2 = FileUtils.readFileToString(new File("sunspider-test-contents.js"));
v8xx.eval(input1);
v8xx.eval(input2);
v8xx.eval("testContents").getClass();
V8Array tests = (V8Array)v8xx.eval("tests");
V8Array testContents = (V8Array)v8xx.eval("testContents");
List<String> scriptSources = new ArrayList<String>();
for(int i=0; i<tests.size();i++) {
String testName = (String)tests.get(i);
String testScript = (String)testContents.get(i);
Pattern p = Pattern.compile("var _sunSpiderStartDate = new Date\\(\\);(.*?)var _sunSpiderInterval = new Date\\(\\) - _sunSpiderStartDate;",Pattern.DOTALL | Pattern.MULTILINE);
Matcher m = p.matcher(testScript);
m.find();
testScript = m.group(1);
scriptSources.add(testScript);
}
System.err.println("");
int c = 0;
for(String scriptSource : scriptSources) {
Compilable compilable = (Compilable) rhinoengine;
CompiledScript script = compilable.compile(scriptSource);
System.err.println("Running script nr " + tests.get(c++));
long before, after;
Object res;
before = System.nanoTime();
res = v8engine.eval(scriptSource);
after = System.nanoTime();
System.err.println("V8 : " +((after-before)/1000/1000)+" ms");
before = System.nanoTime();
res = script.eval();
after = System.nanoTime();
System.err.println("Rhino: " +((after-before)/1000/1000)+" ms");
System.err.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment