Coverage report

  %line %branch
net.sf.infrared.agent.LayerTimeTracker
69% 
92% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  * 
 17  
  * 
 18  
  * Original Author:  kamal.govindraj (Tavant Technologies)
 19  
  * Contributor(s):   binil.thomas;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.agent;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 import org.apache.log4j.Logger;
 31  
 
 32  
 import net.sf.infrared.agent.util.MutableInteger;
 33  
 import net.sf.infrared.base.model.ExecutionTimer;
 34  
 import net.sf.infrared.base.model.LayerTime;
 35  
 import net.sf.infrared.base.util.LoggingFactory;
 36  
 
 37  
 /**
 38  
  * 
 39  
  * @author kamal.govindraj
 40  
  * @author binil.thomas
 41  
  */
 42  3
 public class LayerTimeTracker {
 43  2
     private static final Logger log = LoggingFactory.getLogger(LayerTimeTracker.class);
 44  
 
 45  
     private static final String LAYER_SEPARATOR = ".";
 46  
 
 47  
     private static final int LAYER_PATH_INITIAL_SIZE = 256;
 48  
 
 49  
     // Captures the total time taken for execution of APIs in each layer. At the
 50  
     // end of the request, this map is passed over to the statistics object, 
 51  
     // which updates the layer timings in its Map.
 52  
     // Holds String (layer name) -> LayerTime entries
 53  2
     private Map layerTimings = new HashMap();
 54  
 
 55  
     // Determines when the control exits a layer. At this time, the execution
 56  
     // time has to be added to the layer time.
 57  2
     private Map layerCount = new HashMap();
 58  
 
 59  2
     private StringBuffer layerPath = new StringBuffer(LAYER_PATH_INITIAL_SIZE);
 60  
     
 61  2
     private long pruneThreshold = -1;
 62  
 
 63  
     public void enterLayer(ExecutionTimer et) {
 64  0
         String layer = et.getContext().getLayer();
 65  0
         enterLayer(layer);
 66  0
     }
 67  
 
 68  
     public boolean leaveLayer(ExecutionTimer et) {
 69  0
         String layer = et.getContext().getLayer();
 70  0
         return leaveLayer(layer, et.getInclusiveTime());
 71  
     }
 72  
 
 73  
     public Map reset(boolean isFaulty) {
 74  
 //        if (log.isDebugEnabled()) {
 75  
 //            log.debug(this + " - Resetting layer timings");
 76  
 //        }
 77  0
         Map oldLayerTimings = layerTimings;
 78  0
         layerTimings = new HashMap();
 79  0
         if (isFaulty) {
 80  0
             layerCount.clear();
 81  0
             layerPath = new StringBuffer(LAYER_PATH_INITIAL_SIZE);
 82  
         }
 83  
         
 84  0
         return oldLayerTimings;
 85  
     }
 86  
 
 87  
     public Map getLayerTimings() {
 88  2
         return Collections.unmodifiableMap(layerTimings);
 89  
     }
 90  
 
 91  
     public String toString() {
 92  0
         return "LayerTimeTracker for thread " + Thread.currentThread();
 93  
     }
 94  
     
 95  
     public String getCurrentLayer() {
 96  8
         return layerPath.toString().intern();
 97  
     }
 98  
 
 99  
     void enterLayer(String layer) {
 100  9
         MutableInteger currentCount = getLayerCount(layer);
 101  
 
 102  9
         if (currentCount.isZero()) {
 103  8
             addLayerToPath(layer);
 104  
         }
 105  
 
 106  9
         currentCount.increment();
 107  
 
 108  9
         if (log.isDebugEnabled()) {
 109  0
             log.debug(this + " - Entered layer " + layer + " for the " + currentCount + "-th time");
 110  
         }
 111  9
     }
 112  
 
 113  
     boolean leaveLayer(String layer, long time) {
 114  9
         MutableInteger currentCount = getLayerCount(layer);
 115  
         assert currentCount.isPositive(): 
 116  9
             this + " - Mistmatch in Enter/Leave layer. Count should be +ve, was " + currentCount;
 117  
         
 118  
 
 119  9
         currentCount.decrement();
 120  
         /*
 121  
         if (log.isDebugEnabled()) {
 122  
             log.debug(this + " - Leaving layer " + layer + ". " + "We are still " + currentCount
 123  
                     + " deep in this layer");
 124  
         }*/
 125  
 
 126  9
         if (currentCount.isZero()) {
 127  
             // We are leaving the outermost method call from a layer
 128  
             // Need to add the time to the layers time
 129  8
             addTimeToLayer(getCurrentLayer(), time);
 130  8
             removeLayerFromPath();
 131  
         }
 132  
 
 133  9
         return true;
 134  
     }
 135  
 
 136  
     long getPruneBelowTime() {
 137  8
         return pruneThreshold;
 138  
     }
 139  
 
 140  
     void setPruneBelowTime(long time) {
 141  0
         pruneThreshold = time;
 142  0
     }
 143  
     
 144  
     private MutableInteger getLayerCount(String layer) {
 145  18
         MutableInteger count = (MutableInteger) layerCount.get(layer);
 146  
 
 147  18
         if (count == null) {
 148  5
             count = new MutableInteger(0);
 149  5
             layerCount.put(layer, count);
 150  
         }
 151  
 
 152  18
         return count;
 153  
     }
 154  
 
 155  
     private void addLayerToPath(String layer) {
 156  8
         if (layerPath.length() > 0) {
 157  4
             layerPath.append(LAYER_SEPARATOR);
 158  
         }
 159  
 
 160  8
         layerPath.append(layer);
 161  8
     }
 162  
 
 163  
     private void addTimeToLayer(String layer, long executionTime) {
 164  8
         if (executionTime <= getPruneBelowTime()) {
 165  0
             if (log.isDebugEnabled()) {
 166  0
                 log.debug("Discarded tracking of layer " + layer + 
 167  
                         " because the time (" + executionTime + 
 168  
                         ") <= prune threshold (" + getPruneBelowTime() + ")");
 169  
             }
 170  0
             return;
 171  
         }
 172  8
         LayerTime layerTime = (LayerTime) layerTimings.get(layer);
 173  
 
 174  8
         if (layerTime == null) {
 175  7
             layerTime = new LayerTime(layer);
 176  7
             layerTimings.put(layer, layerTime);
 177  
         }
 178  
 
 179  8
         layerTime.addToTime(executionTime);
 180  
 
 181  8
         if (log.isDebugEnabled()) {
 182  0
             log.debug(this + " - Adding " + executionTime + " to layer " + layer);
 183  
         }
 184  8
     }
 185  
 
 186  
     private void removeLayerFromPath() {
 187  8
         if (layerPath.lastIndexOf(LAYER_SEPARATOR) > -1) {
 188  
             // Strip off the last part of the layer name
 189  4
             layerPath.delete(layerPath.lastIndexOf(LAYER_SEPARATOR), layerPath.length());
 190  
         } else {
 191  4
             layerPath.delete(0, layerPath.length());
 192  
         }
 193  8
     }    
 194  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.