21 package org.matsim.analysis;
23 import org.jfree.chart.axis.CategoryLabelPositions;
28 import java.io.BufferedWriter;
29 import java.io.IOException;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
33 import java.util.Map.Entry;
58 private final Map<Integer, Map<String, Long>>
iterations;
76 private final DateFormat
formatter =
new SimpleDateFormat(
"HH:mm:ss");
81 private final Map<Integer, Map<String, List<String>>>
children;
85 this.iterations =
new LinkedHashMap<>();
86 this.identifiers =
new LinkedList<>();
87 this.operations =
new LinkedList<>();
88 this.currentIterationValues = null;
89 this.children =
new LinkedHashMap<>();
96 this.nextIdentifierPosition = 0;
97 this.nextOperationPosition = 0;
98 this.iteration = null;
99 this.currentIterationValues = null;
100 this.iterations.clear();
101 this.identifiers.clear();
102 this.operations.clear();
103 this.currentMeasuredOperations.clear();
104 this.currentIterationChildren.clear();
105 this.children.clear();
115 if (this.iterations.get(
this.iteration) == null) {
116 this.currentIterationValues =
new HashMap<>();
117 this.iterations.put(this.iteration, this.currentIterationValues);
118 this.nextIdentifierPosition = 0;
119 this.nextOperationPosition = 0;
120 this.currentMeasuredOperations =
new Stack<>();
121 this.currentIterationChildren =
new HashMap<>();
122 this.children.put(this.iteration, this.currentIterationChildren);
134 if (identifier.equals(OPERATION_OTHER)) {
135 throw new RuntimeException(
"Identifier " + OPERATION_OTHER +
" is reserved! Please use another one. Aborting!");
138 String ident =
"BEGIN " + identifier;
140 this.currentIterationValues.put(ident, System.currentTimeMillis());
142 this.currentIterationChildren.put(identifier,
new ArrayList<>());
145 if (!this.currentMeasuredOperations.isEmpty()) {
146 String parent = this.currentMeasuredOperations.peek();
147 this.currentIterationChildren.get(parent).add(identifier);
151 this.currentMeasuredOperations.push(identifier);
161 String ident =
"END " + identifier;
164 this.currentIterationValues.put(ident, System.currentTimeMillis());
167 this.currentMeasuredOperations.pop();
181 this.currentIterationValues.put(identifier, System.currentTimeMillis());
194 writer.write(
"iteration");
195 for (String identifier : this.identifiers) {
196 writer.write(delimiter);
197 writer.write(identifier);
199 writer.write(delimiter);
200 for (String identifier : this.operations) {
201 writer.write(delimiter);
202 writer.write(identifier);
207 for (Map.Entry<Integer, Map<String, Long>> entry :
this.iterations.entrySet()) {
208 Integer iteration = entry.getKey();
209 Map<String, Long> data = entry.getValue();
211 writer.write(iteration.toString());
213 for (String identifier : this.identifiers) {
214 Long time = data.get(identifier);
215 writer.write(delimiter);
219 writer.write(delimiter);
221 for (String identifier: this.operations) {
222 Long startTime = data.get(
"BEGIN " + identifier);
223 Long endTime = data.get(
"END " + identifier);
224 writer.write(delimiter);
225 if (startTime != null && endTime != null) {
226 double diff = (endTime - startTime) / 1000.0;
235 }
catch (IOException e) {
247 int iterations = this.iterations.entrySet().size();
248 Map<String, double[]> arrayMap =
new HashMap<>();
249 for (String identifier : this.operations) arrayMap.put(identifier,
new double[iterations]);
252 for(Entry<Integer, Map<String, Long>> entry : this.iterations.entrySet()) {
253 Map<String, Long> data = entry.getValue();
256 Map<String, List<String>> childrenMap = this.children.get(entry.getKey());
259 for (String identifier : this.operations) {
260 Long startTime = data.get(
"BEGIN " + identifier);
261 Long endTime = data.get(
"END " + identifier);
262 if (startTime != null && endTime != null) {
263 double diff = (endTime - startTime);
269 for (String child : childrenMap.get(identifier)) {
270 Long childStartTime = data.get(
"BEGIN " + child);
271 Long childEndTime = data.get(
"END " + child);
272 diff -= (childEndTime - childStartTime);
275 arrayMap.get(identifier)[iter] = diff / 1000.0;
276 }
else arrayMap.get(identifier)[iter] = 0.0;
281 String title =
"Computation time distribution per iteration";
282 String xAxisLabel =
"iteration";
283 String yAxisLabel =
"seconds";
285 String[] categories =
new String[this.iterations.size()];
287 for (
int iteration : this.iterations.keySet()) {
288 categories[index] = String.valueOf(iteration);
300 chart.
getChart().getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
302 double[] iterationData = null;
303 for (String operation : this.operations) {
304 double[] data = arrayMap.get(operation);
305 if (operation.equals(OPERATION_ITERATION)) {
306 iterationData = data;
311 if (iterationData != null) {
313 System.arraycopy(iterationData, 0, otherData, 0, iterations);
314 chart.
addSeries(OPERATION_OTHER, otherData);
317 chart.
saveAsPng(filename +
".png", 1024, 768);
326 int pos = this.identifiers.indexOf(identifier);
328 this.identifiers.add(this.nextIdentifierPosition, identifier);
329 this.nextIdentifierPosition++;
331 this.nextIdentifierPosition = pos + 1;
339 int pos = this.operations.indexOf(identifier);
341 this.operations.add(this.nextOperationPosition, identifier);
342 this.nextOperationPosition++;
344 this.nextOperationPosition = pos + 1;
356 if (millis == null) {
359 return this.formatter.format(
new Date(millis));
final Map< Integer, Map< String, List< String > > > children
void writeSeparatedFile(final String filename, final String delimiter)
final List< String > operations
void ensureIdentifier(final String identifier)
void endOperation(final String identifier)
void timestamp(final String identifier)
Map< String, Long > currentIterationValues
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
void addSeries(final String title, final double[] values)
final Map< Integer, Map< String, Long > > iterations
Stack< String > currentMeasuredOperations
void ensureOperation(final String identifier)
static final String OPERATION_OTHER
Map< String, List< String > > currentIterationChildren
String formatMilliTime(final Long millis)
static final String writeTime(final double seconds, final String timeformat)
int nextOperationPosition
void saveAsPng(final String filename, final int width, final int height)
static final String OPERATION_ITERATION
void beginOperation(final String identifier)
void beginIteration(final int iteration)
final DateFormat formatter
void writeGraphFile(String filename)
int nextIdentifierPosition
final List< String > identifiers