From df296093a9aa60341ee50d41d039a3dc49f9685b Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Sun, 3 Nov 2024 05:50:11 -0500 Subject: [PATCH] add benchmark --- .../benchmarks/BenchmarkLoopLineMerge.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLoopLineMerge.java diff --git a/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLoopLineMerge.java b/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLoopLineMerge.java new file mode 100644 index 0000000000..a003f8ca9b --- /dev/null +++ b/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLoopLineMerge.java @@ -0,0 +1,81 @@ +package com.onthegomap.planetiler.benchmarks; + +import com.onthegomap.planetiler.util.Format; +import com.onthegomap.planetiler.util.LoopLineMerger; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import org.locationtech.jts.geom.CoordinateXY; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.operation.linemerge.LineMerger; +import org.locationtech.jts.util.GeometricShapeFactory; + +public class BenchmarkLoopLineMerge { + + public static void main(String[] args) { + for (int i = 0; i < 10; i++) { + System.err.println( + " JTS line merger (/s):\t" + + timeJts(10) + "\t" + + timeJts(1_000) + "\t" + + timeJts(10_000) + "\t" + + timeJts(100_000)); + System.err.println( + "loop line merger (/s):\t" + + timeLoop(10) + "\t" + + timeLoop(1_000) + "\t" + + timeLoop(10_000) + "\t" + + timeLoop(100_000)); + System.err.println(); + } + } + + private static String timeLoop(int parts) { + return time(parts, geom -> { + var merger = new LoopLineMerger(); + merger.add(geom); + return merger.getMergedLineStrings(0.1, 0.1); + }); + } + + private static String timeJts(int parts) { + return time(parts, geom -> { + var merger = new LineMerger(); + merger.add(geom); + return merger.getMergedLineStrings(); + }); + } + + private static String time(int parts, Function> fn) { + Geometry multiLinestring = makeCircles(parts); + long start = System.nanoTime(); + long end = start + Duration.ofSeconds(1).toNanos(); + int num = 0; + for (; System.nanoTime() < end;) { + fn.apply(multiLinestring); + num++; + } + return Format.defaultInstance() + .numeric(Math.round(num * 1d / ((System.nanoTime() - start) * 1d / Duration.ofSeconds(1).toNanos())), true); + } + + private static Geometry makeCircles(int parts) { + var factory = new GeometricShapeFactory(); + factory.setCentre(new CoordinateXY(0, 0)); + factory.setSize(2); + factory.setNumPoints(parts); + List lines = new ArrayList<>(); + lines.add(factory.createArc(0, Math.PI * 2)); + factory.setCentre(new CoordinateXY(1, 0)); + lines.add(factory.createArc(0, Math.PI * 2)); + factory.setCentre(new CoordinateXY(1, 1)); + lines.add(factory.createArc(0, Math.PI * 2)); + factory.setCentre(new CoordinateXY(0, 1)); + lines.add(factory.createArc(0, Math.PI * 2)); + return new GeometryFactory().createMultiLineString(lines.toArray(LineString[]::new)); + } +}