From b3186691ef4361d30621ca69dbb26bddf1c3dfb1 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Sun, 3 Nov 2024 06:08:37 -0500 Subject: [PATCH] more realistic lines --- .../benchmarks/BenchmarkLineMerge.java | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLineMerge.java b/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLineMerge.java index 7b6ff96675..c85691cc99 100644 --- a/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLineMerge.java +++ b/planetiler-benchmarks/src/main/java/com/onthegomap/planetiler/benchmarks/BenchmarkLineMerge.java @@ -1,18 +1,20 @@ package com.onthegomap.planetiler.benchmarks; +import com.onthegomap.planetiler.geo.GeoUtils; 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.Random; import java.util.function.Function; +import org.locationtech.jts.geom.Coordinate; 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 BenchmarkLineMerge { @@ -20,38 +22,39 @@ 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)); + timeJts(10, 10) + "\t" + + timeJts(10, 100) + "\t" + + timeJts(1000, 10) + "\t" + + timeJts(1000, 1000)); System.err.println( "loop line merger (/s):\t" + - timeLoop(10) + "\t" + - timeLoop(1_000) + "\t" + - timeLoop(10_000) + "\t" + - timeLoop(100_000)); + timeLoop(10, 10) + "\t" + + timeLoop(10, 100) + "\t" + + timeLoop(1000, 10)); + // TODO currently this does not finish: + // + "\t" + timeLoop(1000, 1000)); System.err.println(); } } - private static String timeLoop(int parts) { - return time(parts, geom -> { + private static String timeLoop(int lines, int parts) { + return time(lines, 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 -> { + private static String timeJts(int lines, int parts) { + return time(lines, parts, geom -> { var merger = new LineMerger(); merger.add(geom); return merger.getMergedLineStrings(); }); } - private static String time(int parts, Function> fn) { - Geometry multiLinestring = makeCircles(parts); + private static String time(int lines, int parts, Function> fn) { + Geometry multiLinestring = makeLines(lines, parts); long start = System.nanoTime(); long end = start + Duration.ofSeconds(1).toNanos(); int num = 0; @@ -63,19 +66,23 @@ private static String time(int parts, Function> .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)); + private static Geometry makeLines(int lines, int parts) { + List result = new ArrayList<>(); + var random = new Random(0); + for (int i = 0; i < lines; i++) { + Coordinate[] coords = new Coordinate[parts]; + double x = random.nextInt(100); + double y = random.nextInt(100); + double dx = random.nextInt(-3, 3); + double dy = random.nextInt(-3, 3); + for (int j = 0; j < parts; j++) { + coords[j] = new CoordinateXY( + x += dx, + y += dy + ); + } + result.add(GeoUtils.JTS_FACTORY.createLineString(coords)); + } + return new GeometryFactory().createMultiLineString(result.toArray(LineString[]::new)); } }