Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Inspection fixes

  • Loading branch information...
commit ebb9a8fc4920e64f9df849c1c27797c22de9a07a 1 parent a1cfbcb
oleksiyp authored
View
2  Crawler.groovy
@@ -4,7 +4,7 @@ import java.util.regex.Pattern;
try {
List urls = GLOBALS.list("urls");
- String url = Variations.any(urls);
+ String url = Variations.takeAny(urls);
if (url == null) {
return;
}
View
2  Example2.groovy
@@ -4,7 +4,7 @@ import java.util.regex.Pattern;
try {
List urls = GLOBALS.list("urls", "http://google.com");
- String url = Variations.any(urls);
+ String url = Variations.takeAny(urls);
HTTP.get({ line ->
for (String href : HTTP.parse(Pattern.compile("href=[\"\']([^\"\']+)[\"\']"), line)) {
urls.add(HTTP.resolve(url, href));
View
12 README.textile
@@ -10,7 +10,7 @@ java -jar loadtest-1.0.jar Example1.groovy
h2. Environment
-For groovy scripts following classes avialable: HTTP and Variations. HTTP class could be used to create load. Variation helps to create non-deterministic behavior. Look groovy documentation and src/main/resources/org/loadtest/Classes.groovy for details.
+For groovy scripts following classes available: HTTP and Variations. HTTP class could be used to create load. Variation helps to create non-deterministic behavior. Look groovy documentation and src/main/resources/org/loadtest/Classes.groovy for details.
h3. Variations
@@ -18,10 +18,10 @@ h3. Variations
h4. select(prob1, closure1, prob2, closure2, prob3, closure3, ...)
-Select one of the closures to execute depending on probability. Probabilites could be not normalized: i.e. sum(prob~i~) != 1.0
+Select one of the closures to execute depending on probability. Probabilities could be not normalized: i.e. sum(prob~i~) != 1.0
-h4. any(arguments...);
+h4. takeAny(arguments...);
Select one of arguments with equal probability.
@@ -39,13 +39,13 @@ Gets value of one(!randomly selected) of urls and reads line by line calling rep
h4. parse(Pattern regex, String ...html)
-Process text throught regular expression and selects regular expression groups. Returns 2-d array if several groups used and 1-d if one.
+Process text thought regular expression and selects regular expression groups. Returns 2-d array if several groups used and 1-d if one.
-h4. resolve(String url, String path)
+h4. resolve(String name, String path)
-Resolves path relative to url.
+Resolves path relative to name.
h2. License
View
9 pom.xml
@@ -6,7 +6,6 @@
<packaging>jar</packaging>
<version>1.0</version>
<name>Web load test</name>
- <url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@@ -56,14 +55,6 @@
</archive>
</configuration>
</plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>versions-maven-plugin</artifactId>
- <version>1.2</version>
- <configuration>
- <newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</newVersion>
- </configuration>
- </plugin>
</plugins>
</build>
</project>
View
32 src/main/java/org/loadtest/LoadTestTool.java
@@ -18,6 +18,7 @@
* Tool to run test load specified by script using several threads.
* Main entry point.
*/
+@SuppressWarnings({"WeakerAccess", "WeakerAccess"})
public class LoadTestTool {
private final Options options;
private ScheduledExecutorService executor;
@@ -64,33 +65,34 @@ public synchronized void stop() {
scriptRunner = null;
}
- public boolean isStopped() {
- return executor == null;
+ public boolean isStarted() {
+ return executor != null;
}
public static void main(String[] args) throws IOException {
- LoadTestTool testload = new LoadTestTool(new Options().parse(args));
+ LoadTestTool loadtest = new LoadTestTool(new Options().parse(args));
Scanner scanner = new Scanner(System.in);
- int nThreads = testload.getOptions().getConcurrentThreads();
+ int nThreads = loadtest.getOptions().getConcurrentThreads();
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
do {
System.out.println("Starting with " + nThreads + " threads (press q[ENTER] - to quit, [ENTER] - to stop)");
- testload.start(nThreads);
+ loadtest.start(nThreads);
if (scanner.nextLine().equals("q")) {
break;
}
System.out.println("Stopping (press q[ENTER] - to quit, [ENTER] - to start)");
- testload.stop();
+ loadtest.stop();
- int increment = testload.getOptions().getConcurrentThreadsIncrement();
+ int increment = loadtest.getOptions().getConcurrentThreadsIncrement();
if (increment > 0) {
nThreads += increment;
}
} while (!scanner.nextLine().equals("q"));
} catch (Throwable thr) {
- System.out.println("Breaked");
+ System.out.println("Error happened: " + thr);
}
+ System.exit(0);
}
public synchronized ScriptRunner getScriptRunner() {
@@ -101,6 +103,7 @@ public Options getOptions() {
return options;
}
+ @SuppressWarnings({"UnusedDeclaration"})
public static class Options {
@Option(name="-n", usage="Number of runs to perform", aliases = "--runs-number")
private int requests = -1;
@@ -112,11 +115,6 @@ public Options getOptions() {
@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;
- */
-
@Option(name="-h", usage="Display usage information", aliases="--help")
private boolean displayUsage = false;
@@ -127,16 +125,16 @@ public Options getOptions() {
private int slowQueriesToShow = 24;
@Option(name="-e", usage="Evaluate groovy script", aliases="--eval")
- private List<String> scriptTexts = new ArrayList<String>();
+ private final List<String> scriptTexts = new ArrayList<String>();
@Option(name="-ie", usage="Evaluate init script to initialize globals", aliases="--init-eval")
- private List<String> initScriptTexts = new ArrayList<String>();
+ private final List<String> initScriptTexts = new ArrayList<String>();
@Option(name="-if", usage="Runs init script to initialize globals", aliases="--init-file")
- private List<File> initScripts = new ArrayList<File>();
+ private final List<File> initScripts = new ArrayList<File>();
@Argument
- private List<File> scripts = new ArrayList();
+ private final List<File> scripts = new ArrayList<File>();
public Options() {
}
View
27 src/main/java/org/loadtest/ScriptRunner.java
@@ -11,16 +11,17 @@
/**
* Runs scripts in environment produced by "Classes.groovy" and three bindings:
- * GLOBAL - global variables interchanger,
+ * GLOBAL - global variables interchanged
* STATS - statistics object,
* RANDOM - random object
*/
+@SuppressWarnings({"WeakerAccess"})
public class ScriptRunner implements Runnable {
- private LoadTestTool loadTool;
- private Stats stats = new Stats();
+ private final LoadTestTool loadTool;
+ private final Stats stats = new Stats();
private Object globals;
- private Random random = new Random();
- private static ThreadLocal local = new ThreadLocal();
+ private final Random random = new Random();
+ private final static ThreadLocal<GroovyShell> LOCAL_SHELL = new ThreadLocal<GroovyShell>();
public ScriptRunner(LoadTestTool loadTool) {
this.loadTool = loadTool;
@@ -64,7 +65,7 @@ public void run(GroovyShellRunner runner) {
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)");
+ System.out.println("Finished (press q[ENTER] - to quit, [ENTER][ENTER] - to restart)");
loadTool.stop();
return;
}
@@ -73,13 +74,13 @@ public void run(GroovyShellRunner runner) {
Binding binding = createNewBinding();
GroovyShell shell = new GroovyShell(binding);
try {
- local.set(shell);
+ LOCAL_SHELL.set(shell);
shell.evaluate(new GroovyCodeSource(ScriptRunner.class.getResource("Classes.groovy")));
synchronized (this) {
if (globals == null) {
- globals = shell.evaluate("new Globals()");
+ globals = shell.evaluate("return new Globals()");
}
}
binding.setVariable("GLOBALS", globals);
@@ -87,13 +88,13 @@ public void run(GroovyShellRunner runner) {
runner.run(shell);
} catch (CompilationFailedException e) {
- stats.addError();
+ stats.addError(e);
errorCase(e);
} catch (Throwable e) {
e.printStackTrace();
- stats.addError();
+ stats.addError(e);
} finally {
- local.remove();
+ LOCAL_SHELL.remove();
}
}
@@ -102,7 +103,7 @@ public void run(GroovyShellRunner runner) {
}
private void errorCase(Exception e) {
- if (!loadTool.isStopped()) {
+ if (loadTool.isStarted()) {
e.printStackTrace();
loadTool.stop();
}
@@ -113,6 +114,6 @@ public Stats getStats() {
}
public static GroovyShell getGroovyShell() {
- return (GroovyShell) local.get();
+ return LOCAL_SHELL.get();
}
}
View
60 src/main/java/org/loadtest/Stats.java
@@ -5,6 +5,7 @@
/**
* Class accumulates statistics: number of runs, errors, queries and http fetch execution time.
*/
+@SuppressWarnings({"WeakerAccess"})
public class Stats {
public Stats() {
reset();
@@ -14,27 +15,27 @@ public synchronized void reset() {
runs = 0;
samples = 0;
errors = 0;
- urlStats = new TreeMap();
+ sampleStats = new TreeMap<String, SampleStat>();
}
private int runs;
private int samples;
private int errors;
- private Map urlStats;
+ private Map<String, SampleStat> sampleStats;
public int getRuns() {
return runs;
}
- private static class UrlStat {
+ private static class SampleStat {
private int count;
private long time;
private long minimum;
private long maximum;
- private final String url;
+ private final String name;
- private UrlStat(String url) {
- this.url = url;
+ private SampleStat(String name) {
+ this.name = name;
}
@@ -49,7 +50,7 @@ public void add(long time) {
this.count++;
}
- public double getAvarage() {
+ public double getAverage() {
if (count == 0) {
return 0;
} else {
@@ -65,45 +66,54 @@ public long getMaximum() {
return maximum;
}
- public String getUrl() {
- return url;
+ public String getName() {
+ return name;
}
}
public synchronized void addRun() { runs++; }
public synchronized void addSample(String url, long time) {
samples++;
- UrlStat stat = (UrlStat) urlStats.get(url);
+ SampleStat stat = sampleStats.get(url);
if (stat == null) {
- stat = new UrlStat(url);
- urlStats.put(url, stat);
+ stat = new SampleStat(url);
+ sampleStats.put(url, stat);
}
stat.add(time);
}
- public synchronized void addError() { errors++; }
+ @SuppressWarnings({"UnusedParameters"})
+ public synchronized void addError(Throwable thr) { errors++; }
- public synchronized void report(int slowQueriesToShow) {
+ public synchronized void report(int slowSamplesToShow) {
System.out.println("Stats{" +
"runs=" + runs +
", samples=" + samples +
", errors=" + errors +
'}');
- List lst = new ArrayList(urlStats.values());
- Collections.sort(lst, new UrlStatComparator());
- for (Object stat : lst.subList(0, Math.min(slowQueriesToShow, lst.size()))) {
- UrlStat urlStat = (UrlStat) stat;
+ List<SampleStat> list = new ArrayList<SampleStat>(sampleStats.values());
+
+ // sort & cut states
+ Collections.sort(list, new SlowestSampleStatComparator());
+ list = list.subList(0, Math.min(slowSamplesToShow, list.size()));
+
+ for (SampleStat sampleStat : list) {
+
+ // cut string
+ String statName = sampleStat.getName();
+ statName = statName.substring(0, Math.min(45, statName.length()));
+
System.out.printf("min=%5d avg=%8.2f max=%5d %s%n",
- urlStat.getMinimum(),
- urlStat.getAvarage(),
- urlStat.getMaximum(),
- urlStat.getUrl().substring(0, Math.min(45, urlStat.getUrl().length())));
+ sampleStat.getMinimum(),
+ sampleStat.getAverage(),
+ sampleStat.getMaximum(),
+ statName);
}
}
- private class UrlStatComparator implements Comparator<UrlStat> {
- public int compare(UrlStat o1, UrlStat o2) {
- return -Double.compare(o1.getAvarage(), o2.getAvarage());
+ private class SlowestSampleStatComparator implements Comparator<SampleStat> {
+ public int compare(SampleStat o1, SampleStat o2) {
+ return -Double.compare(o1.getAverage(), o2.getAverage());
}
}
}
View
18 src/main/resources/org/loadtest/Classes.groovy
@@ -17,24 +17,26 @@ class Variations{
for (int i = 0; i < args.length; i += 2) {
args[i] /= v;
}
- double coinExperiment = getBindings().RANDOM.nextDouble(1.0);
+ double coinExperiment = getBindings().RANDOM.nextDouble((double)1.0);
for (int i = 0; i < args.length; i += 2) {
if (coinExperiment < args[i]) {
- args[i+1]();
+ Closure closure = (Closure)args[i+1];
+ closure.call();
break;
}
}
}
- public static def any(Object ...args) {
- return any(Arrays.asList(args));
+ public static def takeAny(Object ...args) {
+ return takeAny(Arrays.asList(args));
}
- public static def any(List args) {
+ public static def takeAny(List args) {
synchronized(args) {
if (args.size() == 0) {
return null
};
+
return args.get(getBindings().RANDOM.nextInt(args.size()));
}
}
@@ -49,9 +51,9 @@ class HTTP {
public static def get(Closure reporter, String ...urls) {
long start = System.currentTimeMillis();
- String urlString = Variations.any(urls);
+ String urlString = Variations.takeAny(urls);
if (urlString == null) {
- throw new IllegalArgumentException("url should not be null");
+ throw new IllegalArgumentException("name should not be null");
}
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
@@ -70,7 +72,7 @@ class HTTP {
Object result = null;
while ((line = reader.readLine()) != null) {
if (reporter != null) {
- result = reporter(line);
+ result = reporter.call(line);
}
}
reader.close();
Please sign in to comment.
Something went wrong with that request. Please try again.