Skip to content

Commit

Permalink
Adds some new functionality to Measurement class. (#1366)
Browse files Browse the repository at this point in the history
* Adds some new functionality to Measurement class.

Signed-off-by: ammagamma <contactammagamma@gmail.com>

* Prevents possible NPE.

Signed-off-by: ammagamma <contactammagamma@gmail.com>

* Adds timestamp and comma as column separator to summary file in Measurement class.

Signed-off-by: ammagamma <contactammagamma@gmail.com>

* Makes use of try-with-resource in Measurement class.

Signed-off-by: ammagamma <contactammagamma@gmail.com>

* improve API key handling for PRs

* minor fix
  • Loading branch information
easbar authored and karussell committed May 17, 2018
1 parent 6b768b6 commit 3bdc49b
Showing 1 changed file with 107 additions and 17 deletions.
124 changes: 107 additions & 17 deletions tools/src/main/java/com/graphhopper/tools/Measurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
Expand All @@ -61,16 +62,24 @@ public class Measurement {
private int maxNode;

public static void main(String[] strs) {
new Measurement().start(CmdArgs.read(strs));
CmdArgs cmdArgs = CmdArgs.read(strs);
int repeats = cmdArgs.getInt("measurement.repeats", 1);
for (int i = 0; i < repeats; ++i)
new Measurement().start(cmdArgs);
}

// 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", "");
if (isEmpty(propLocation))
propLocation = "measurement" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".properties";
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());
put("measurement.timestamp", timeStamp);
if (isEmpty(propLocation)) {
propLocation = "measurement" + timeStamp + ".properties";
}

seed = args.getLong("measurement.seed", 123);
String gitCommit = args.get("measurement.gitinfo", "");
Expand Down Expand Up @@ -101,6 +110,9 @@ protected DataReader importData() throws IOException {

hopper.init(args).
forDesktop();
if (cleanGraph) {
hopper.clean();
}

hopper.getCHFactoryDecorator().setDisablingAllowed(true);
hopper.getLMFactoryDecorator().setDisablingAllowed(true);
Expand Down Expand Up @@ -152,7 +164,6 @@ protected DataReader importData() throws IOException {
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_sod", vehicleStr, true, -1, false);
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_instr", vehicleStr, false, -1, true);
}
logger.info("store into " + propLocation);
} catch (Exception ex) {
logger.error("Problem while measuring " + graphLocation, ex);
put("error", ex.toString());
Expand All @@ -164,12 +175,11 @@ protected DataReader importData() throws IOException {
System.gc();
put("measurement.totalMB", getTotalMB());
put("measurement.usedMB", getUsedMB());
try {
store(new FileWriter(propLocation), "measurement finish, "
+ new Date().toString() + ", " + Constants.BUILD_DATE);
} catch (IOException ex) {
logger.error("Problem while storing properties " + graphLocation + ", " + propLocation, ex);

if (!summaryLocation.trim().isEmpty()) {
writeSummary(summaryLocation, propLocation);
}
storeProperties(graphLocation, propLocation);
}
}

Expand Down Expand Up @@ -500,14 +510,94 @@ void put(String key, Object val) {
properties.put(key, "" + val);
}

private void store(FileWriter fileWriter, String comment) throws IOException {
fileWriter.append("#" + comment + "\n");
for (Entry<String, String> e : properties.entrySet()) {
fileWriter.append(e.getKey());
fileWriter.append("=");
fileWriter.append(e.getValue());
fileWriter.append("\n");
private void storeProperties(String graphLocation, String propLocation) {
logger.info("storing measurement properties in " + propLocation);
try (FileWriter fileWriter = new FileWriter(propLocation)) {
String comment = "measurement finish, " + new Date().toString() + ", " + Constants.BUILD_DATE;
fileWriter.append("#" + comment + "\n");
for (Entry<String, String> e : properties.entrySet()) {
fileWriter.append(e.getKey());
fileWriter.append("=");
fileWriter.append(e.getValue());
fileWriter.append("\n");
}
fileWriter.flush();
} catch (IOException e) {
logger.error("Problem while storing properties " + graphLocation + ", " + propLocation, e);
}
}

/**
* Writes a selection of measurement results to a single line in
* a file. Each run of the measurement class will append a new line.
*/
private void writeSummary(String summaryLocation, String propLocation) {
logger.info("writing summary to " + summaryLocation);
// choose properties that should be in summary here
String[] properties = {
"graph.nodes",
"graph.edges",
"measurement.seed",
CH.PREPARE + "time",
CH.PREPARE + "shortcuts",
"routing.distance_mean",
"routing.mean",
"routing.visited_nodes_mean",
"routingCH.distance_mean",
"routingCH.mean",
"routingCH.visited_nodes_mean",
"measurement.timestamp"
};
File f = new File(summaryLocation);
boolean writeHeader = !f.exists();
try (FileWriter writer = new FileWriter(f, true)) {
if (writeHeader)
writer.write(getSummaryHeader(properties));
writer.write(getSummaryLogLine(properties, propLocation));
} catch (IOException e) {
logger.error("Could not write summary to file '{}'", summaryLocation, e);
}
}

private String getSummaryHeader(String[] properties) {
StringBuilder sb = new StringBuilder("#");
for (String p : properties) {
String columnName = String.format("%" + getSummaryColumnWidth(p) + "s, ", p);
sb.append(columnName);
}
fileWriter.flush();
sb.append("propertyFile");
sb.append('\n');
return sb.toString();
}

private String getSummaryLogLine(String[] properties, String propLocation) {
StringBuilder sb = new StringBuilder(" ");
for (String p : properties) {
sb.append(getFormattedProperty(p));
}
sb.append(propLocation);
sb.append('\n');
return sb.toString();
}

private String getFormattedProperty(String property) {
String result = properties.get(property);
if (result == null) {
result = "missing";
}
// limit number of decimal places for floating point numbers
try {
double doubleValue = Double.parseDouble(result.trim());
if (doubleValue != (long) doubleValue) {
result = String.format("%.2f", doubleValue);
}
} catch (NumberFormatException e) {
// its not a number, never mind
}
return String.format("%" + getSummaryColumnWidth(property) + "s, ", result);
}

private int getSummaryColumnWidth(String p) {
return Math.max(10, p.length());
}
}

0 comments on commit 3bdc49b

Please sign in to comment.