Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Concurrent threads increment

  • Loading branch information...
commit a1cfbcbb43995333b8e775ebe476fc0dfb16e5e5 1 parent 8db0b13
oleksiyp authored
View
5 Crawler.groovy
@@ -1,10 +1,13 @@
// example of simple crawler
-// run: java -jar loadtest.jar -n 3 -d 1000 -e 'GLOBALS.list("urls","http://google.com")' Crawler.groovy
+// run: java -jar loadtest.jar -n 3 -d 1000 -ie 'GLOBALS.list("urls","http://google.com")' Crawler.groovy
import java.util.regex.Pattern;
try {
List urls = GLOBALS.list("urls");
String url = Variations.any(urls);
+ if (url == null) {
+ return;
+ }
HTTP.get({ line ->
for (String href : HTTP.parse(Pattern.compile("href=[\"\']([^\"\']+)[\"\']"), line)) {
urls.add(HTTP.resolve(url, href));
View
BIN  loadtest-1.0.jar
Binary file not shown
View
2  pom.xml
@@ -5,7 +5,7 @@
<artifactId>loadtest</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
- <name>Web loadTool test</name>
+ <name>Web load test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
View
41 src/main/java/org/loadtest/LoadTestTool.java
@@ -28,14 +28,15 @@ public LoadTestTool(Options options) {
this.options = options;
}
- public synchronized void start() {
+ public synchronized void start(int nThreads) {
if (executor != null) {
return;
}
scriptRunner = new ScriptRunner(this);
- executor = Executors.newScheduledThreadPool(options.concurrentThreads);
- for (int i = 0; i < options.concurrentThreads; i++) {
- long initDelay = (long)(1000L * options.delay * i / options.concurrentThreads);
+ scriptRunner.init();
+ executor = Executors.newScheduledThreadPool(nThreads);
+ for (int i = 0; i < nThreads; i++) {
+ long initDelay = (long)(1000L * options.delay * i / nThreads);
long delay = (long)(1000L * options.delay);
if (initDelay <= 0) initDelay = 0;
if (delay <= 0) delay = 1;
@@ -70,16 +71,22 @@ public boolean isStopped() {
public static void main(String[] args) throws IOException {
LoadTestTool testload = new LoadTestTool(new Options().parse(args));
Scanner scanner = new Scanner(System.in);
+ int nThreads = testload.getOptions().getConcurrentThreads();
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
do {
- System.out.println("Starting(press q[ENTER] - to quit, [ENTER] - to stop)");
- testload.start();
+ System.out.println("Starting with " + nThreads + " threads (press q[ENTER] - to quit, [ENTER] - to stop)");
+ testload.start(nThreads);
if (scanner.nextLine().equals("q")) {
break;
}
- System.out.println("Stopping(press q[ENTER] - to quit, [ENTER] - to start)");
+ System.out.println("Stopping (press q[ENTER] - to quit, [ENTER] - to start)");
testload.stop();
+
+ int increment = testload.getOptions().getConcurrentThreadsIncrement();
+ if (increment > 0) {
+ nThreads += increment;
+ }
} while (!scanner.nextLine().equals("q"));
} catch (Throwable thr) {
System.out.println("Breaked");
@@ -98,8 +105,13 @@ public Options getOptions() {
@Option(name="-n", usage="Number of runs to perform", aliases = "--runs-number")
private int requests = -1;
+
@Option(name="-c", usage="Number of concurrent threads", aliases = "--threads")
private int concurrentThreads = 1;
+
+ @Option(name="-ci", usage="Number of concurrent threads to increment after stop/start (use [ENTER])", aliases = "--threads-increment")
+ private int concurrentThreadsIncrement = 0;
+
/*
@Option(name="-t", usage="Seconds to max. wait for responses", aliases = "--time-limit")
private double timelimit = 0;
@@ -118,10 +130,10 @@ public Options getOptions() {
private List<String> scriptTexts = new ArrayList<String>();
@Option(name="-ie", usage="Evaluate init script to initialize globals", aliases="--init-eval")
- private List<String> initScriptTexts;
+ private List<String> initScriptTexts = new ArrayList<String>();
@Option(name="-if", usage="Runs init script to initialize globals", aliases="--init-file")
- private List<File> initScripts;
+ private List<File> initScripts = new ArrayList<File>();
@Argument
private List<File> scripts = new ArrayList();
@@ -136,6 +148,9 @@ public Options parse(String[] args) {
if ((scripts.isEmpty() && scriptTexts.isEmpty()) || displayUsage) {
usage(parser, new CmdLineException(parser, "specify one or more scripts"));
}
+ if (concurrentThreads <= 1) {
+ concurrentThreads = 1;
+ }
} catch (CmdLineException e) {
usage(parser, e);
}
@@ -165,6 +180,14 @@ public void setConcurrentThreads(int concurrentThreads) {
this.concurrentThreads = concurrentThreads;
}
+ public int getConcurrentThreadsIncrement() {
+ return concurrentThreadsIncrement;
+ }
+
+ public void setConcurrentThreadsIncrement(int concurrentThreadsIncrement) {
+ this.concurrentThreadsIncrement = concurrentThreadsIncrement;
+ }
+
public boolean isDisplayUsage() {
return displayUsage;
}
View
1  src/main/java/org/loadtest/ScriptRunner.java
@@ -90,6 +90,7 @@ public void run(GroovyShellRunner runner) {
stats.addError();
errorCase(e);
} catch (Throwable e) {
+ e.printStackTrace();
stats.addError();
} finally {
local.remove();
View
8 src/main/resources/org/loadtest/Classes.groovy
@@ -27,11 +27,14 @@ class Variations{
}
public static def any(Object ...args) {
- return args[getBindings().RANDOM.nextInt(args.length)];
+ return any(Arrays.asList(args));
}
public static def any(List args) {
synchronized(args) {
+ if (args.size() == 0) {
+ return null
+ };
return args.get(getBindings().RANDOM.nextInt(args.size()));
}
}
@@ -47,6 +50,9 @@ class HTTP {
public static def get(Closure reporter, String ...urls) {
long start = System.currentTimeMillis();
String urlString = Variations.any(urls);
+ if (urlString == null) {
+ throw new IllegalArgumentException("url should not be null");
+ }
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
String contentType = conn.getContentType();
View
6 src/main/resources/org/loadtest/Vars.groovy
@@ -1,6 +0,0 @@
-import org.loadtest.Globals
-import org.loadtest.Stats
-
-Globals GLOBALS;
-Stats STATS;
-Random RANDOM;
Please sign in to comment.
Something went wrong with that request. Please try again.