MATSIM
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
org.matsim.analysis.IterationStopWatch Class Reference

Public Member Functions

 IterationStopWatch ()
 
void reset ()
 
void beginIteration (final int iteration)
 
void beginOperation (final String identifier)
 
void endOperation (final String identifier)
 
void endIteration ()
 
void timestamp (final String identifier)
 
void writeSeparatedFile (final String filename, final String delimiter)
 
void writeGraphFile (String filename)
 

Static Public Attributes

static final String OPERATION_ITERATION = "iteration"
 
static final String OPERATION_OTHER = "other"
 

Private Member Functions

void ensureIdentifier (final String identifier)
 
void ensureOperation (final String identifier)
 
String formatMilliTime (final Long millis)
 

Private Attributes

Integer iteration = null
 
final Map< Integer, Map< String, Long > > iterations
 
final List< String > identifiers
 
final List< String > operations
 
Map< String, Long > currentIterationValues
 
int nextIdentifierPosition = 0
 
int nextOperationPosition = 0
 
final DateFormat formatter = new SimpleDateFormat("HH:mm:ss")
 
Stack< String > currentMeasuredOperations
 
Map< String, List< String > > currentIterationChildren
 
final Map< Integer, Map< String, List< String > > > children
 

Detailed Description

This class provides a mechanism similar to a stop watch, allowing to measure the duration of operations and remembering time stamps. The class collects all the data and provides a simple analysis of the time stamps and durations for operations for each iteration in the simulation. This analysis can be dumped to console or to a file using the write()-methods.

Author
mrieser

Definition at line 43 of file IterationStopWatch.java.

Constructor & Destructor Documentation

◆ IterationStopWatch()

org.matsim.analysis.IterationStopWatch.IterationStopWatch ( )

Creates a new IterationStopWatch.

Definition at line 84 of file IterationStopWatch.java.

84  {
85  this.iterations = new LinkedHashMap<>();
86  this.identifiers = new LinkedList<>();
87  this.operations = new LinkedList<>();
88  this.currentIterationValues = null;
89  this.children = new LinkedHashMap<>();
90  }
final Map< Integer, Map< String, List< String > > > children
final Map< Integer, Map< String, Long > > iterations

Member Function Documentation

◆ reset()

void org.matsim.analysis.IterationStopWatch.reset ( )

Resets the stop watch, deleting all gathered values.

Definition at line 95 of file IterationStopWatch.java.

95  {
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();
106  }
final Map< Integer, Map< String, List< String > > > children
final Map< Integer, Map< String, Long > > iterations
Map< String, List< String > > currentIterationChildren

◆ beginIteration()

void org.matsim.analysis.IterationStopWatch.beginIteration ( final int  iteration)

Sets the current iteration, so that the times measured using beginOperation(String), endOperation(String) and timestamp(String) are assigned to the correct iteration for the analysis.

Definition at line 113 of file IterationStopWatch.java.

References org.matsim.analysis.IterationStopWatch.beginOperation(), and org.matsim.analysis.IterationStopWatch.iteration.

113  {
114  this.iteration = iteration;
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);
123  }
125  }
final Map< Integer, Map< String, List< String > > > children
final Map< Integer, Map< String, Long > > iterations
Map< String, List< String > > currentIterationChildren
void beginOperation(final String identifier)
Here is the call graph for this function:

◆ beginOperation()

void org.matsim.analysis.IterationStopWatch.beginOperation ( final String  identifier)

Tells the stop watch that an operation begins.

Parameters
identifierThe name of the beginning operation.

Definition at line 132 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.beginIteration().

132  {
133 
134  if (identifier.equals(OPERATION_OTHER)) {
135  throw new RuntimeException("Identifier " + OPERATION_OTHER + " is reserved! Please use another one. Aborting!");
136  }
137 
138  String ident = "BEGIN " + identifier;
139  ensureIdentifier(ident);
140  this.currentIterationValues.put(ident, System.currentTimeMillis());
141 
142  this.currentIterationChildren.put(identifier, new ArrayList<>());
143 
144  // check whether this operation has a parent operation
145  if (!this.currentMeasuredOperations.isEmpty()) {
146  String parent = this.currentMeasuredOperations.peek();
147  this.currentIterationChildren.get(parent).add(identifier);
148  }
149 
150  // add ident to stack
151  this.currentMeasuredOperations.push(identifier);
152  }
void ensureIdentifier(final String identifier)
Map< String, List< String > > currentIterationChildren

◆ endOperation()

void org.matsim.analysis.IterationStopWatch.endOperation ( final String  identifier)

Tells the stop watch that an operation ends. The operation must have been started before with beginOperation(String).

Parameters
identifierThe name of the ending operation.

Definition at line 160 of file IterationStopWatch.java.

References org.matsim.analysis.IterationStopWatch.ensureIdentifier(), and org.matsim.analysis.IterationStopWatch.ensureOperation().

Referenced by org.matsim.analysis.IterationStopWatch.endIteration().

