|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.benchmark.spatial; |
| 10 | + |
| 11 | +import org.elasticsearch.geometry.LinearRing; |
| 12 | +import org.elasticsearch.geometry.simplify.GeometrySimplifier; |
| 13 | +import org.elasticsearch.geometry.simplify.SimplificationErrorCalculator; |
| 14 | +import org.openjdk.jmh.annotations.Benchmark; |
| 15 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 16 | +import org.openjdk.jmh.annotations.Fork; |
| 17 | +import org.openjdk.jmh.annotations.Measurement; |
| 18 | +import org.openjdk.jmh.annotations.Mode; |
| 19 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 20 | +import org.openjdk.jmh.annotations.Param; |
| 21 | +import org.openjdk.jmh.annotations.Scope; |
| 22 | +import org.openjdk.jmh.annotations.Setup; |
| 23 | +import org.openjdk.jmh.annotations.State; |
| 24 | +import org.openjdk.jmh.annotations.Warmup; |
| 25 | +import org.openjdk.jmh.infra.Blackhole; |
| 26 | + |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.FileNotFoundException; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | +import java.io.InputStreamReader; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.text.ParseException; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.zip.GZIPInputStream; |
| 36 | + |
| 37 | +@Fork(1) |
| 38 | +@Warmup(iterations = 5) |
| 39 | +@Measurement(iterations = 5) |
| 40 | +@BenchmarkMode(Mode.AverageTime) |
| 41 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 42 | +@State(Scope.Thread) |
| 43 | +public class GeometrySimplificationBenchmark { |
| 44 | + @Param({ "cartesiantrianglearea", "triangleArea", "triangleheight", "heightandbackpathdistance" }) |
| 45 | + public String calculatorName; |
| 46 | + |
| 47 | + @Param({ "10", "100", "1000", "10000", "20000" }) |
| 48 | + public int maxPoints; |
| 49 | + |
| 50 | + private GeometrySimplifier<LinearRing> simplifier; |
| 51 | + private static LinearRing ring; |
| 52 | + |
| 53 | + @Setup |
| 54 | + public void setup() throws ParseException, IOException { |
| 55 | + SimplificationErrorCalculator calculator = SimplificationErrorCalculator.byName(calculatorName); |
| 56 | + this.simplifier = new GeometrySimplifier.LinearRingSimplifier(maxPoints, calculator); |
| 57 | + if (ring == null) { |
| 58 | + ring = loadRing("us.json.gz"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Benchmark |
| 63 | + public void simplify(Blackhole bh) { |
| 64 | + bh.consume(simplifier.simplify(ring)); |
| 65 | + } |
| 66 | + |
| 67 | + private static LinearRing loadRing(@SuppressWarnings("SameParameterValue") String name) throws IOException, ParseException { |
| 68 | + String json = loadJsonFile(name); |
| 69 | + org.apache.lucene.geo.Polygon[] lucenePolygons = org.apache.lucene.geo.Polygon.fromGeoJSON(json); |
| 70 | + LinearRing ring = null; |
| 71 | + for (org.apache.lucene.geo.Polygon lucenePolygon : lucenePolygons) { |
| 72 | + double[] x = lucenePolygon.getPolyLons(); |
| 73 | + double[] y = lucenePolygon.getPolyLats(); |
| 74 | + if (ring == null || x.length > ring.length()) { |
| 75 | + ring = new LinearRing(x, y); |
| 76 | + } |
| 77 | + } |
| 78 | + return ring; |
| 79 | + } |
| 80 | + |
| 81 | + private static String loadJsonFile(String name) throws IOException { |
| 82 | + InputStream is = GeometrySimplificationBenchmark.class.getResourceAsStream(name); |
| 83 | + if (is == null) { |
| 84 | + throw new FileNotFoundException("classpath resource not found: " + name); |
| 85 | + } |
| 86 | + if (name.endsWith(".gz")) { |
| 87 | + is = new GZIPInputStream(is); |
| 88 | + } |
| 89 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); |
| 90 | + StringBuilder builder = new StringBuilder(); |
| 91 | + reader.lines().forEach(builder::append); |
| 92 | + return builder.toString(); |
| 93 | + } |
| 94 | +} |
0 commit comments