Skip to content

Commit

Permalink
Add option to output measurement result as json
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Oct 9, 2019
1 parent 76d37e3 commit bfdc736
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
10 changes: 10 additions & 0 deletions tools/pom.xml
Expand Up @@ -31,6 +31,16 @@
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand Down
51 changes: 45 additions & 6 deletions tools/src/main/java/com/graphhopper/tools/Measurement.java
Expand Up @@ -17,6 +17,7 @@
*/
package com.graphhopper.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.GraphHopper;
Expand Down Expand Up @@ -67,16 +68,23 @@ public static void main(String[] strs) {
// creates properties file in the format key=value
// Every value is one y-value in a separate diagram with an identical x-value for every Measurement.start call
void start(CmdArgs args) {
String graphLocation = args.get("graph.location", "");
String propLocation = args.get("measurement.location", "");
final String graphLocation = args.get("graph.location", "");
final boolean useJson = args.getBool("measurement.json", false);
boolean cleanGraph = args.getBool("measurement.clean", false);
String summaryLocation = args.get("measurement.summaryfile", "");
String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date());
final String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date());
put("measurement.timestamp", timeStamp);
String propLocation = args.get("measurement.location", "");
if (isEmpty(propLocation)) {
propLocation = "measurement" + timeStamp + ".properties";
if (useJson) {
String gitInfo = Constants.GIT_INFO;
// if we start from IDE or otherwise jar was not built using maven the git commit id will be unknown
String prefix = gitInfo.startsWith("${git.commit.id}") ? "unknown" : gitInfo.split("\\|")[0].substring(0, 8);
propLocation = prefix + "_" + timeStamp + ".json";
} else {
propLocation = "measurement" + timeStamp + ".properties";
}
}

seed = args.getLong("measurement.seed", 123);
put("measurement.gitinfo", args.get("measurement.gitinfo", ""));
int count = args.getInt("measurement.count", 5000);
Expand Down Expand Up @@ -211,7 +219,11 @@ protected DataReader importData() throws IOException {
if (!summaryLocation.trim().isEmpty()) {
writeSummary(summaryLocation, propLocation);
}
storeProperties(graphLocation, propLocation);
if (useJson) {
storeJson(graphLocation, propLocation);
} else {
storeProperties(graphLocation, propLocation);
}
}
}

Expand Down Expand Up @@ -563,6 +575,33 @@ void put(String key, Object val) {
properties.put(key, "" + val);
}

private void storeJson(String graphLocation, String jsonLocation) {
logger.info("storing measurement json in " + jsonLocation);
String gitInfo = properties.get("gh.gitinfo");
if (gitInfo == null) {
logger.error("gitinfo not available, writing properties instead of json");
storeProperties(graphLocation, jsonLocation);
return;
}
properties.remove("gh.gitinfo");
Map<String, String> gitInfoMap = new HashMap<>();
String[] gitInfos = gitInfo.split("\\|");
gitInfoMap.put("commit", gitInfos[0]);
gitInfoMap.put("branch", gitInfos[1]);
gitInfoMap.put("dirty", gitInfos[2].startsWith("${git.commit.id}") ? "true" : gitInfos[2].split("=")[1]);
gitInfoMap.put("time", gitInfos[3]);
Map<String, Object> result = new HashMap<>();
result.put("gitinfo", gitInfoMap);
result.put("metrics", properties);
try {
new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValue(new File(jsonLocation), result);
} catch (IOException e) {
logger.error("Problem while storing json " + graphLocation + ", " + jsonLocation, e);
}
}

private void storeProperties(String graphLocation, String propLocation) {
logger.info("storing measurement properties in " + propLocation);
try (FileWriter fileWriter = new FileWriter(propLocation)) {
Expand Down

0 comments on commit bfdc736

Please sign in to comment.