160  {
161  String ident = "END " + identifier;
162  ensureIdentifier(ident);
163  ensureOperation(identifier);
164  this.currentIterationValues.put(ident, System.currentTimeMillis());
165 
166 
167  this.currentMeasuredOperations.pop();
168  }
void ensureIdentifier(final String identifier)
void ensureOperation(final String identifier)
Here is the call graph for this function:

◆ endIteration()

void org.matsim.analysis.IterationStopWatch.endIteration ( )

Definition at line 170 of file IterationStopWatch.java.

References org.matsim.analysis.IterationStopWatch.endOperation().

170  {
172  }
void endOperation(final String identifier)
Here is the call graph for this function:

◆ timestamp()

void org.matsim.analysis.IterationStopWatch.timestamp ( final String  identifier)

Tells the stop watch that a special event happened, for which the time should be remembered.

Parameters
identifierThe name of the event.

Definition at line 179 of file IterationStopWatch.java.

References org.matsim.analysis.IterationStopWatch.ensureIdentifier().

179  {
180  ensureIdentifier(identifier);
181  this.currentIterationValues.put(identifier, System.currentTimeMillis());
182  }
void ensureIdentifier(final String identifier)
Here is the call graph for this function:

◆ writeSeparatedFile()

void org.matsim.analysis.IterationStopWatch.writeSeparatedFile ( final String  filename,
final String  delimiter 
)

Writes the gathered data into a file.

Parameters
filenameThe name of a file where to write the gathered data.
delimiterThe delimiter to be used as field separator.

Definition at line 190 of file IterationStopWatch.java.

References org.matsim.analysis.IterationStopWatch.formatMilliTime(), org.matsim.core.utils.io.IOUtils.getBufferedWriter(), and org.matsim.core.utils.misc.Time.writeTime().

190  {
191  try (BufferedWriter writer = IOUtils.getBufferedWriter(filename)) {
192 
193  // print header
194  writer.write("iteration");
195  for (String identifier : this.identifiers) {
196  writer.write(delimiter);
197  writer.write(identifier);
198  }
199  writer.write(delimiter);
200  for (String identifier : this.operations) {
201  writer.write(delimiter);
202  writer.write(identifier);
203  }
204  writer.write('\n');
205 
206  // print data
207  for (Map.Entry<Integer, Map<String, Long>> entry : this.iterations.entrySet()) {
208  Integer iteration = entry.getKey();
209  Map<String, Long> data = entry.getValue();
210  // iteration
211  writer.write(iteration.toString());
212  // identifiers
213  for (String identifier : this.identifiers) {
214  Long time = data.get(identifier);
215  writer.write(delimiter);
216  writer.write(formatMilliTime(time));
217  }
218  // blank separator
219  writer.write(delimiter);
220  // durations of operations
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;
227  writer.write(Time.writeTime(diff));
228  }
229  }
230 
231  // finish
232  writer.write("\n");
233  }
234  writer.flush();
235  } catch (IOException e) {
236  throw new RuntimeException(e);
237  }
238  }
Here is the call graph for this function:

◆ writeGraphFile()

void org.matsim.analysis.IterationStopWatch.writeGraphFile ( String  filename)

Writes the gathered data as graph into a png file.

Parameters
filenameThe name of a file where to write the gathered data.

Definition at line 245 of file IterationStopWatch.java.

References org.matsim.core.utils.charts.ChartUtil.addMatsimLogo(), org.matsim.core.utils.charts.StackedBarChart.addSeries(), org.matsim.core.utils.charts.StackedBarChart.getChart(), org.matsim.analysis.IterationStopWatch.iterations, and org.matsim.core.utils.charts.ChartUtil.saveAsPng().

245  {
246 
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]);
250 
251  int iter = 0;
252  for(Entry<Integer, Map<String, Long>> entry : this.iterations.entrySet()) {
253  Map<String, Long> data = entry.getValue();
254 
255  // children map of current iteration
256  Map<String, List<String>> childrenMap = this.children.get(entry.getKey());
257 
258  // durations of operations
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);
264 
265  /*
266  * If the operation has children, subtract their durations since they are
267  * also included in the plot. Otherwise, their duration would be counted twice.
268  */
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);
273  }
274 
275  arrayMap.get(identifier)[iter] = diff / 1000.0;
276  } else arrayMap.get(identifier)[iter] = 0.0;
277  }
278  iter++;
279  }
280 
281  String title = "Computation time distribution per iteration";
282  String xAxisLabel = "iteration";
283  String yAxisLabel = "seconds";
284 
285  String[] categories = new String[this.iterations.size()];
286  int index = 0;
287  for (int iteration : this.iterations.keySet()) {
288  categories[index] = String.valueOf(iteration);
289  index++;
290  }
291 
292  StackedBarChart chart = new StackedBarChart(title, xAxisLabel, yAxisLabel, categories);
293  chart.addMatsimLogo();
294 
295  /*
296  * Rotate x-axis labels by 90° which should allow more of them to be plotted before overlapping.
297  * However, a more general solution that also is able to skip labels would be nice.
298  * cdobler nov'13
299  */
300  chart.getChart().getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
301 
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;
307  } else {
308  chart.addSeries(operation, data);
309  }
310  }
311  if (iterationData != null) {
312  double[] otherData = new double[iterations];
313  System.arraycopy(iterationData, 0, otherData, 0, iterations);
314  chart.addSeries(OPERATION_OTHER, otherData);
315  }
316 
317  chart.saveAsPng(filename + ".png", 1024, 768);
318  }
final Map< Integer, Map< String, List< String > > > children
final Map< Integer, Map< String, Long > > iterations
Here is the call graph for this function:

