001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.dbscript;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.logicalcobwebs.proxool.ProxoolException;
011:
012: import java.sql.SQLException;
013:
014: /**
015: * Run a {@link Script script}.
016: *
017: * @version $Revision: 1.12 $, $Date: 2006/01/18 14:40:05 $
018: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
019: * @author $Author: billhorsman $ (current maintainer)
020: * @since Proxool 0.5
021: */
022: public class ScriptRunner {
023:
024: private static final Log LOG = LogFactory
025: .getLog(ScriptRunner.class);
026:
027: /**
028: * Run the script.
029: *
030: * @param script to run
031: * @param adapter so we know where to connections from
032: * @throws SQLException if anything goes wrong
033: */
034: protected static final void runScript(Script script,
035: ConnectionAdapterIF adapter, CommandFilterIF commandFilter)
036: throws SQLException, ProxoolException {
037: adapter.setup(script.getDriver(), script.getUrl(), script
038: .getInfo());
039:
040: try {
041: Thread.sleep(5000);
042: } catch (InterruptedException e) {
043: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
044: }
045:
046: Command[] commands = script.getCommands();
047: for (int i = 0; i < commands.length; i++) {
048: Command command = commands[i];
049: long start = System.currentTimeMillis();
050:
051: // Execute the SQL
052: Commander[] commanders = new Commander[command.getLoad()];
053: for (int load = 0; load < command.getLoad(); load++) {
054: commanders[load] = new Commander(adapter, command,
055: commandFilter);
056: Thread t = new Thread(commanders[load]);
057: t.setName(script.getName() + "." + command.getName()
058: + "." + load);
059: t.start();
060: }
061:
062: while (true) {
063: try {
064: Thread.sleep(1000);
065: } catch (InterruptedException e) {
066: LOG.error("Awoken from sleep", e);
067: }
068:
069: int remaining = command.getLoad();
070: for (int load = 0; load < command.getLoad(); load++) {
071: if (commanders[load].isFinished()) {
072: remaining--;
073: }
074: }
075:
076: if (remaining > 0) {
077: // LOG.debug("Waiting for " + remaining + " threads to complete.");
078: } else {
079: break;
080: }
081: }
082:
083: long elapsed = System.currentTimeMillis() - start;
084: int count = command.getLoad() * command.getLoops();
085: double lap = (double) elapsed / (double) count;
086: if (count > 1) {
087: LOG.info(adapter.getName() + ":" + command.getName()
088: + " ran " + count + " commands in " + elapsed
089: + " milliseconds (avg." + lap + ")");
090: } else {
091: LOG.debug(adapter.getName() + ":" + command.getName()
092: + " ran in " + elapsed + " milliseconds");
093: }
094:
095: }
096: }
097:
098: }
099:
100: /*
101: Revision history:
102: $Log: ScriptRunner.java,v $
103: Revision 1.12 2006/01/18 14:40:05 billhorsman
104: Unbundled Jakarta's Commons Logging.
105:
106: Revision 1.11 2003/03/03 11:12:03 billhorsman
107: fixed licence
108:
109: Revision 1.10 2003/02/19 15:14:21 billhorsman
110: fixed copyright (copy and paste error,
111: not copyright change)
112:
113: Revision 1.9 2003/02/06 17:41:02 billhorsman
114: now uses imported logging
115:
116: Revision 1.8 2003/01/17 00:38:12 billhorsman
117: wide ranging changes to clarify use of alias and url -
118: this has led to some signature changes (new exceptions
119: thrown) on the ProxoolFacade API.
120:
121: Revision 1.7 2002/11/09 16:00:34 billhorsman
122: fix doc
123:
124: Revision 1.6 2002/11/09 14:45:07 billhorsman
125: now threaded and better exception handling
126:
127: Revision 1.5 2002/11/06 21:07:14 billhorsman
128: Support for CommandFilterIF
129:
130: Revision 1.4 2002/11/02 14:22:16 billhorsman
131: Documentation
132:
133: Revision 1.3 2002/11/02 13:57:34 billhorsman
134: checkstyle
135:
136: Revision 1.2 2002/11/02 12:45:54 billhorsman
137: improved debug
138:
139: Revision 1.1 2002/11/02 11:29:53 billhorsman
140: new script runner for testing
141:
142: */
|