Permalink
Browse files

Added crawler script

  • Loading branch information...
1 parent 8c570b6 commit fa3bd4c1cc27321e5d4a47efbd44673fe36c1685 cap_protect committed Nov 4, 2011
View
@@ -0,0 +1,15 @@
+// example of simple crawler
+// run: java -jar loadtest.jar -n 3 -d 1000 Example2.groovy
+import java.util.regex.Pattern;
+
+try {
+ List urls = GLOBALS.list("urls", "http://google.com");
+ String url = Variations.any(urls);
+ HTTP.get({ line ->
+ for (String href : HTTP.parse(Pattern.compile("href=[\"\']([^\"\']+)[\"\']"), line)) {
+ urls.add(HTTP.resolve(url, href));
+ }
+ }, url);
+} catch (Throwable thr) {
+ System.err.println(thr);
+}
@@ -1,39 +0,0 @@
-package org.loadtest;
-
-import javax.swing.event.ListSelectionEvent;
-import java.util.*;
-
-/**
- * Object used by scripts to interchange values
- */
-public class Globals {
- private Map values = new HashMap();
-
- public synchronized void put(String name, Object value) {
- values.put(name, value);
- }
-
- public synchronized Object get(String name) {
- return values.get(name);
- }
-
- public synchronized List list(String name, Object ...defaultValues) {
- List list = (List) values.get(name);
- if (list == null) {
- list = Collections.synchronizedList(new ArrayList());
- list.addAll(Arrays.asList(defaultValues));
- values.put(name, list);
- }
- return list;
- }
-
- public synchronized long increment(String name) {
- Number num = (Number)values.get(name);
- if (num == null) {
- num = 0L;
- }
- long res = num.longValue();
- values.put(name, res + 1);
- return res;
- }
-}
@@ -18,7 +18,7 @@
public class ScriptRunner implements Runnable {
private LoadTest load;
private Stats stats = new Stats();
- private Globals globals = new Globals();
+ private Object globals;
private Random random = new Random();
private static ThreadLocal local = new ThreadLocal();
@@ -29,7 +29,6 @@ public ScriptRunner(LoadTest load) {
protected Binding createNewBinding() {
Binding binding = new Binding();
binding.setVariable("STATS", stats);
- binding.setVariable("GLOBALS", globals);
binding.setVariable("RANDOM", random);
return binding;
}
@@ -52,13 +51,21 @@ public void run() {
shell.evaluate(new GroovyCodeSource(ScriptRunner.class.getResource("Classes.groovy")));
- for (File script : load.getOptions().getScripts()) {
- shell.evaluate(script);
+ synchronized (this) {
+ if (globals == null) {
+ globals = shell.evaluate("new Globals()");
+ }
}
+ binding.setVariable("GLOBALS", globals);
for (String script : load.getOptions().getScriptTexts()) {
shell.evaluate(script);
}
+
+ for (File script : load.getOptions().getScripts()) {
+ shell.evaluate(script);
+ }
+
} catch (CompilationFailedException e) {
stats.addError();
errorCase(e);
@@ -119,3 +119,35 @@ class HTTP {
}
+public class Globals {
+ private Map values = new HashMap();
+
+ public synchronized void put(String name, Object value) {
+ values.put(name, value);
+ }
+
+ public synchronized Object get(String name) {
+ return values.get(name);
+ }
+
+ public synchronized List list(String name, String []defaultValues) {
+ List list = (List) values.get(name);
+ if (list == null) {
+ list = Collections.synchronizedList(new ArrayList(Arrays.asList(defaultValues)));
+ values.put(name, list);
+ }
+ return list;
+ }
+
+ public synchronized long increment(String name) {
+ Number num = (Number)values.get(name);
+ if (num == null) {
+ num = 0L;
+ }
+ long res = num.longValue();
+ values.put(name, res + 1);
+ return res;
+ }
+}
+
+

0 comments on commit fa3bd4c

Please sign in to comment.