[ 
https://issues.apache.org/jira/browse/TINKERPOP-1686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16084720#comment-16084720
 ] 

ASF GitHub Bot commented on TINKERPOP-1686:
-------------------------------------------

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());
    }
    ```


> Make TraversalMetrics thread safe
> ---------------------------------
>
>                 Key: TINKERPOP-1686
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP-1686
>             Project: TinkerPop
>          Issue Type: Improvement
>          Components: process
>    Affects Versions: 3.2.4
>            Reporter: stephen mallette
>            Assignee: stephen mallette
>              Labels: breaking
>             Fix For: 3.3.0
>
>
> Ensure that it is possible to write to metrics from multiple threads.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to