Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Documentation and groovy environment changes

  • Loading branch information...
commit d1a5be9c4195946f28b244452161c6720652db35 1 parent e5351cf
cap_protect authored
Showing with 129 additions and 111 deletions.
  1. +17 −1 README.textile
  2. +112 −110 src/main/resources/org/loadtest/Classes.groovy
View
18 README.textile
@@ -10,7 +10,23 @@ java -jar loadtest-1.0.jar Example1.groovy
h2. Environment
-In groovy following classes avialable: HTTP and Variations.
+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.
+
+h3. Variations
+
+ select(prob1, closure1, prob2, closure2, prob3, closure3, ...)
+
+ any(arguments...);
+
+h3. HTTP
+
+ get([Closure reporter, ]String ...urls)
+
+ parse(Pattern regex, String ...html)
+
+ resolve(String uri, String path)
h2. License
View
222 src/main/resources/org/loadtest/Classes.groovy
@@ -1,110 +1,112 @@
-import java.util.regex.Pattern
-import java.util.regex.Matcher
-import org.loadtest.ScriptRunner
-
-
-class Variations{
- private static def getBindings() {
- return ScriptRunner.getGroovyShell().getContext();
- }
-
- public static def select(...args) {
- double v = 0;
- for (int i = 0; i < args.length; i += 2) {
- v += args[i];
- args[i] = v;
- }
- for (int i = 0; i < args.length; i += 2) {
- args[i] /= v;
- }
- double coinExperiment = getBindings().RANDOM.nextDouble(1.0);
- for (int i = 0; i < args.length; i += 2) {
- if (coinExperiment < args[i]) {
- args[i+1]();
- break;
- }
- }
- }
-
- public static def any(Object ...args) {
- return args[getBindings().RANDOM.nextInt(args.length)];
- }
-
- public static void main(String[] args) {}
-}
-
-class HTTP {
- private static def getBindings() {
- return ScriptRunner.getGroovyShell().getContext();
- }
-
- public static def get(Closure reporter, String ...urls) {
- long start = System.currentTimeMillis();
- String urlString = Variations.any(urls);
- URL url = new URL(urlString);
- URLConnection conn = url.openConnection();
- String contentType = conn.getContentType();
- String charset = "UTF-8";
- if (contentType != null && !contentType.isEmpty()) {
- Matcher matcher = Pattern.compile("charset=([^ ]+)", Pattern.CASE_INSENSITIVE)
- .matcher(contentType);
- if (matcher.find()) {
- charset = matcher.group(1);
- }
- }
- InputStream input = url.openStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
- String line;
- Object result = null;
- while ((line = reader.readLine()) != null) {
- if (reporter != null) {
- result = reporter(line);
- }
- }
- reader.close();
- long end = System.currentTimeMillis();
- getBindings().STATS.addQuery(urlString, end - start);
- return result;
- }
-
- public static def get(String ...urls) {
- return get(null, urls);
- }
-
- public static def parse(Pattern regex, String ...html) {
- List result = new ArrayList();
- for (String line : html) {
- Matcher matcher = regex.matcher(line);
- while (matcher.find()) {
- String []res = new String[matcher.groupCount()];
- for (int i = 1; i <= matcher.groupCount(); i++) {
- res[i - 1] = matcher.group(i);
- }
- result.add(res);
- }
- }
- boolean oneSizer = true;;
- for (String []arr : result) {
- if (arr.length != 1) oneSizer = false;
- }
- if (oneSizer) {
- String []resultArray = new String[result.size()];
- int i = 0;
- for (String []arr : result) {
- resultArray[i++] = arr[0];
- }
- return resultArray;
- } else {
- String [][]resultArray = result.toArray(new String[result.size()][0]);
- return resultArray;
- }
- }
-
- public static def resolve(String uri, String path) {
- return new URI(uri).resolve(path).toString();
- }
-
- public static void main(String[] args) {}
-}
-
-
+import java.util.regex.Pattern
+import java.util.regex.Matcher
+import org.loadtest.ScriptRunner
+
+
+class Variations{
+ private static def getBindings() {
+ return ScriptRunner.getGroovyShell().getContext();
+ }
+
+ public static def select(...args) {
+ double v = 0;
+ for (int i = 0; i < args.length; i += 2) {
+ v += args[i];
+ args[i] = v;
+ }
+ for (int i = 0; i < args.length; i += 2) {
+ args[i] /= v;
+ }
+ double coinExperiment = getBindings().RANDOM.nextDouble(1.0);
+ for (int i = 0; i < args.length; i += 2) {
+ if (coinExperiment < args[i]) {
+ args[i+1]();
+ break;
+ }
+ }
+ }
+
+ public static def any(Object ...args) {
+ return args[getBindings().RANDOM.nextInt(args.length)];
+ }
+
+ public static void main(String[] args) {}
+}
+
+class HTTP {
+ private static def getBindings() {
+ return ScriptRunner.getGroovyShell().getContext();
+ }
+
+ public static def get(Closure reporter, String ...urls) {
+ long start = System.currentTimeMillis();
+ String urlString = Variations.any(urls);
+ URL url = new URL(urlString);
+ URLConnection conn = url.openConnection();
+ String contentType = conn.getContentType();
+ String charset = "UTF-8";
+ if (contentType != null && !contentType.isEmpty()) {
+ Matcher matcher = Pattern.compile("charset=([^ ]+)", Pattern.CASE_INSENSITIVE)
+ .matcher(contentType);
+ if (matcher.find()) {
+ charset = matcher.group(1);
+ }
+ }
+ InputStream input = url.openStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
+ String line;
+ Object result = null;
+ while ((line = reader.readLine()) != null) {
+ if (reporter != null) {
+ result = reporter(line);
+ }
+ }
+ reader.close();
+ long end = System.currentTimeMillis();
+ getBindings().STATS.addQuery(urlString, end - start);
+ return result;
+ }
+
+ public static def get(String ...urls) {
+ List result = new ArrayList();
+ return get({line->result.add(list)}, urls);
+ return result.toArray(new String[result.size()]);
+ }
+
+ public static def parse(Pattern regex, String ...html) {
+ List result = new ArrayList();
+ for (String line : html) {
+ Matcher matcher = regex.matcher(line);
+ while (matcher.find()) {
+ String []res = new String[matcher.groupCount()];
+ for (int i = 1; i <= matcher.groupCount(); i++) {
+ res[i - 1] = matcher.group(i);
+ }
+ result.add(res);
+ }
+ }
+ boolean oneSizer = true;;
+ for (String []arr : result) {
+ if (arr.length != 1) oneSizer = false;
+ }
+ if (oneSizer) {
+ String []resultArray = new String[result.size()];
+ int i = 0;
+ for (String []arr : result) {
+ resultArray[i++] = arr[0];
+ }
+ return resultArray;
+ } else {
+ String [][]resultArray = result.toArray(new String[result.size()][0]);
+ return resultArray;
+ }
+ }
+
+ public static def resolve(String uri, String path) {
+ return new URI(uri).resolve(path).toString();
+ }
+
+ public static void main(String[] args) {}
+}
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.