Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation and groovy environment changes
- Loading branch information
cap_protect
committed
Nov 4, 2011
1 parent
e5351cf
commit d1a5be9
Showing
2 changed files
with
129 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} | ||
} | ||
|
||
|