Permalink
Browse files

Rename. Init scripts.

  • Loading branch information...
1 parent 0feede9 commit 8db0b1363f9dddd26bdff2e3765b653f323e2dc5 oleksiyp committed Nov 5, 2011
View
@@ -5,7 +5,7 @@
<artifactId>loadtest</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
- <name>Web load test</name>
+ <name>Web loadTool test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -51,7 +51,7 @@
<archive>
<index>true</index>
<manifest>
- <mainClass>org.loadtest.LoadTest</mainClass>
+ <mainClass>org.loadtest.LoadTestTool</mainClass>
</manifest>
</archive>
</configuration>
@@ -18,13 +18,13 @@
* Tool to run test load specified by script using several threads.
* Main entry point.
*/
-public class LoadTest {
+public class LoadTestTool {
private final Options options;
private ScheduledExecutorService executor;
private ScheduledExecutorService printExecutor;
private ScriptRunner scriptRunner;
- public LoadTest(Options options) {
+ public LoadTestTool(Options options) {
this.options = options;
}
@@ -53,7 +53,9 @@ public synchronized void start() {
}
public synchronized void stop() {
- if (executor == null) { return; }
+ if (executor == null) {
+ return;
+ }
executor.shutdownNow();
executor = null;
printExecutor.shutdownNow();
@@ -66,7 +68,7 @@ public boolean isStopped() {
}
public static void main(String[] args) throws IOException {
- LoadTest testload = new LoadTest(new Options().parse(args));
+ LoadTestTool testload = new LoadTestTool(new Options().parse(args));
Scanner scanner = new Scanner(System.in);
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
@@ -115,6 +117,12 @@ public Options getOptions() {
@Option(name="-e", usage="Evaluate groovy script", aliases="--eval")
private List<String> scriptTexts = new ArrayList<String>();
+ @Option(name="-ie", usage="Evaluate init script to initialize globals", aliases="--init-eval")
+ private List<String> initScriptTexts;
+
+ @Option(name="-if", usage="Runs init script to initialize globals", aliases="--init-file")
+ private List<File> initScripts;
+
@Argument
private List<File> scripts = new ArrayList();
@@ -188,6 +196,14 @@ public void setSlowQueriesToShow(int slowQueriesToShow) {
public List<File> getScripts() {
return scripts;
}
+
+ public List<String> getInitScriptTexts() {
+ return initScriptTexts;
+ }
+
+ public List<File> getInitScripts() {
+ return initScripts;
+ }
}
private class StatsPrinter implements Runnable {
@@ -6,7 +6,7 @@
import org.codehaus.groovy.control.CompilationFailedException;
import java.io.File;
-import java.util.List;
+import java.io.IOException;
import java.util.Random;
/**
@@ -16,14 +16,14 @@
* RANDOM - random object
*/
public class ScriptRunner implements Runnable {
- private LoadTest load;
+ private LoadTestTool loadTool;
private Stats stats = new Stats();
private Object globals;
private Random random = new Random();
private static ThreadLocal local = new ThreadLocal();
- public ScriptRunner(LoadTest load) {
- this.load = load;
+ public ScriptRunner(LoadTestTool loadTool) {
+ this.loadTool = loadTool;
}
protected Binding createNewBinding() {
@@ -33,13 +33,39 @@ protected Binding createNewBinding() {
return binding;
}
+ public void init() {
+ run(new GroovyShellRunner() {
+ public void run(GroovyShell shell) throws IOException {
+ for (String script : loadTool.getOptions().getInitScriptTexts()) {
+ shell.evaluate(script);
+ }
+
+ for (File script : loadTool.getOptions().getInitScripts()) {
+ shell.evaluate(script);
+ }
+ }});
+ }
+
public void run() {
+ run(new GroovyShellRunner() {
+ public void run(GroovyShell shell) throws IOException {
+ for (String script : loadTool.getOptions().getScriptTexts()) {
+ shell.evaluate(script);
+ }
+
+ for (File script : loadTool.getOptions().getScripts()) {
+ shell.evaluate(script);
+ }
+ }});
+ }
+
+ public void run(GroovyShellRunner runner) {
synchronized (stats) {
- if (load.getOptions().getRequests() != -1
- && stats.getRuns() >= load.getOptions().getRequests()) {
+ if (loadTool.getOptions().getRequests() != -1
+ && stats.getRuns() >= loadTool.getOptions().getRequests()) {
stats.report(Integer.MAX_VALUE);
System.out.println("Finnished(press q[ENTER] - to quit, [ENTER][ENTER] - to restart)");
- load.stop();
+ loadTool.stop();
return;
}
stats.addRun();
@@ -58,13 +84,7 @@ public void run() {
}
binding.setVariable("GLOBALS", globals);
- for (String script : load.getOptions().getScriptTexts()) {
- shell.evaluate(script);
- }
-
- for (File script : load.getOptions().getScripts()) {
- shell.evaluate(script);
- }
+ runner.run(shell);
} catch (CompilationFailedException e) {
stats.addError();
@@ -76,10 +96,14 @@ public void run() {
}
}
+ private interface GroovyShellRunner {
+ void run(GroovyShell shell) throws IOException;
+ }
+
private void errorCase(Exception e) {
- if (!load.isStopped()) {
+ if (!loadTool.isStopped()) {
e.printStackTrace();
- load.stop();
+ loadTool.stop();
}
}

0 comments on commit 8db0b13

Please sign in to comment.