| package org.eclipse.e4.javascript.js.test; |
| |
| import junit.framework.TestCase; |
| import org.eclipse.e4.javascript.JSBundle; |
| import org.eclipse.e4.javascript.test.Activator; |
| import org.mozilla.javascript.*; |
| import org.osgi.framework.Version; |
| |
| public class PerfVersionTest extends TestCase { |
| |
| public PerfVersionTest(String name) { |
| super(name); |
| } |
| |
| public void testVersion() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| String script = ""; |
| script += "assertTrue('versions not equal',new Version(0,0,0).equals(Version.EMPTY_VERSION));"; |
| // script += "assertEquals('versions not equal',Version.EMPTY_VERSION,new Version(0,0,0));"; |
| // script += "assertFalse('versions equal',new Version(1,0,0).equals(Version.EMPTY_VERSION));"; |
| // script += "assertNotEquals('versions equal',Version.EMPTY_VERSION,new Version(0,1,0));"; |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 1000; i++) { |
| context.evaluateString(scope, script, "script.js", 0, null); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testVersion2() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("Version", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertTrue(obj instanceof Callable); |
| Function version = (Function) obj; |
| Context.enter(); |
| try { |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 10000; i++) { |
| Scriptable version1 = (Scriptable) version.get("EMPTY_VERSION", scope); |
| Callable eq = (Callable) ScriptableObject.getProperty(version1, "equals"); |
| Integer ZERO = new Integer(0); |
| Scriptable version2 = version.construct(Context.getCurrentContext(), scope, new Object[] {ZERO, ZERO, ZERO, null}); |
| assertTrue(((Boolean) eq.call(Context.getCurrentContext(), scope, version1, new Object[] {version2})).booleanValue()); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testVersion3() throws Exception { |
| |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 100000; i++) { |
| Version version1 = Version.emptyVersion; |
| Version version2 = new Version(0, 0, i + 1); |
| assertFalse(version1.equals(version2)); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } |
| |
| public void testVersion4() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| String script = ""; |
| script += "function func(){assertTrue('versions not equal',new Version(0,0,0).equals(Version.EMPTY_VERSION));};"; |
| context.evaluateString(scope, script, "script.js", 0, null); |
| Callable func = (Callable) ScriptableObject.getProperty(scope, "func"); |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 10000; i++) { |
| func.call(Context.getCurrentContext(), scope, scope, null); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testVersion5() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| String script = ""; |
| script += "function func(){assertTrue('versions not equal',new Version(0,0,0).equals(Version.EMPTY_VERSION));};"; |
| Function func = context.compileFunction(scope, script, "script.js", 0, null); |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 10000; i++) { |
| func.call(Context.getCurrentContext(), scope, scope, null); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testVersion6() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| String script = ""; |
| script += "function func(){new Version(0,0,0).equals(Version.EMPTY_VERSION);};"; |
| Function func = context.compileFunction(scope, script, "script.js", 0, null); |
| long current = System.currentTimeMillis(); |
| for (int i = 0; i < 10000; i++) { |
| func.call(context, scope, scope, null); |
| } |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testVersion7() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| String script = ""; |
| script += "function func(){\n"; |
| script += " for(var i =0; i < 10000 ; i++){\n"; |
| script += " new Version(0,0,0).equals(Version.EMPTY_VERSION);\n"; |
| script += " }"; |
| script += "}"; |
| Function func = context.compileFunction(scope, script, "script.js", 0, null); |
| long current = System.currentTimeMillis(); |
| func.call(context, scope, scope, null); |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testFib() throws Exception { |
| JSBundle jsBundle = Activator.getJSBundle(); |
| Scriptable scope = jsBundle.getScope(); |
| Context.enter(); |
| try { |
| Context context = Context.getCurrentContext(); |
| context.setOptimizationLevel(9); |
| String script = ""; |
| script += "function func(){\n"; |
| script += " bench(50000);\n"; |
| script += "}"; |
| Function func = context.compileFunction(scope, script, "script.js", 0, null); |
| long current = System.currentTimeMillis(); |
| func.call(context, scope, scope, null); |
| System.out.println(System.currentTimeMillis() - current); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| } |