Skip to content

Commit 1c20680

Browse files
Hendrik Leuschneraoles
authored andcommitted
fix: fix csv reading structure
1 parent c9c62de commit 1c20680

File tree

4 files changed

+187
-186
lines changed

4 files changed

+187
-186
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class BenchmarkEnums {
77

88
// Constant for preference
99
public static final String PREFERENCE = "preference";
10+
public static final String METRICS = "metrics";
11+
public static final String DURATION = "duration";
1012
// Constant for recommended
1113
public static final String RECOMMENDED = "recommended";
1214
public static final String OPTIONS = "options";
@@ -95,7 +97,7 @@ public static MatrixModes fromString(String value) {
9597

9698
public List<String> getProfiles() {
9799
return switch (this) {
98-
case ALGO_DIJKSTRA_MATRIX, ALGO_CORE_MATRIX, ALGO_RPHAST_MATRIX -> List.of("driving-car", "driving-hgv", "cycling-regular", "foot-walking");
100+
case ALGO_DIJKSTRA_MATRIX, ALGO_CORE_MATRIX, ALGO_RPHAST_MATRIX -> List.of("driving-car");//, "driving-hgv", "cycling-regular", "foot-walking");
99101
};
100102
}
101103
/**
@@ -109,11 +111,11 @@ public List<String> getProfiles() {
109111
*/
110112
public Map<String, Object> getRequestParams() {
111113
return switch (this) {
112-
case ALGO_RPHAST_MATRIX -> Map.of(PREFERENCE, RECOMMENDED);
113-
case ALGO_CORE_MATRIX -> Map.of(PREFERENCE,
114-
RECOMMENDED, OPTIONS, Map.of("dynamic_speeds", "true"));
115-
case ALGO_DIJKSTRA_MATRIX -> Map.of(PREFERENCE,
116-
RECOMMENDED, OPTIONS, List.of(Map.of("dynamic_speeds", "false"), Map.of("avoid_features", List.of("ferries"))));
114+
case ALGO_RPHAST_MATRIX -> Map.of(METRICS, List.of(DURATION));
115+
case ALGO_CORE_MATRIX -> Map.of(METRICS,
116+
List.of(DURATION), OPTIONS, Map.of("dynamic_speeds", "true"));
117+
case ALGO_DIJKSTRA_MATRIX -> Map.of(METRICS,
118+
List.of(DURATION), OPTIONS, Map.of("dynamic_speeds", "false", "avoid_features", List.of("ferries")));
117119

118120
};
119121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public List<DirectionsModes> getDirectionsModes() {
156156

157157
public List<MatrixModes> getMatrixModes() {
158158
return matrixModes.isEmpty() ? List.of(ALGO_DIJKSTRA_MATRIX, ALGO_CORE_MATRIX, ALGO_RPHAST_MATRIX)
159-
: directionsModes.stream()
159+
: matrixModes.stream()
160160
.map(MatrixModes::fromString)
161161
.toList();
162162
}

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

Lines changed: 55 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.heigit.ors.benchmark;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
45
import io.gatling.javaapi.core.PopulationBuilder;
56
import io.gatling.javaapi.core.ScenarioBuilder;
67
import io.gatling.javaapi.core.Session;
@@ -11,6 +12,7 @@
1112
import org.slf4j.LoggerFactory;
1213

1314
import java.util.ArrayList;
15+
import java.util.HashMap;
1416
import java.util.List;
1517
import java.util.Map;
1618
import java.util.concurrent.atomic.AtomicInteger;
@@ -116,7 +118,6 @@ private String formatScenarioName(MatrixModes mode, String profile, boolean isPa
116118
*
117119
* @param name descriptive name for the scenario
118120
* @param sourceFile path to CSV file containing test coordinates
119-
* @param config test configuration parameters
120121
* @param mode matrix calculation mode to test
121122
* @param profile routing profile to test
122123
* @return ScenarioBuilder configured for matrix testing
@@ -156,117 +157,78 @@ private static HttpRequestActionBuilder createRequest(String name, MatrixModes m
156157
String profile) {
157158
return http(name)
158159
.post("/v2/matrix/" + profile)
160+
.header("Accept", "application/json;charset=UTF-8")
159161
.body(StringBody(session -> createRequestBody(session, mode)))
160162
.asJson()
161163
.check(status().is(200));
162164
}
163165

164166
/**
165167
* Creates the JSON request body for matrix API calls from CSV session data.
166-
*
168+
*
167169
* @param session Gatling session containing CSV row data
168170
* @param mode matrix calculation mode providing additional parameters
169171
* @return JSON string representation of the request body
170-
* @throws RequestBodyCreationException if JSON serialization fails
172+
* @throws RequestBodyCreationException if JSON serialization fails or data is missing
171173
*/
172174
static String createRequestBody(Session session, MatrixModes mode) {
173175
try {
174-
// Get the data from the CSV row
175-
String coordinatesStr = (String) session.get("coordinates");
176-
String sourcesStr = (String) session.get("sources");
177-
String destinationsStr = (String) session.get("destinations");
178-
179-
Map<String, Object> requestBody = new java.util.HashMap<>(Map.of(
180-
"locations", parseCoordinatesFromString(coordinatesStr),
181-
"sources", parseIntegerArrayFromString(sourcesStr),
182-
"destinations", parseIntegerArrayFromString(destinationsStr)));
183-
184-
requestBody.putAll(mode.getRequestParams());
185-
return objectMapper.writeValueAsString(requestBody);
186-
} catch (JsonProcessingException e) {
187-
throw new RequestBodyCreationException("Failed to create request body", e);
188-
}
189-
}
190-
191-
/**
192-
* Parses coordinate pairs from CSV string format to nested list structure.
193-
*
194-
* Converts strings like "[[8.695556, 49.392701], [8.684623, 49.398284]]"
195-
* into List<List<Double>> format expected by the matrix API.
196-
*
197-
* @param coordinatesStr string representation of coordinate array from CSV
198-
* @return list of coordinate pairs as [longitude, latitude] arrays
199-
* @throws RequestBodyCreationException if parsing fails or format is invalid
200-
*/
201-
static List<List<Double>> parseCoordinatesFromString(String coordinatesStr) {
202-
try {
203-
if (coordinatesStr == null || coordinatesStr.trim().isEmpty()) {
204-
throw new RequestBodyCreationException("Coordinates string is null or empty");
176+
// 1) Retrieve the raw feeder values. Gatling will give us a List<String> for each column.
177+
@SuppressWarnings("unchecked")
178+
List<String> coordsList = (List<String>) session.get("coordinates");
179+
@SuppressWarnings("unchecked")
180+
List<String> sourcesList = (List<String>) session.get("sources");
181+
@SuppressWarnings("unchecked")
182+
List<String> destsList = (List<String>) session.get("destinations");
183+
184+
// 2) Fail fast if any column is missing or empty
185+
if (coordsList == null || coordsList.isEmpty()) {
186+
throw new RequestBodyCreationException("'coordinates' field is missing or empty in session");
205187
}
206-
207-
// Remove quotes if present
208-
String cleaned = coordinatesStr.trim();
209-
if (cleaned.startsWith("\"") && cleaned.endsWith("\"")) {
210-
cleaned = cleaned.substring(1, cleaned.length() - 1);
188+
if (sourcesList == null || sourcesList.isEmpty()) {
189+
throw new RequestBodyCreationException("'sources' field is missing or empty in session");
211190
}
212-
213-
// Remove outer brackets
214-
cleaned = cleaned.substring(2, cleaned.length() - 2);
215-
String[] coordinatePairs = cleaned.split("\\], \\[");
216-
217-
List<List<Double>> locations = new ArrayList<>();
218-
for (String pair : coordinatePairs) {
219-
String[] values = pair.split(", ");
220-
if (values.length != 2) {
221-
throw new RequestBodyCreationException("Invalid coordinate pair: " + pair);
222-
}
223-
double lon = Double.parseDouble(values[0]);
224-
double lat = Double.parseDouble(values[1]);
225-
locations.add(List.of(lon, lat));
226-
}
227-
return locations;
228-
} catch (Exception e) {
229-
throw new RequestBodyCreationException("Failed to parse coordinates: " + coordinatesStr, e);
230-
}
231-
}
232-
233-
/**
234-
* Parses integer arrays from CSV string format.
235-
*
236-
* Converts strings like "[0, 1, 2]" into List<Integer> format
237-
* for sources and destinations parameters.
238-
*
239-
* @param arrayStr string representation of integer array from CSV
240-
* @return list of integers
241-
* @throws RequestBodyCreationException if parsing fails or format is invalid
242-
*/
243-
static List<Integer> parseIntegerArrayFromString(String arrayStr) {
244-
try {
245-
if (arrayStr == null || arrayStr.trim().isEmpty()) {
246-
throw new RequestBodyCreationException("Array string is null or empty");
247-
}
248-
249-
// Remove quotes if present
250-
String cleaned = arrayStr.trim();
251-
if (cleaned.startsWith("\"") && cleaned.endsWith("\"")) {
252-
cleaned = cleaned.substring(1, cleaned.length() - 1);
191+
if (destsList == null || destsList.isEmpty()) {
192+
throw new RequestBodyCreationException("'destinations' field is missing or empty in session");
253193
}
254194

255-
// Remove brackets
256-
cleaned = cleaned.substring(1, cleaned.length() - 1);
195+
// 3) The first element of each List<String> is the actual JSON‐style value.
196+
String coordinatesJson = coordsList.get(0);
197+
String sourcesJson = sourcesList.get(0);
198+
String destsJson = destsList.get(0);
199+
200+
logger.debug(
201+
"Raw CSV values → coordinatesJson: {}, sourcesJson: {}, destsJson: {}",
202+
coordinatesJson, sourcesJson, destsJson
203+
);
204+
205+
// 4) Let Jackson parse "[[lon, lat], [lon, lat], …]" into List<List<Double>>
206+
List<List<Double>> locations = objectMapper.readValue(
207+
coordinatesJson, new TypeReference<List<List<Double>>>() {}
208+
);
209+
210+
// 5) Similarly parse "[0, 1, 2]" into List<Integer>
211+
List<Integer> sources = objectMapper.readValue(
212+
sourcesJson, new TypeReference<List<Integer>>() {}
213+
);
214+
List<Integer> destinations = objectMapper.readValue(
215+
destsJson, new TypeReference<List<Integer>>() {}
216+
);
217+
218+
// 6) Build the request body map and merge in any extra params from MatrixModes
219+
Map<String, Object> requestBody = new HashMap<>(Map.of(
220+
"locations", locations,
221+
"sources", sources,
222+
"destinations", destinations
223+
));
224+
requestBody.putAll(mode.getRequestParams());
257225

258-
if (cleaned.trim().isEmpty()) {
259-
return new ArrayList<>();
260-
}
226+
// 7) Serialize to JSON and return
227+
return objectMapper.writeValueAsString(requestBody);
261228

262-
String[] values = cleaned.split(", ");
263-
List<Integer> result = new ArrayList<>();
264-
for (String value : values) {
265-
result.add(Integer.parseInt(value.trim()));
266-
}
267-
return result;
268-
} catch (Exception e) {
269-
throw new RequestBodyCreationException("Failed to parse integer array: " + arrayStr, e);
229+
} catch (JsonProcessingException e) {
230+
// Jackson failed to parse or serialize
231+
throw new RequestBodyCreationException("Failed to serialize request body to JSON", e);
270232
}
271233
}
272234
}

0 commit comments

Comments
 (0)