Skip to content

Commit

Permalink
Documentation and groovy environment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cap_protect committed Nov 4, 2011
1 parent e5351cf commit d1a5be9
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 111 deletions.
18 changes: 17 additions & 1 deletion README.textile
Expand Up @@ -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

Expand Down
222 changes: 112 additions & 110 deletions 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) {}
}


0 comments on commit d1a5be9

Please sign in to comment.