Created
April 17, 2019 09:37
-
-
Save karussell/dac80727629cf2a4fedc93c288a3e91f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.graphhopper.matrix.perf.test; | |
import com.graphhopper.api.GHMRequest; | |
import com.graphhopper.api.GHMatrixBatchRequester; | |
import com.graphhopper.api.GHMatrixSyncRequester; | |
import com.graphhopper.api.GraphHopperMatrixWeb; | |
import com.graphhopper.api.MatrixResponse; | |
import com.graphhopper.util.Helper; | |
import com.graphhopper.util.StopWatch; | |
import com.graphhopper.util.shapes.GHPoint; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
/** | |
* | |
* @author peter | |
*/ | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
OkHttpClient okclient = new OkHttpClient.Builder(). | |
connectTimeout(20, TimeUnit.SECONDS). | |
readTimeout(1000, TimeUnit.SECONDS).build(); | |
String pointsStr = ""; | |
boolean ors = false; | |
boolean urlGH = false; | |
String ghKey = "TODO"; | |
// double lat = 50.4, lon = 5.3; | |
// double lat = 48, lon = 10.2; | |
// double lat = 52.46, lon = 13.28; | |
// double lat = 49.62, lon = 7.36; | |
double lat = 52.438014, lon = 13.272858; | |
GraphHopperMatrixWeb matrix = new GraphHopperMatrixWeb( | |
new GHMatrixBatchRequester("http://localhost:8989/matrix"). | |
setMaxIterations(2000). | |
setSleepAfterGET(200) | |
// new GHMatrixSyncRequester("http://localhost:8989/matrix") | |
).setKey(ghKey); | |
GHMRequest request = new GHMRequest().addOutArray("times").addOutArray("distances"); | |
request.setVehicle("car"); | |
request.getHints().put("boost", "true"); | |
int MAX = 520; | |
int mapCounter = 0; | |
for (int i = 0; i < MAX; i++) { | |
if (", 2, ".contains(", " + i + ",")) { | |
System.out.println("skipping " + i + " (" + mapCounter + ") = " + lat + "," + lon); | |
} else if (ors) { | |
mapCounter++; | |
if (i == 0) { | |
pointsStr = "&locations="; | |
} | |
pointsStr += lon + "," + lat + "|"; | |
} else if (urlGH) { | |
pointsStr += "&point=" + Helper.round6(lat) + "," + Helper.round6(lon); | |
} else { | |
mapCounter++; | |
request.addFromPoint(new GHPoint(lat, lon)); | |
request.addToPoint(new GHPoint(lat, lon)); | |
} | |
lat += 0.16 / MAX; | |
lon += 0.27 / MAX; | |
} | |
// this call would force non-rphast | |
// request.addToPoint(new GHPoint(lat, lon)); | |
String baseUrl; | |
if (ors) { | |
// baseUrl = "http://localhost:8080/openrouteservice-4.3.0/matrix?profile=driving-car"; | |
baseUrl = "https://api.openrouteservice.org/matrix?api_key=TODO&profile=driving-car"; | |
} else { | |
baseUrl = "http://localhost:8989/matrix?out_array=distances&key=" + ghKey + "&vehicle=car&boost=true&type=json"; | |
// baseUrl = "http://infra2:8909/matrix?out_array=distances&"; | |
} | |
StopWatch watch = new StopWatch().start(); | |
if (ors || urlGH) { | |
Response rsp = okclient.newCall(new Request.Builder().url(baseUrl + pointsStr).build()).execute(); | |
System.out.println(rsp.code() + " took: " + watch.stop().getSeconds() + " " + rsp.body().bytes().length); | |
// System.out.println(rsp.body().string()); | |
rsp.close(); | |
} else { | |
MatrixResponse rsp = matrix.route(request); | |
if (!rsp.hasErrors()) { | |
System.out.println(watch.stop().getSeconds() + " " + rsp.getDebugInfo()); | |
} else { | |
System.out.println("error " + rsp.getErrors()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.graphhopper.matrix.perf.test; | |
import com.graphhopper.GHRequest; | |
import com.graphhopper.GHResponse; | |
import com.graphhopper.api.GHMRequest; | |
import com.graphhopper.api.GHMatrixBatchRequester; | |
import com.graphhopper.api.GHMatrixSyncRequester; | |
import com.graphhopper.api.GraphHopperMatrixWeb; | |
import static com.graphhopper.api.GraphHopperMatrixWeb.MT_JSON; | |
import com.graphhopper.api.GraphHopperWeb; | |
import com.graphhopper.api.MatrixResponse; | |
import com.graphhopper.util.Helper; | |
import com.graphhopper.util.StopWatch; | |
import com.graphhopper.util.shapes.GHPoint; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.RejectedExecutionException; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicLong; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
// small_truck, middle europe matrix 500x 500 = 100s (for car: 96s) | |
// small_truck, middle europe matrix 1000x1000 = 430s | |
// small_truck, middle europe matrix 1500x1500 = 1380s | |
public class PerfTest { | |
// middle europe | |
// double maxLat = 52.762892, minLon = 5.998535; | |
// double minLat = 48.151428, maxLon = 14.611816; | |
// quebec | |
// double maxLat = 46.879907, minLon = -71.479797; | |
// double minLat = 46.687131, maxLon = -71.07193; | |
// berlin | |
double maxLat = 52.403257, minLon = 13.163681; | |
double minLat = 52.578436, maxLon = 13.583908; | |
double deltaLat = maxLat - minLat, deltaLon = maxLon - minLon; | |
private final Logger logger = LoggerFactory.getLogger(getClass()); | |
private ExecutorService service; | |
private String KEY = "TODO"; | |
public static void main(String[] args) { | |
// new PerfTest().startParallel(3, 3340); | |
new PerfTest().start(); | |
// new PerfTest().startRoutingAPI(); | |
} | |
private GHPoint newPoint(Random rand) { | |
return new GHPoint(rand.nextDouble() * deltaLat + minLat, rand.nextDouble() * deltaLon + minLon); | |
} | |
public void start() { | |
// r1 | |
// GraphHopperMatrixWeb matrixWeb = new GraphHopperMatrixWeb(new GHMatrixBatchRequester(). | |
// setSleepAfterGET(500). | |
// setMaxIterations(10000)); | |
// r2 | |
GraphHopperMatrixWeb matrixWeb = new GraphHopperMatrixWeb( | |
// new GHMatrixBatchRequester("http://localhost:9000/api/1/matrix"). | |
// new GHMatrixBatchRequester("http://localhost:8989/matrix"). | |
new GHMatrixBatchRequester("https://graphhopper.com/api/1/matrix", | |
new OkHttpClient.Builder(). | |
connectTimeout(15, TimeUnit.SECONDS). | |
readTimeout(15, TimeUnit.SECONDS).build()). | |
setSleepAfterGET(500). | |
setMaxIterations(10000)); | |
matrixWeb.setKey(KEY); | |
StopWatch sw = null; | |
int CNT = 1; | |
int POINTS = 2000; | |
long times = 0; | |
// force same points | |
Random rand = new Random(123); | |
GHMRequest req = new GHMRequest(); | |
// | |
// print GET fetching | |
// req.getHints().put("debug", true); | |
req.setVehicle("car"); | |
req.addOutArray("times"); | |
for (int pointIdx = 0; pointIdx < POINTS; pointIdx++) { | |
GHPoint p = newPoint(rand); | |
if (pointIdx == 623 || pointIdx == 623) { | |
// minor workaround to skip unreachable points | |
System.out.println("skipped " + p); | |
p = new GHPoint(52.475253, 13.364182); | |
} | |
req.addPoint(p); | |
} | |
System.out.println("start " + new Date()); | |
for (int requestIdx = 0; requestIdx < CNT + 1; requestIdx++) { | |
// // skip first request | |
if (requestIdx == 1) { | |
sw = new StopWatch().start(); | |
} | |
MatrixResponse rsp = matrixWeb.route(req); | |
System.out.println(requestIdx + ", " + new Date() + " rsp: " + rsp.toString() + ", " + Helper.getMemInfo()); | |
if (rsp.hasErrors()) { | |
throw new RuntimeException("Errors:" + rsp.getErrors(), rsp.getErrors().get(0)); | |
} else if (requestIdx > 0) { | |
times += rsp.getTime(0, 5); | |
} | |
} | |
System.out.println("points:" + POINTS + ", took/req:" + sw.stop().getSeconds() / CNT + "s, avg time/min:" + ((double) times / CNT) / 1000 / 60 | |
+ ", " + Helper.getMemInfo()); | |
} | |
void startRoutingAPI() { | |
GraphHopperWeb web = new GraphHopperWeb(); // "http://localhost:8989/route" | |
web.setDownloader(new OkHttpClient.Builder().connectTimeout(1, TimeUnit.DAYS).readTimeout(1, TimeUnit.DAYS).build()); | |
web.setKey(KEY); | |
GHRequest ghRequest = new GHRequest(); | |
int POINTS = 620; | |
Random rand = new Random(); | |
for (int pointIdx = 0; pointIdx < POINTS; pointIdx++) { | |
GHPoint p = newPoint(rand); | |
if (pointIdx == -1) { | |
// minor workaround to skip unreachable points | |
System.out.println("skipped " + p); | |
p = new GHPoint(52.475253, 13.364182); | |
} | |
ghRequest.addPoint(p); | |
} | |
String str = ""; | |
for (GHPoint p : ghRequest.getPoints()) { | |
str += "&point=" + p.lat + "," + p.lon; | |
} | |
System.out.println("length:" + str.length()); | |
GHResponse rsp = web.route(ghRequest); | |
if (rsp.hasErrors()) { | |
System.out.println(rsp.getErrors()); | |
} else { | |
System.out.println("distance: " + rsp.getBest().getDistance() / 1000 + "km"); | |
} | |
} | |
void testPOSTRequestRoutingAPI() throws IOException { | |
int POINTS = 50; | |
Random rand = new Random(); | |
List<List> points = new ArrayList<List>(); | |
for (int pointIdx = 0; pointIdx < POINTS; pointIdx++) { | |
GHPoint p = newPoint(rand); | |
if (pointIdx == 623 || pointIdx == 623) { | |
// minor workaround to skip unreachable points | |
System.out.println("skipped " + p); | |
p = new GHPoint(52.475253, 13.364182); | |
} | |
points.add(Arrays.asList(Helper.round6(p.lon), Helper.round6(p.lat))); | |
} | |
// json.put("points", new ArrayList<>()); | |
// String url = "https://graphhopper.com/api/1/route?key=TODO"; | |
// Request okRequest = new Request.Builder().url(url).post(RequestBody.create(MT_JSON, json.toString())).build(); | |
// String str = new OkHttpClient().newCall(okRequest).execute().body().string(); | |
// System.out.println(str); | |
} | |
// instead of full matrix try many 1x20 requests to just find neighbor times | |
void startParallel(final int workers, final int requestsPerWorker) { | |
int POINTS = 20; | |
final AtomicLong times = new AtomicLong(); | |
final GHMRequest request = new GHMRequest(); | |
request.setVehicle("small_truck"); | |
request.addOutArray("times"); | |
request.addFromPoint(new GHPoint(52.51831, 13.387184)); | |
Random rand = new Random(123); | |
for (int pointIdx = 0; pointIdx < POINTS; pointIdx++) { | |
GHPoint p = newPoint(rand); | |
if (pointIdx == -1) { | |
// minor workaround to skip unreachable points | |
System.out.println("skipped " + p); | |
p = new GHPoint(52.475253, 13.364182); | |
} | |
request.addToPoint(p); | |
} | |
StopWatch sw = new StopWatch().start(); | |
Collection<Callable<Object>> workerCollection = new ArrayList<Callable<Object>>(workers); | |
for (int i = 0; i < workers; i++) { | |
final int workerNo = i; | |
workerCollection.add(new Callable<Object>() { | |
@Override | |
public Object call() throws Exception { | |
try { | |
for (int i = 0; i < requestsPerWorker; i++) { | |
GraphHopperMatrixWeb matrixWeb = new GraphHopperMatrixWeb( | |
new GHMatrixSyncRequester("http://localhost:8989/matrix")); | |
MatrixResponse rsp = matrixWeb.route(request); | |
if (rsp.hasErrors()) { | |
throw new IllegalStateException("Errors:" + rsp.getErrors()); | |
} else { | |
times.addAndGet(rsp.getTime(0, 5)); | |
} | |
} | |
} catch (Throwable ex) { | |
logger.error("Worker " + workerNo + " died", ex); | |
} | |
return null; | |
} | |
}); | |
} | |
service = Executors.newFixedThreadPool(workers); | |
try { | |
logger.info("Started with " + workers + " workers"); | |
service.invokeAll(workerCollection); | |
} catch (RejectedExecutionException ex) { | |
logger.info("Cannot create threads", ex); | |
} catch (InterruptedException ex) { | |
logger.info("Was interrupted", ex); | |
} finally { | |
service.shutdownNow(); | |
} | |
logger.info("workers:" + workers + ", requests/worker:" + requestsPerWorker + ", total requests:" + requestsPerWorker * workers | |
+ ", matrix: 1x" + POINTS + ", took:" + sw.stop().getSeconds() + "s, " + Helper.getMemInfo()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.graphhopper</groupId> | |
<artifactId>matrix-perf-test</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven.compiler.source>1.7</maven.compiler.source> | |
<maven.compiler.target>1.7</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.graphhopper</groupId> | |
<artifactId>directions-api-client-hc</artifactId> | |
<version>0.11-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>com.graphhopper</groupId> | |
<artifactId>graphhopper-tools</artifactId> | |
<version>0.11-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>com.squareup.okhttp3</groupId> | |
<artifactId>okhttp</artifactId> | |
<version>3.4.2</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment