Skip to content

Commit

Permalink
improve benchmark code
Browse files Browse the repository at this point in the history
 * path to database can be parametrized
 * do separate warmup and benchmark loops (so JIT can kick in)
 * use fixed random seeds for repeatable runs
  • Loading branch information
phraktle committed Dec 27, 2015
1 parent 8a874ee commit b2ec1a3
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions sample/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,43 @@

public class Benchmark {

public static void main(String[] args) throws IOException,
InvalidDatabaseException {
File file = new File("GeoLite2-City.mmdb");
private final static int COUNT = 1000000;
private final static int WARMUPS = 3;
private final static int BENCHMARKS = 5;
private final static boolean TRACE = false;

Reader r = new Reader(file, FileMode.MEMORY_MAPPED);
Random random = new Random();
int count = 1000000;
public static void main(String[] args) throws IOException, InvalidDatabaseException {
File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb");
loop("Warming up", file, WARMUPS);
loop("Benchmarking", file, BENCHMARKS);
}

private static void loop(String msg, File file, int loops) throws IOException {
System.out.println(msg);
for (int i = 0; i < loops; i++) {
Reader r = new Reader(file, FileMode.MEMORY_MAPPED);
bench(r, COUNT, i);
}
System.out.println();
}

private static void bench(Reader r, int count, int seed) throws IOException {
Random random = new Random(seed);
long startTime = System.nanoTime();
for (int i = 0; i < count; i++) {
InetAddress ip = InetAddresses.fromInteger(random.nextInt());
if (i % 50000 == 0) {
System.out.println(i + " " + ip);
}
JsonNode t = r.get(ip);
if (TRACE) {
if (i % 50000 == 0) {
System.out.println(i + " " + ip);
System.out.println(t);
}
}
}
long endTime = System.nanoTime();

long duration = endTime - startTime;
System.out.println("Requests per second: " + count * 1000000000.0
/ (duration));
long qps = count * 1000000000L / duration;
System.out.println("Requests per second: " + qps);
}
}

0 comments on commit b2ec1a3

Please sign in to comment.