Permalink
Browse files

added Example2

  • Loading branch information...
1 parent a0219cb commit b5fdad5f7917e1c1a5e8f95ab3eb8fbdeae1ae01 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);
+}
View
Binary file not shown.
@@ -1,7 +1,7 @@
package org.loadtest;
-import java.util.HashMap;
-import java.util.Map;
+import javax.swing.event.ListSelectionEvent;
+import java.util.*;
/**
* Object used by scripts to interchange values
@@ -16,6 +16,17 @@ public synchronized void put(String name, Object 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) {
@@ -92,14 +92,14 @@ public synchronized void report(int slowQueriesToShow) {
urlStat.getMinimum(),
urlStat.getAvarage(),
urlStat.getMaximum(),
- urlStat.getUrl());
+ urlStat.getUrl().substring(0, Math.min(45, urlStat.getUrl().length())));
}
}
private class UrlStatComparator implements Comparator<UrlStat> {
public int compare(UrlStat o1, UrlStat o2) {
- return Double.compare(o1.getAvarage(), o2.getAvarage());
+ return -Double.compare(o1.getAvarage(), o2.getAvarage());
}
}
}
@@ -30,6 +30,12 @@ class Variations{
return args[getBindings().RANDOM.nextInt(args.length)];
}
+ public static def any(List args) {
+ synchronized(args) {
+ return args.get(getBindings().RANDOM.nextInt(args.size()));
+ }
+ }
+
public static void main(String[] args) {}
}
@@ -69,7 +75,7 @@ class HTTP {
public static def get(String ...urls) {
List result = new ArrayList();
- return get({line->result.add(list)}, urls);
+ HTTP.get({line->result.add(line);}, urls);
return result.toArray(new String[result.size()]);
}

0 comments on commit b5fdad5

Please sign in to comment.