From b2ec1a3d9af3540c0efbf8e32f24ee880cabfce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Szathma=CC=81ry?= Date: Sun, 27 Dec 2015 10:01:06 +0100 Subject: [PATCH 1/2] improve benchmark code * path to database can be parametrized * do separate warmup and benchmark loops (so JIT can kick in) * use fixed random seeds for repeatable runs --- sample/Benchmark.java | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/sample/Benchmark.java b/sample/Benchmark.java index 39ebafa9..fe90b5a7 100644 --- a/sample/Benchmark.java +++ b/sample/Benchmark.java @@ -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); } } From aaed9213fa080b47c0950b6d1c5949d7fc8a0609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Szathma=CC=81ry?= Date: Sun, 27 Dec 2015 15:01:38 +0100 Subject: [PATCH 2/2] benchmark: remove dependency on Guava --- sample/Benchmark.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sample/Benchmark.java b/sample/Benchmark.java index fe90b5a7..e9a9a7ae 100644 --- a/sample/Benchmark.java +++ b/sample/Benchmark.java @@ -4,7 +4,6 @@ import java.util.Random; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.net.InetAddresses; import com.maxmind.db.InvalidDatabaseException; import com.maxmind.db.Reader; import com.maxmind.db.Reader.FileMode; @@ -34,8 +33,10 @@ private static void loop(String msg, File file, int loops) throws IOException { private static void bench(Reader r, int count, int seed) throws IOException { Random random = new Random(seed); long startTime = System.nanoTime(); + byte[] address = new byte[4]; for (int i = 0; i < count; i++) { - InetAddress ip = InetAddresses.fromInteger(random.nextInt()); + random.nextBytes(address); + InetAddress ip = InetAddress.getByAddress(address); JsonNode t = r.get(ip); if (TRACE) { if (i % 50000 == 0) {