1 6 package org.logicalcobwebs.dbscript; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.ProxoolException; 11 12 import java.sql.SQLException ; 13 14 22 public class ScriptRunner { 23 24 private static final Log LOG = LogFactory.getLog(ScriptRunner.class); 25 26 33 protected static final void runScript(Script script, ConnectionAdapterIF adapter, CommandFilterIF commandFilter) throws SQLException , ProxoolException { 34 adapter.setup(script.getDriver(), script.getUrl(), script.getInfo()); 35 36 try { 37 Thread.sleep(5000); 38 } catch (InterruptedException e) { 39 e.printStackTrace(); } 41 42 Command[] commands = script.getCommands(); 43 for (int i = 0; i < commands.length; i++) { 44 Command command = commands[i]; 45 long start = System.currentTimeMillis(); 46 47 Commander[] commanders = new Commander[command.getLoad()]; 49 for (int load = 0; load < command.getLoad(); load++) { 50 commanders[load] = new Commander(adapter, command, commandFilter); 51 Thread t = new Thread (commanders[load]); 52 t.setName(script.getName() + "." + command.getName() + "." + load); 53 t.start(); 54 } 55 56 while (true) { 57 try { 58 Thread.sleep(1000); 59 } catch (InterruptedException e) { 60 LOG.error("Awoken from sleep", e); 61 } 62 63 int remaining = command.getLoad(); 64 for (int load = 0; load < command.getLoad(); load++) { 65 if (commanders[load].isFinished()) { 66 remaining--; 67 } 68 } 69 70 if (remaining > 0) { 71 } else { 73 break; 74 } 75 } 76 77 long elapsed = System.currentTimeMillis() - start; 78 int count = command.getLoad() * command.getLoops(); 79 double lap = (double) elapsed / (double) count; 80 if (count > 1) { 81 LOG.info(adapter.getName() + ":" + command.getName() + " ran " + count + " commands in " + elapsed + " milliseconds (avg." + lap + ")"); 82 } else { 83 LOG.debug(adapter.getName() + ":" + command.getName() + " ran in " + elapsed + " milliseconds"); 84 } 85 86 } 87 } 88 89 } 90 91 134 | Popular Tags |