1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.performance; 27 28 import java.io.OutputStream; 29 import java.util.concurrent.Callable; 30 import java.util.concurrent.ExecutionException; 31 import java.util.concurrent.Future; 32 import java.util.concurrent.TimeUnit; 33 import java.util.concurrent.TimeoutException; 34 import jdk.nashorn.internal.objects.Global; 35 import jdk.nashorn.internal.runtime.Context; 36 import jdk.nashorn.internal.runtime.ScriptFunction; 37 import jdk.nashorn.internal.runtime.ScriptRuntime; 38 39 /** 40 * 41 * @author Pavel Stepanov 42 */ 43 public class PerformanceWrapper extends jdk.nashorn.tools.Shell { 44 45 int _numberOfIterations; 46 int _runsPerIteration; 47 48 protected void runCompileOnlyTest(final String name, final int numberOfIterations, final int runsPerIteration, final String testURL) throws Throwable { 49 final String[] args = { name, "--compile-only=true", "-dump-on-error", "--", testURL }; 50 51 final long[] times = new long[numberOfIterations + 1]; 52 times[0] = System.nanoTime(); // Calendar.getInstance().getTimeInMillis(); 53 54 for (int iteration = 1; iteration <= numberOfIterations; iteration++) { 55 for (int i = 0; i < runsPerIteration; i++) { 56 run(System.in, System.out, System.err, args); 57 } 58 times[iteration] = System.nanoTime(); 59 } 60 61 for (int i = 0; i < numberOfIterations; i++) { 62 System.out.println("Iteration " + (i + 1) + " average time: " + ((times[i + 1] - times[i]) / (float)runsPerIteration) / 1000000.0 + " ms."); 63 } 64 } 65 66 protected void runExecuteOnlyTest(final String name, final int numberOfIterations, final int runsPerIteration, final String testURL) throws Throwable { 67 runExecuteOnlyTest(name, numberOfIterations, runsPerIteration, testURL, System.out, System.err, new String[0]); 68 } 69 70 protected void runExecuteOnlyTest(final String name, final int numberOfIterations, final int runsPerIteration, final String testURL, final OutputStream out, final OutputStream err) throws Throwable { 71 runExecuteOnlyTest(name, numberOfIterations, runsPerIteration, testURL, out, err, new String[0]); 72 } 73 74 75 protected void runExecuteOnlyTest(final String name, final int numberOfIterations, final int runsPerIteration, final String testURL, final OutputStream out, final OutputStream err, String[] newargs) throws Throwable { 76 String[] args=new String[newargs.length+1]; 77 System.arraycopy(newargs, 0, args, 1, newargs.length); 78 args[0]=name; 79 80 // for (String s: args) 81 // System.out.println(s); 82 83 _numberOfIterations = numberOfIterations; 84 _runsPerIteration = runsPerIteration; 85 run(System.in, out, err, args); 86 // System.out.println("overridableRun finished"); 87 } 88 89 @Override 90 protected Object apply(final ScriptFunction target, final Object self) { 91 if (_runsPerIteration == 0 && _numberOfIterations == 0) { 92 final Global global = jdk.nashorn.internal.runtime.Context.getGlobal(); 93 final ScriptFunction _target = target; 94 final Object _self = self; 95 96 class MyThread implements Callable<Object> { 97 @Override 98 public Object call() { 99 Context.setGlobal(global); 100 //just execute and return script is sufficient 101 final Object scriptRuntimeApplyResult = ScriptRuntime.apply(_target, _self); 102 return scriptRuntimeApplyResult; 103 } 104 } 105 106 final java.util.concurrent.ThreadPoolExecutor executor = new java.util.concurrent.ThreadPoolExecutor(1, 1, 1, TimeUnit.MINUTES, new java.util.concurrent.ArrayBlockingQueue<Runnable>(10)); 107 final MyThread myThread = new MyThread(); 108 // executor.execute(myThread); 109 Object result; 110 Future<?> futureResult = null; 111 112 try { 113 futureResult = executor.submit(myThread); 114 final String timeout = System.getProperty("timeout.value"); 115 int tmout = 0; 116 if (timeout != null) { 117 try { 118 tmout = Integer.parseInt(timeout); 119 } catch (final Exception e) { 120 e.printStackTrace(); 121 } 122 } 123 if (tmout != 0) { 124 result = futureResult.get(10, TimeUnit.MINUTES); 125 } else { 126 result = futureResult.get(); 127 } 128 } catch (final InterruptedException | ExecutionException e) { 129 e.printStackTrace(); 130 return null; 131 } catch (final TimeoutException e) { 132 System.out.println("timeout while script execution"); 133 futureResult.cancel(true); 134 return null; 135 } 136 137 return result; 138 } 139 140 final long[] times = new long[_numberOfIterations + 1]; 141 times[0] = System.nanoTime(); 142 for (int iteration = 1; iteration <= _numberOfIterations; iteration++) { 143 for (int i = 0; i < _runsPerIteration; i++) { 144 // empty 145 } 146 times[iteration] = System.nanoTime(); 147 } 148 149 for (int i = 0; i < _numberOfIterations; i++) { 150 System.out.println("Iteration " + (i + 1) + " average time: " + ((times[i + 1] - times[i]) / (float)_runsPerIteration) / 1000000.0 + " ms."); 151 } 152 153 return ScriptRuntime.apply(target, self); 154 } 155 }