Skip to content

Commit db1beb1

Browse files
author
Hendrik Leuschner
committed
feat: add matrix benchmark enums
1 parent 144093e commit db1beb1

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

ors-benchmark/src/main/java/org/heigit/ors/benchmark/BenchmarkEnums.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,48 @@ public String getValue() {
7373
}
7474
}
7575

76+
/**
77+
* Enum representing different matrix modes for benchmarking.
78+
* getRequestParams() provides the parameters to trigger each matrix algorithm.
79+
*/
80+
public enum MatrixModes {
81+
ALGO_DIJKSTRA_MATRIX,
82+
ALGO_CORE_MATRIX,
83+
ALGO_RPHAST_MATRIX;
84+
85+
public static MatrixModes fromString(String value) {
86+
return switch (value.toLowerCase()) {
87+
case "algodijkstra" -> ALGO_DIJKSTRA_MATRIX;
88+
case "algocore" -> ALGO_CORE_MATRIX;
89+
case "algorphast" -> ALGO_RPHAST_MATRIX;
90+
default -> throw new IllegalArgumentException("Invalid directions mode: " + value);
91+
};
92+
}
93+
94+
public List<String> getProfiles() {
95+
return switch (this) {
96+
case ALGO_DIJKSTRA_MATRIX, ALGO_CORE_MATRIX, ALGO_RPHAST_MATRIX -> List.of("driving-car", "driving-hgv", "cycling-regular", "foot-walking");
97+
};
98+
}
99+
/**
100+
* Returns the request parameters for the matrix algorithm.
101+
* These parameters are used to trigger the specific matrix algorithm.
102+
* This is not great as we have to maintain this in multiple places,
103+
* at the moment this is the only way to trigger the algorithms.
104+
* What would be better is to have a common interface for the algorithms,
105+
* but that would require a larger refactor of the codebase.
106+
* @return a map of request parameters
107+
*/
108+
public Map<String, Object> getRequestParams() {
109+
return switch (this) {
110+
case ALGO_RPHAST_MATRIX -> Map.of(PREFERENCE, RECOMMENDED);
111+
case ALGO_CORE_MATRIX -> Map.of(PREFERENCE,
112+
RECOMMENDED, "options", Map.of("dynamic_speeds", "true"));
113+
case ALGO_DIJKSTRA_MATRIX -> Map.of(PREFERENCE,
114+
RECOMMENDED, "options", List.of(Map.of("dynamic_speeds", "false"), Map.of("avoid_features", List.of("ferries"))));
115+
116+
};
117+
}
118+
}
119+
76120
}

ors-benchmark/src/test/java/org/heigit/ors/benchmark/BenchmarkEnumsTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,42 @@ void testRangeTypeGetValue() {
5252
assertEquals("time", BenchmarkEnums.RangeType.TIME.getValue());
5353
assertEquals("distance", BenchmarkEnums.RangeType.DISTANCE.getValue());
5454
}
55+
56+
@Test
57+
void testMatrixModesFromString() {
58+
assertEquals(BenchmarkEnums.MatrixModes.ALGO_DIJKSTRA_MATRIX, BenchmarkEnums.MatrixModes.fromString("algodijkstra"));
59+
assertEquals(BenchmarkEnums.MatrixModes.ALGO_CORE_MATRIX, BenchmarkEnums.MatrixModes.fromString("algocore"));
60+
assertEquals(BenchmarkEnums.MatrixModes.ALGO_RPHAST_MATRIX, BenchmarkEnums.MatrixModes.fromString("algorphast"));
61+
62+
Throwable exception = assertThrows(IllegalArgumentException.class, () -> BenchmarkEnums.MatrixModes.fromString("invalid"));
63+
assertTrue(exception instanceof IllegalArgumentException);
64+
}
65+
66+
@Test
67+
void testMatrixModesGetDefaultProfiles() {
68+
List<String> dijkstraProfiles = BenchmarkEnums.MatrixModes.ALGO_DIJKSTRA_MATRIX.getProfiles();
69+
assertTrue(dijkstraProfiles.contains("driving-car"));
70+
assertTrue(dijkstraProfiles.contains("foot-walking"));
71+
assertEquals(4, dijkstraProfiles.size());
72+
73+
List<String> coreProfiles = BenchmarkEnums.MatrixModes.ALGO_CORE_MATRIX.getProfiles();
74+
assertTrue(coreProfiles.contains("driving-car"));
75+
assertTrue(coreProfiles.contains("foot-walking"));
76+
assertEquals(4, coreProfiles.size());
77+
}
78+
79+
@Test
80+
void testMatrixModesGetRequestParams() {
81+
Map<String, Object> rphastParams = BenchmarkEnums.MatrixModes.ALGO_RPHAST_MATRIX.getRequestParams();
82+
assertEquals("recommended", rphastParams.get("preference"));
83+
assertEquals(1, rphastParams.size());
84+
85+
Map<String, Object> coreParams = BenchmarkEnums.MatrixModes.ALGO_CORE_MATRIX.getRequestParams();
86+
assertEquals("recommended", coreParams.get("preference"));
87+
assertTrue(coreParams.get("options") instanceof Map);
88+
89+
Map<String, Object> dijkstraParams = BenchmarkEnums.MatrixModes.ALGO_DIJKSTRA_MATRIX.getRequestParams();
90+
assertEquals("recommended", dijkstraParams.get("preference"));
91+
assertTrue(dijkstraParams.get("options") instanceof List);
92+
}
5593
}

0 commit comments

Comments
 (0)