View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.management.stats;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleSession;
11  import org.mule.api.construct.FlowConstruct;
12  
13  import java.io.Serializable;
14  import java.util.concurrent.atomic.AtomicLong;
15  
16  /**
17   * Accumulates the processing time for all branches of a flow
18   */
19  public class ProcessingTime implements Serializable
20  {
21  
22      /**
23       * Serial version
24       */
25      private static final long serialVersionUID = 1L;
26  
27      private AtomicLong accumulator = new AtomicLong();
28      private FlowConstructStatistics statistics;
29  
30      /**
31       * Create a ProcessingTime for the specified MuleSession.
32       *
33       * @return ProcessingTime if the session has an enabled FlowConstructStatistics or null otherwise
34       */
35      public static ProcessingTime newInstance(MuleSession session, MuleContext muleContext)
36      {
37          if (session != null)
38          {
39              FlowConstruct fc = session.getFlowConstruct();
40              if (fc != null)
41              {
42                  FlowConstructStatistics stats = fc.getStatistics();
43                  if (stats != null && fc.getStatistics().isEnabled())
44                  {
45                      return new ProcessingTime(stats, muleContext);
46                  }
47              }
48  
49          }
50  
51          return null;
52      }
53  
54      /**
55       * Create a Processing Time
56       *
57       * @param stats       never null
58       * @param muleContext
59       */
60      private ProcessingTime(FlowConstructStatistics stats, MuleContext muleContext)
61      {
62          this.statistics = stats;
63          ProcessingTimeWatcher processorTimeWatcher = muleContext.getProcessorTimeWatcher();
64          processorTimeWatcher.addProcessingTime(this);
65      }
66  
67      /**
68       * Add the execution time for this branch to the flow construct's statistics
69       *
70       * @param startTime time this branch started
71       */
72      public void addFlowExecutionBranchTime(long startTime)
73      {
74          if (statistics.isEnabled())
75          {
76              long elapsedTime = getEffectiveTime(System.currentTimeMillis() - startTime);
77              statistics.addFlowExecutionBranchTime(elapsedTime, accumulator.addAndGet(elapsedTime));
78          }
79      }
80  
81      /**
82       * Convert processing time to effective processing time.  If processing took less than a tick, we consider
83       * it to have been one millisecond
84       */
85      public static long getEffectiveTime(long time)
86      {
87          return (time <= 0) ? 1L : time;
88      }
89  
90      public FlowConstructStatistics getStatistics()
91      {
92          return statistics;
93      }
94  
95      public AtomicLong getAccumulator()
96      {
97          return accumulator;
98      }
99  }