Skip to content

Commit 6461ce6

Browse files
committed
Use JMH for benchmarks.
1 parent 7fd195e commit 6461ce6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+412
-300
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "src/test/resources/maxmind-db"]
2-
path = src/test/resources/maxmind-db
2+
path = reader/src/test/resources/maxmind-db
33
url = https://github.com/maxmind/MaxMind-DB

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ Maven, you must
188188
Failure to do so will result in `InvalidDatabaseException` exceptions being
189189
thrown when querying the database.
190190

191+
## Benchmarking ##
192+
193+
Set an environment variable `GEO_LITE` with the path to `GeoLite2-City.mmdb`.
194+
195+
```shell
196+
mvn -Dcheckstyle.skip -DskipTests clean package
197+
GEO_LITE=/.../GeoLite2-City.mmdb java -jar benchmarks/target/microbenchmarks.jar
198+
```
199+
200+
For more, see [https://github.com/openjdk/jmh](https://github.com/openjdk/jmh) or
201+
`java -jar benchmarks/target/microbenchmarks.jar -h`.
202+
191203
## Format ##
192204

193205
The MaxMind DB format is an open format for quickly mapping IP addresses to

benchmarks/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.maxmind.db</groupId>
5+
<artifactId>benchmarks</artifactId>
6+
<version>3.1.2-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<jmhVersion>1.37</jmhVersion>
10+
</properties>
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.maxmind.db</groupId>
14+
<artifactId>maxmind-db</artifactId>
15+
<version>3.1.2-SNAPSHOT</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.openjdk.jmh</groupId>
19+
<artifactId>jmh-core</artifactId>
20+
<version>${jmhVersion}</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-compiler-plugin</artifactId>
28+
<version>3.13.0</version>
29+
<configuration>
30+
<release>11</release>
31+
<source>11</source>
32+
<target>11</target>
33+
<annotationProcessorPaths>
34+
<path>
35+
<groupId>org.openjdk.jmh</groupId>
36+
<artifactId>jmh-generator-annprocess</artifactId>
37+
<version>${jmhVersion}</version>
38+
</path>
39+
</annotationProcessorPaths>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-shade-plugin</artifactId>
45+
<version>3.6.0</version>
46+
<executions>
47+
<execution>
48+
<phase>package</phase>
49+
<goals>
50+
<goal>shade</goal>
51+
</goals>
52+
<configuration>
53+
<finalName>microbenchmarks</finalName>
54+
<transformers>
55+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
56+
<mainClass>org.openjdk.jmh.Main</mainClass>
57+
</transformer>
58+
</transformers>
59+
<filters>
60+
<filter>
61+
<artifact>*:*</artifact>
62+
<excludes>
63+
<exclude>META-INF/services/javax.annotation.processing.Processor</exclude>
64+
</excludes>
65+
</filter>
66+
</filters>
67+
</configuration>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.maxmind.db;
2+
3+
import com.maxmind.db.Reader.FileMode;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
8+
import java.util.Map;
9+
import java.util.Random;
10+
import java.util.concurrent.TimeUnit;
11+
import org.openjdk.jmh.annotations.Benchmark;
12+
import org.openjdk.jmh.annotations.BenchmarkMode;
13+
import org.openjdk.jmh.annotations.Fork;
14+
import org.openjdk.jmh.annotations.Measurement;
15+
import org.openjdk.jmh.annotations.Mode;
16+
import org.openjdk.jmh.annotations.OperationsPerInvocation;
17+
import org.openjdk.jmh.annotations.OutputTimeUnit;
18+
import org.openjdk.jmh.annotations.Scope;
19+
import org.openjdk.jmh.annotations.Setup;
20+
import org.openjdk.jmh.annotations.State;
21+
import org.openjdk.jmh.annotations.Warmup;
22+
import org.openjdk.jmh.infra.Blackhole;
23+
24+
@State(Scope.Benchmark)
25+
@Warmup(iterations = 3, time = 5)
26+
@Measurement(iterations = 10, time = 5)
27+
@Fork(1)
28+
@BenchmarkMode(Mode.AverageTime)
29+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
30+
public class BenchmarkGet {
31+
private static final int COUNT = 1_000_000;
32+
33+
private static InetAddress[] getInetAddresses(final int seed) throws UnknownHostException {
34+
final InetAddress[] addresses = new InetAddress[COUNT];
35+
final Random random = new Random(seed);
36+
final byte[] address = new byte[4];
37+
for (int addressIx = 0; addressIx < COUNT; addressIx++) {
38+
random.nextBytes(address);
39+
addresses[addressIx] = InetAddress.getByAddress(address);
40+
}
41+
return addresses;
42+
}
43+
44+
InetAddress[] addresses;
45+
Reader reader;
46+
Reader cachedReader;
47+
48+
@Setup
49+
public void setup() throws IOException {
50+
addresses = getInetAddresses(0);
51+
final File database = new File(System.getenv("GEO_LITE"));
52+
reader = new Reader(database, FileMode.MEMORY_MAPPED, NoCache.getInstance());
53+
cachedReader = new Reader(database, FileMode.MEMORY_MAPPED, new CHMCache());
54+
}
55+
56+
@Benchmark
57+
@OperationsPerInvocation(COUNT)
58+
public void withoutCaching(Blackhole bh) throws IOException {
59+
for (InetAddress address: addresses) {
60+
bh.consume(reader.get(address, Map.class));
61+
}
62+
}
63+
64+
@Benchmark
65+
@OperationsPerInvocation(COUNT)
66+
public void withCaching(Blackhole bh) throws IOException {
67+
for (InetAddress address: addresses) {
68+
bh.consume(cachedReader.get(address, Map.class));
69+
}
70+
}
71+
}

dev-bin/release.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ fi
9797

9898

9999
# could be combined with the primary build
100-
mvn release:clean
101-
mvn release:prepare -DreleaseVersion="$version" -Dtag="$tag"
102-
mvn release:perform
100+
mvn -pl reader release:clean
101+
mvn -pl reader release:prepare -DreleaseVersion="$version" -Dtag="$tag"
102+
mvn -pl reader release:perform
103103
rm -fr ".gh-pages/doc/$tag"
104-
cp -r target/checkout/target/reports/apidocs ".gh-pages/doc/$tag"
104+
cp -r reader/target/checkout/target/reports/apidocs ".gh-pages/doc/$tag"
105105
rm .gh-pages/doc/latest
106106
ln -fs "$tag" .gh-pages/doc/latest
107107

0 commit comments

Comments
 (0)