-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create tests with Thrash for sandbox benchmarking.
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ target/ | |
.classpath | ||
.project | ||
.settings/ | ||
*Thrash* | ||
|
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 |
---|---|---|
@@ -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)); | ||
} | ||
} |