msbarry-tilecoord-hilbert
Mike Barry 2022-06-23 21:10:46 -04:00
rodzic 230727a776
commit a0488dde32
1 zmienionych plików z 51 dodań i 16 usunięć

Wyświetl plik

@ -5,6 +5,7 @@ import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.geo.TileCoord;
import com.onthegomap.planetiler.mbtiles.Mbtiles;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -13,6 +14,8 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,35 +30,52 @@ public class BenchmarkMbtilesRead {
Arguments arguments = Arguments.fromArgs(args);
int repetitions = arguments.getInteger("bench_repetitions", "number of repetitions", 10);
int nrTileReads = arguments.getInteger("bench_nr_tile_reads", "number of tiles to read", 500_000);
Path tilesPath = arguments.file("bench_tiles_path", "path to z,x,y tiles");
int nrTileReads = arguments.getInteger("bench_nr_tile_reads", "number of tiles to read",
tilesPath != null ? lineCount(tilesPath) : 500_000);
int preWarms = arguments.getInteger("bench_pre_warms", "number of pre warm runs", 3);
List<Path> mbtilesPaths = arguments.getList("bench_mbtiles", "the mbtiles file to read from", List.of()).stream()
.map(Paths::get).toList();
if (mbtilesPaths.isEmpty()) {
throw new IllegalArgumentException("pass one or many paths to the same mbtiles file");
}
mbtilesPaths.stream().forEach(p -> {
for (var p : mbtilesPaths) {
if (!Files.exists(p) || !Files.isRegularFile(p)) {
throw new IllegalArgumentException("%s does not exists".formatted(p));
}
});
}
List<TileCoord> randomCoordsToFetchPerRepetition = new LinkedList<>();
do {
try (var db = Mbtiles.newReadOnlyDatabase(mbtilesPaths.get(0))) {
try (var statement = db.connection().prepareStatement(SELECT_RANDOM_COORDS)) {
statement.setInt(1, nrTileReads - randomCoordsToFetchPerRepetition.size());
var rs = statement.executeQuery();
while (rs.next()) {
int x = rs.getInt("tile_column");
int y = rs.getInt("tile_row");
int z = rs.getInt("zoom_level");
randomCoordsToFetchPerRepetition.add(TileCoord.ofXYZ(x, (1 << z) - 1 - y, z));
int toFetch = nrTileReads - randomCoordsToFetchPerRepetition.size();
if (tilesPath != null) {
Predicate<String> isValidLine = Pattern.compile("^\\d+,\\d+,\\d+$").asMatchPredicate();
try (var lines = Files.lines(tilesPath)) {
lines
.filter(isValidLine)
.map(s -> s.split(","))
.limit(toFetch)
.forEach(parts -> {
randomCoordsToFetchPerRepetition
.add(TileCoord.ofXYZ(Integer.parseInt(parts[2]), Integer.parseInt(parts[0]),
Integer.parseInt(parts[1])));
});
}
} else {
try (var db = Mbtiles.newReadOnlyDatabase(mbtilesPaths.get(0))) {
try (var statement = db.connection().prepareStatement(SELECT_RANDOM_COORDS)) {
statement.setInt(1, toFetch);
var rs = statement.executeQuery();
while (rs.next()) {
int x = rs.getInt("tile_column");
int y = rs.getInt("tile_row");
int z = rs.getInt("zoom_level");
randomCoordsToFetchPerRepetition.add(TileCoord.ofXYZ(x, (1 << z) - 1 - y, z));
}
}
}
}
@ -101,14 +121,29 @@ public class BenchmarkMbtilesRead {
}
}
private static boolean isInt(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private static int lineCount(Path tilesPath) {
try (var lines = Files.lines(tilesPath)) {
return (int) lines.count();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static ReadResult readEachTile(List<TileCoord> coordsToFetch, Path dbPath) throws IOException {
try (var db = Mbtiles.newReadOnlyDatabase(dbPath)) {
db.getTile(0, 0, 0); // trigger prepared statement creation
var totalSw = Stopwatch.createStarted();
for (var coordToFetch : coordsToFetch) {
if (db.getTile(coordToFetch) == null) {
throw new IllegalStateException("%s should exist in %s".formatted(coordToFetch, dbPath));
}
db.getTile(coordToFetch);
}
totalSw.stop();
return new ReadResult(totalSw.elapsed(), coordsToFetch.size());