Search in sources :

Example 1 with MiniPerfTest

use of com.graphhopper.util.MiniPerfTest in project graphhopper by graphhopper.

the class TrafficChangeWithNodeOrderingReusingTest method runPerformanceTest.

private static void runPerformanceTest(final GraphHopperStorage ghStorage, CHConfig chConfig, long seed, final int iterations) {
    final int numNodes = ghStorage.getNodes();
    RoutingCHGraph chGraph = ghStorage.createCHGraph(ghStorage.createCHStorage(chConfig), chConfig);
    final Random random = new Random(seed);
    LOGGER.info("Running performance test, seed = {}", seed);
    final double[] distAndWeight = { 0.0, 0.0 };
    MiniPerfTest performanceTest = new MiniPerfTest();
    performanceTest.setIterations(iterations).start(new MiniPerfTest.Task() {

        private long queryTime;

        @Override
        public int doCalc(boolean warmup, int run) {
            if (!warmup && run % 1000 == 0) {
                LOGGER.debug("Finished {} of {} runs. {}", run, iterations, run > 0 ? String.format(Locale.ROOT, " Time: %6.2fms", queryTime * 1.e-6 / run) : "");
            }
            if (run == iterations - 1) {
                String avg = fmt(queryTime * 1.e-6 / run);
                LOGGER.debug("Finished all ({}) runs, avg time: {}ms", iterations, avg);
            }
            int from = random.nextInt(numNodes);
            int to = random.nextInt(numNodes);
            long start = nanoTime();
            RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
            Path path = algo.calcPath(from, to);
            if (!warmup && !path.isFound())
                return 1;
            if (!warmup) {
                queryTime += nanoTime() - start;
                double distance = path.getDistance();
                double weight = path.getWeight();
                distAndWeight[0] += distance;
                distAndWeight[1] += weight;
            }
            return 0;
        }
    });
    if (performanceTest.getDummySum() > 0.5 * iterations) {
        throw new IllegalStateException("too many errors, probably something is wrong");
    }
    LOGGER.info("Total distance: {}, total weight: {}", distAndWeight[0], distAndWeight[1]);
    LOGGER.info("Average query time: {}ms", performanceTest.getMean());
}
Also used : PMap(com.graphhopper.util.PMap) MiniPerfTest(com.graphhopper.util.MiniPerfTest) Random(java.util.Random) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)

Example 2 with MiniPerfTest

use of com.graphhopper.util.MiniPerfTest in project graphhopper by graphhopper.

the class CHImportTest method runQueries.

private static void runQueries(GraphHopper hopper, String profile) {
    // Bavaria, but trying to avoid regions that are not covered
    BBox bounds = new BBox(10.508422, 12.326602, 47.713457, 49.940615);
    int numQueries = 10_000;
    long seed = 123;
    Random rnd = new Random(seed);
    AtomicInteger notFoundCount = new AtomicInteger();
    MiniPerfTest test = new MiniPerfTest().setIterations(numQueries).start((warmup, run) -> {
        GHPoint from = getRandomPoint(rnd, bounds);
        GHPoint to = getRandomPoint(rnd, bounds);
        GHRequest req = new GHRequest(from, to).setProfile(profile);
        GHResponse rsp = hopper.route(req);
        if (rsp.hasErrors()) {
            if (rsp.getErrors().stream().anyMatch(t -> !(t instanceof PointNotFoundException || t instanceof ConnectionNotFoundException)))
                throw new IllegalStateException("Unexpected error: " + rsp.getErrors().toString());
            notFoundCount.incrementAndGet();
            return 0;
        } else {
            return (int) rsp.getBest().getRouteWeight();
        }
    });
    System.out.println("Total queries: " + numQueries + ", Failed queries: " + notFoundCount.get());
    System.out.println(test.getReport());
}
Also used : ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BBox(com.graphhopper.util.shapes.BBox) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) MiniPerfTest(com.graphhopper.util.MiniPerfTest)

Aggregations

MiniPerfTest (com.graphhopper.util.MiniPerfTest)2 Random (java.util.Random)2 GHRequest (com.graphhopper.GHRequest)1 GHResponse (com.graphhopper.GHResponse)1 CHRoutingAlgorithmFactory (com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)1 PMap (com.graphhopper.util.PMap)1 ConnectionNotFoundException (com.graphhopper.util.exceptions.ConnectionNotFoundException)1 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)1 BBox (com.graphhopper.util.shapes.BBox)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1