◆ ensureIdentifier()

void org.matsim.analysis.IterationStopWatch.ensureIdentifier ( final String  identifier)
private

Make sure the given identifier exists in our collection. If it is missing, insert it at the correct place. "Correct" means that it tries to insert this identifier right after the last-requested identifier. This should help to write the gathered data out in a natural way.

Definition at line 325 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.endOperation(), and org.matsim.analysis.IterationStopWatch.timestamp().

325  {
326  int pos = this.identifiers.indexOf(identifier);
327  if (pos == -1) {
328  this.identifiers.add(this.nextIdentifierPosition, identifier);
329  this.nextIdentifierPosition++;
330  } else {
331  this.nextIdentifierPosition = pos + 1;
332  }
333  }

◆ ensureOperation()

void org.matsim.analysis.IterationStopWatch.ensureOperation ( final String  identifier)
private

Does the same as ensureIdentifier(String), but for operation-identifiers.

Definition at line 338 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.endOperation().

338  {
339  int pos = this.operations.indexOf(identifier);
340  if (pos == -1) {
341  this.operations.add(this.nextOperationPosition, identifier);
342  this.nextOperationPosition++;
343  } else {
344  this.nextOperationPosition = pos + 1;
345  }
346  }

◆ formatMilliTime()

String org.matsim.analysis.IterationStopWatch.formatMilliTime ( final Long  millis)
private

Formats the time given in milliseconds (e.g. returned by java.lang.System#currentTimeMillis()) nicely for output.

Parameters
millisA time value in milliseconds
Returns
A String containing the formatted time, or an empty String if millis is null

Definition at line 355 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.writeSeparatedFile().

355  {
356  if (millis == null) {
357  return "";
358  }
359  return this.formatter.format(new Date(millis));
360  }

Member Data Documentation

◆ OPERATION_ITERATION

final String org.matsim.analysis.IterationStopWatch.OPERATION_ITERATION = "iteration"
static

Strings used to identify the operations in the IterationStopWatch.

Definition at line 47 of file IterationStopWatch.java.

◆ OPERATION_OTHER

final String org.matsim.analysis.IterationStopWatch.OPERATION_OTHER = "other"
static

Definition at line 52 of file IterationStopWatch.java.

◆ iteration

Integer org.matsim.analysis.IterationStopWatch.iteration = null
private

The current iteration number, or null if not yet initialized.

Definition at line 55 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.beginIteration().

◆ iterations

final Map<Integer, Map<String, Long> > org.matsim.analysis.IterationStopWatch.iterations
private

The main collection, where all the gathered data is stored.

Definition at line 58 of file IterationStopWatch.java.

Referenced by org.matsim.analysis.IterationStopWatch.writeGraphFile().

◆ identifiers

final List<String> org.matsim.analysis.IterationStopWatch.identifiers
private

A list of identifiers used to enumerate time stamps.

Definition at line 61 of file IterationStopWatch.java.

◆ operations

final List<String> org.matsim.analysis.IterationStopWatch.operations
private

A list of identifiers used to enumerate operations.

Definition at line 64 of file IterationStopWatch.java.

◆ currentIterationValues

Map<String, Long> org.matsim.analysis.IterationStopWatch.currentIterationValues
private

A cache for easy access to the current object in iterations.

Definition at line 67 of file IterationStopWatch.java.

◆ nextIdentifierPosition

int org.matsim.analysis.IterationStopWatch.nextIdentifierPosition = 0
private

The position within identifiers, where the next identifier is expected.

Definition at line 70 of file IterationStopWatch.java.

◆ nextOperationPosition

int org.matsim.analysis.IterationStopWatch.nextOperationPosition = 0
private

The position within operations, where the next identifier is expected.

Definition at line 73 of file IterationStopWatch.java.

◆ formatter

final DateFormat org.matsim.analysis.IterationStopWatch.formatter = new SimpleDateFormat("HH:mm:ss")
private

A formatter for dates, used when writing out the data.

Definition at line 76 of file IterationStopWatch.java.

◆ currentMeasuredOperations

Stack<String> org.matsim.analysis.IterationStopWatch.currentMeasuredOperations
private

data structures to identify nested operations

Definition at line 79 of file IterationStopWatch.java.

◆ currentIterationChildren

Map<String, List<String> > org.matsim.analysis.IterationStopWatch.currentIterationChildren
private

Definition at line 80 of file IterationStopWatch.java.

◆ children

final Map<Integer, Map<String, List<String> > > org.matsim.analysis.IterationStopWatch.children
private

Definition at line 81 of file IterationStopWatch.java.


The documentation for this class was generated from the following file: