Skip to content

Commit

Permalink
Benchmark script.
Browse files Browse the repository at this point in the history
Create tests with Thrash for sandbox benchmarking.
  • Loading branch information
jhy committed Jul 2, 2011
1 parent 46e1e97 commit 59d5e26
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ target/
.classpath
.project
.settings/
*Thrash*

37 changes: 37 additions & 0 deletions src/test/java/org/jsoup/integration/Benchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.jsoup.integration;

import java.util.Date;

/**
Does an A/B test on two methods, and prints out how long each took.
@author Jonathan Hedley, jonathan@hedley.net */
public class Benchmark {
public static void run(Runnable a, Runnable b, int count) {
long aMillis;
long bMillis;

print("Running test A (x%d)", count);
aMillis = time(a, count);
print("Running test B");
bMillis = time(b, count);

print("\nResults:");
print("A: %.2fs", aMillis / 1000f);
print("B: %.2fs", bMillis / 1000f);
print("\nB ran in %.2f %% time of A\n", (bMillis *1f / aMillis * 1f) * 100f);
}

private static long time(Runnable test, int count) {
Date start = new Date();
for (int i = 0; i < count; i++) {
test.run();
}
Date end = new Date();
return end.getTime() - start.getTime();
}

private static void print(String msgFormat, Object... msgParams) {
System.out.println(String.format(msgFormat, msgParams));
}
}

0 comments on commit 59d5e26

Please sign in to comment.