Skip to content

Commit

Permalink
Add H3 Benchmarks (elastic#111359)
Browse files Browse the repository at this point in the history
Microbenchmarks for H3
  • Loading branch information
iverase authored Aug 22, 2024
1 parent 62305f0 commit 585bd64
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
// us to invoke the JMH uberjar as usual.
exclude group: 'net.sf.jopt-simple', module: 'jopt-simple'
}
api(project(':libs:elasticsearch-h3'))
api(project(':modules:aggregations'))
api(project(':x-pack:plugin:esql-core'))
api(project(':x-pack:plugin:esql'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.benchmark.h3;

import org.elasticsearch.h3.H3;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.TimeUnit;

@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 25, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
public class H3Benchmark {

@Benchmark
public void pointToH3(H3State state, Blackhole bh) {
for (int i = 0; i < state.points.length; i++) {
for (int res = 0; res <= 15; res++) {
bh.consume(H3.geoToH3(state.points[i][0], state.points[i][1], res));
}
}
}

@Benchmark
public void h3Boundary(H3State state, Blackhole bh) {
for (int i = 0; i < state.h3.length; i++) {
bh.consume(H3.h3ToGeoBoundary(state.h3[i]));
}
}

public static void main(String[] args) throws Exception {
Main.main(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.benchmark.h3;

import org.elasticsearch.h3.H3;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.io.IOException;
import java.util.Random;

@State(Scope.Benchmark)
public class H3State {

double[][] points = new double[1000][2];
long[] h3 = new long[1000];

@Setup(Level.Trial)
public void setupTrial() throws IOException {
Random random = new Random(1234);
for (int i = 0; i < points.length; i++) {
points[i][0] = random.nextDouble() * 180 - 90; // lat
points[i][1] = random.nextDouble() * 360 - 180; // lon
int res = random.nextInt(16); // resolution
h3[i] = H3.geoToH3(points[i][0], points[i][1], res);
}
}
}

0 comments on commit 585bd64

Please sign in to comment.