Skip to content

Commit

Permalink
Rename. Init scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiyp authored and oleksiyp committed Nov 5, 2011
1 parent 0feede9 commit 8db0b13
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -51,7 +51,7 @@
<archive>
<index>true</index>
<manifest>
<mainClass>org.loadtest.LoadTest</mainClass>
<mainClass>org.loadtest.LoadTestTool</mainClass>
</manifest>
</archive>
</configuration>
Expand Down
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -115,6 +117,12 @@ public static class Options {
@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();

Expand Down Expand Up @@ -188,6 +196,14 @@ public List<String> getScriptTexts() {
public List<File> getScripts() {
return scripts;
}

public List<String> getInitScriptTexts() {
return initScriptTexts;
}

public List<File> getInitScripts() {
return initScripts;
}
}

private class StatsPrinter implements Runnable {
Expand Down
56 changes: 40 additions & 16 deletions src/main/java/org/loadtest/ScriptRunner.java
Expand Up @@ -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;

/**
Expand All @@ -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() {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 8db0b13

Please sign in to comment.