|
| 1 | +/* |
| 2 | + * Licensed to GraphHopper GmbH under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with this work for |
| 4 | + * additional information regarding copyright ownership. |
| 5 | + * |
| 6 | + * GraphHopper GmbH licenses this file to you under the Apache License, |
| 7 | + * Version 2.0 (the "License"); you may not use this file except in |
| 8 | + * compliance with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.graphhopper.tools; |
| 20 | + |
| 21 | +import com.graphhopper.GraphHopper; |
| 22 | +import com.graphhopper.GraphHopperConfig; |
| 23 | +import com.graphhopper.routing.ev.*; |
| 24 | +import com.graphhopper.routing.util.EncodingManager; |
| 25 | +import com.graphhopper.routing.weighting.custom.CustomProfile; |
| 26 | +import com.graphhopper.storage.BaseGraph; |
| 27 | +import com.graphhopper.util.*; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Random; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +public class GraphSpeedMeasurement { |
| 36 | + |
| 37 | + public static void main(String[] strs) { |
| 38 | + PMap args = PMap.read(strs); |
| 39 | + List<String> result = new ArrayList<>(); |
| 40 | + for (int speedBits = 7; speedBits <= 31; speedBits += 3) { |
| 41 | + System.out.println("Running measurement for speedBits=" + speedBits); |
| 42 | + GraphHopperConfig ghConfig = new GraphHopperConfig() |
| 43 | + .putObject("datareader.file", args.getString("map", "map-matching/files/leipzig_germany.osm.pbf")) |
| 44 | + .putObject("graph.location", args.getString("location", "graph-speed-measurement") + "-" + speedBits + "-gh") |
| 45 | + .putObject("graph.dataaccess", args.getString("da", "RAM_STORE")) |
| 46 | + .putObject("import.osm.ignored_highways", "") |
| 47 | + .putObject("graph.vehicles", String.format("roads|speed_bits=%d,car|speed_bits=%d,bike|speed_bits=%d,foot|speed_bits=%d", speedBits, speedBits, speedBits, speedBits)) |
| 48 | + .setProfiles(Arrays.asList( |
| 49 | + new CustomProfile("car").setCustomModel(new CustomModel()).setVehicle("roads") |
| 50 | + )); |
| 51 | + GraphHopper hopper = new GraphHopper() |
| 52 | + .init(ghConfig) |
| 53 | + .importOrLoad(); |
| 54 | + BaseGraph baseGraph = hopper.getBaseGraph(); |
| 55 | + |
| 56 | + EncodingManager em = hopper.getEncodingManager(); |
| 57 | + List<BooleanEncodedValue> booleanEncodedValues = em.getEncodedValues().stream().filter(e -> e instanceof BooleanEncodedValue).map(e -> (BooleanEncodedValue) e).collect(Collectors.toList()); |
| 58 | + List<IntEncodedValue> intEncodedValues = em.getEncodedValues().stream().filter(e -> e.getClass().equals(IntEncodedValueImpl.class)).map(e -> (IntEncodedValue) e).collect(Collectors.toList()); |
| 59 | + List<DecimalEncodedValue> decimalEncodedValues = em.getEncodedValues().stream().filter(e -> e instanceof DecimalEncodedValue).map(e -> (DecimalEncodedValue) e).collect(Collectors.toList()); |
| 60 | + List<EnumEncodedValue> enumEncodedValues = em.getEncodedValues().stream().filter(e -> e.getClass().isAssignableFrom(EnumEncodedValue.class)).map(e -> (EnumEncodedValue) e).collect(Collectors.toList()); |
| 61 | + |
| 62 | + EdgeExplorer explorer = baseGraph.createEdgeExplorer(); |
| 63 | + Random rnd = new Random(123); |
| 64 | + |
| 65 | + final int iterations = args.getInt("iters", 1_000_000); |
| 66 | + // this parameter is quite interesting, because when we do multiple repeats per edge the differences between |
| 67 | + // caching and not caching should become more clear. if we benefited from caching doing multiple repeats should |
| 68 | + // not make much of a difference (thinking naively), while not caching should mean we need to do more work. |
| 69 | + final int repeatsPerEdge = args.getInt("repeats_per_edge", 10); |
| 70 | + MiniPerfTest t = new MiniPerfTest().setIterations(iterations) |
| 71 | + .start((warmup, run) -> { |
| 72 | + EdgeIterator iter = explorer.setBaseNode(rnd.nextInt(baseGraph.getNodes())); |
| 73 | + double sum = 0; |
| 74 | + while (iter.next()) { |
| 75 | + for (int i = 0; i < repeatsPerEdge; i++) { |
| 76 | + // note that reading **all** the EVs should be in favor of the caching solution, while cases |
| 77 | + // with many encoded values where only a selected few are read should make the caching less |
| 78 | + // important. but even in this scenario the caching provides no speedup apparently! |
| 79 | + for (BooleanEncodedValue ev : booleanEncodedValues) sum += iter.get(ev) ? 1 : 0; |
| 80 | + for (IntEncodedValue ev : intEncodedValues) sum += iter.get(ev) > 5 ? 1 : 0; |
| 81 | + for (DecimalEncodedValue ev : decimalEncodedValues) sum += iter.get(ev) > 20 ? 1 : 0; |
| 82 | + for (EnumEncodedValue ev : enumEncodedValues) sum += iter.get(ev).ordinal(); |
| 83 | + } |
| 84 | + } |
| 85 | + return (int) sum; |
| 86 | + }); |
| 87 | + result.add(String.format("bits: %d, ints: %d, took: %.2fms, checksum: %d", speedBits, em.getIntsForFlags(), t.getSum(), t.getDummySum())); |
| 88 | + System.out.println(result.get(result.size() - 1)); |
| 89 | + } |
| 90 | + System.out.println(); |
| 91 | + System.out.println("### RESULT ###"); |
| 92 | + for (String res : result) |
| 93 | + System.out.println(res); |
| 94 | + } |
| 95 | +} |
0 commit comments