Github user dkuppitz commented on a diff in the pull request:
https://github.com/apache/tinkerpop/pull/669#discussion_r127075299
--- Diff:
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalMetrics.java
---
@@ -23,52 +23,64 @@
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
import
org.apache.tinkerpop.gremlin.process.traversal.step.util.ProfileStep;
+import org.javatuples.Pair;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
/**
+ * Default implementation for {@link TraversalMetrics} that aggregates
{@link ImmutableMetrics} instances from a
+ * {@link Traversal}.
+ *
* @author Bob Briody (http://bobbriody.com)
* @author Marko A. Rodriguez (http://markorodriguez.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
*/
public final class DefaultTraversalMetrics implements TraversalMetrics,
Serializable {
/**
* toString() specific headers
*/
private static final String[] HEADERS = {"Step", "Count",
"Traversers", "Time (ms)", "% Dur"};
- private final Map<String, MutableMetrics> metrics = new HashMap<>();
- private final TreeMap<Integer, String> indexToLabelMap = new
TreeMap<>();
+ /**
+ * {@link ImmutableMetrics} indexed by their step identifier.
+ */
+ private final Map<String, ImmutableMetrics> stepIndexedMetrics = new
HashMap<>();
- /*
- The following are computed values upon the completion of profiling in
order to report the results back to the user
+ /**
+ * A computed value representing the total time spent on all steps.
*/
private long totalStepDuration;
- private Map<String, ImmutableMetrics> computedMetrics = new
LinkedHashMap<>();
+
+ /**
+ * {@link ImmutableMetrics} indexed by their step position.
+ */
+ private Map<Integer, ImmutableMetrics> positionIndexedMetrics = new
HashMap<>();
+
+ /**
+ * Determines if final metrics have been computed
+ */
+ private volatile boolean finalized = false;
public DefaultTraversalMetrics() {
}
/**
* This is only a convenient constructor needed for GraphSON
deserialization.
*/
- public DefaultTraversalMetrics(final long totalStepDurationNs, final
List<MutableMetrics> metricsMap) {
- this.totalStepDuration = totalStepDurationNs;
- this.computedMetrics = new LinkedHashMap<>(this.metrics.size());
- final AtomicInteger counter = new AtomicInteger(0);
- metricsMap.forEach(metric -> {
- this.computedMetrics.put(metric.getId(),
metric.getImmutableClone());
- this.indexToLabelMap.put(counter.getAndIncrement(),
metric.getId());
- });
+ public DefaultTraversalMetrics(final long totalStepDurationNs, final
List<MutableMetrics> orderedMetrics) {
+ totalStepDuration = totalStepDurationNs;
+ for (int ix = 0; ix < orderedMetrics.size(); ix++) {
--- End diff --
Good that `orderedMetrics.forEach()` was replaced with a simple `for` loop,
but I'm not sure if calling get(ix) 3 times really makes it more efficient.
Maybe:
```
int ix = 0;
for (final MutableMetrics metric : orderedMetrics) {
stepIndexedMetrics.put(metric.getId(), metric.getImmutableClone());
positionIndexedMetrics.put(ix++, metric.getImmutableClone());
}
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---