Coverage report

  %line %branch
net.sf.infrared.base.model.ApplicationStatistics
49% 
80% 

 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:  binil.thoms (Tavant Technologies)
 19  
  * Contributor(s):   prashant.nair, subin.p
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.base.model;
 23  
 
 24  
 import java.util.Collections;
 25  
 import java.util.Iterator;
 26  
 import java.util.LinkedList;
 27  
 import java.util.List;
 28  
 
 29  
 import net.sf.infrared.base.util.Tree;
 30  
 
 31  
 /**
 32  
  * Represents the statistics collected for an application.
 33  
  * 
 34  
  * <p>
 35  
  * Multiple threads in the application collect the statistics of individual operations, 
 36  
  * and those are merged onto an ApplicationStatistics by the aggregator in use.
 37  
  * 
 38  
  * @author binil.thomas
 39  
  * @author prashant.nair
 40  
  * @author subin.p
 41  
  */
 42  
 public class ApplicationStatistics extends AbstractStatistics {
 43  
     private static final int DEFAULT_MAX_LAST_INVOCATIONS = 5;
 44  
 
 45  
     // aggregated call tree for the entire application
 46  
     private AggregateOperationTree tree;
 47  
      
 48  
     // true when the ApplicationStatistics contains any real statistics
 49  5
     private boolean hasStatistics = false;
 50  
 
 51  
     // number of latest operation trees to store
 52  5
     private int maxLastInvocations = DEFAULT_MAX_LAST_INVOCATIONS;
 53  
 
 54  
     // List of Tree objects. The value of a TreeNode in the tree is an ExecutionTimer object
 55  
     private LinkedList lastInvocationsList;
 56  
     
 57  
     // the data held by this object is from this system time
 58  5
     private long startTime = Long.MAX_VALUE;
 59  
     
 60  
     // the data held by this object is until this system time
 61  5
     private long endTime = Long.MIN_VALUE;
 62  
     
 63  
     private LayerTimeRepository repository;
 64  
 
 65  
     public ApplicationStatistics(String applicationName, String instanceId) {
 66  5
         super(applicationName, instanceId);
 67  5
         reset();
 68  5
     }
 69  
 
 70  
     /**
 71  
      * Resets this application statistics
 72  
      */
 73  
     public synchronized void reset() {
 74  5
         repository = new LayerTimeRepository();
 75  5
         hasStatistics = false;
 76  5
         tree = new AggregateOperationTree();
 77  5
         lastInvocationsList = new LinkedList();
 78  5
         startTime = Long.MAX_VALUE;
 79  5
         endTime = Long.MIN_VALUE;
 80  5
     }
 81  
 
 82  
     /**
 83  
      * Merges the statistics of an operation in this application on this ApplicationStatistics
 84  
      */
 85  
     public synchronized void merge(OperationStatistics opStats) {
 86  4
         if (opStats == null) {
 87  1
             return;
 88  
         }
 89  3
         if ( (! getApplicationName().equals( opStats.getApplicationName() )) 
 90  
                 || (! getInstanceId().equals( opStats.getInstanceId() )) ){
 91  2
             throw new IllegalArgumentException("Incorrect application name or instanceId:" +
 92  
                     " Can't merge " + opStats + " to " + this);
 93  
         }
 94  
         
 95  1
         mergeStartAndEndTimes(opStats);
 96  1
         mergeLayerAndExecutionTimes(opStats);
 97  
         
 98  1
         Tree tree = opStats.getOperationTree();
 99  1
         addToLastInvocations(tree);
 100  1
         mergeTree(tree);
 101  1
     }
 102  
     
 103  
     /**
 104  
      * Merges another ApplicationStatistics onto this on
 105  
      */
 106  
     public synchronized void merge(ApplicationStatistics otherStats) {
 107  0
         if (otherStats == null) {
 108  0
             return;
 109  
         }
 110  0
         if ( (! getApplicationName().equals( otherStats.getApplicationName() )) 
 111  
                 || (! getInstanceId().equals( otherStats.getInstanceId() )) ){
 112  0
             throw new IllegalArgumentException("Incorrect application name or instanceId:" +
 113  
                     " Can't merge " + otherStats + " to " + this);
 114  
         }
 115  
         
 116  0
         mergeStartAndEndTimes(otherStats);
 117  0
         mergeLayerAndExecutionTimes(otherStats);
 118  
         
 119  0
         AggregateOperationTree tree = otherStats.getTree();
 120  0
         mergeTree(tree);
 121  
         
 122  0
         addToLastInvocations(otherStats.getLastInvocations());
 123  0
     }
 124  
 
 125  
     /**
 126  
      * False if this ApplicationStatistics is empty and contains no statistics, else true
 127  
      */
 128  
     public boolean hasStatistics() {
 129  2
         return hasStatistics;
 130  
     }
 131  
 
 132  
     public List getLastInvocations() {
 133  4
         return Collections.unmodifiableList(lastInvocationsList);
 134  
     }
 135  
 
 136  
     public AggregateOperationTree getTree() {
 137  0
         return tree;
 138  
     }
 139  
 
 140  
     public int getMaxLastInvocations() {
 141  0
         return maxLastInvocations;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Sets the number of actual invocation trees stored
 146  
      */
 147  
     public void setMaxLastInvocations(int max) {
 148  1
         maxLastInvocations = max;
 149  1
     }
 150  
     
 151  
     /**
 152  
      * Gets the layers that executed in this application
 153  
      */
 154  
     public String[] getLayers() {
 155  0
         return repository.getHierarchicalLayers();
 156  
     }
 157  
     
 158  
     /**
 159  
      * Gets the time spend in a layer
 160  
      */
 161  
     public long getTimeInLayer(String layer) {
 162  0
         return repository.getTimeInHierarchicalLayer(layer);
 163  
     }
 164  
     
 165  
     /**
 166  
      * Gets the aggregate of executions in that happenend in a given layer
 167  
      */
 168  
     public AggregateExecutionTime[] getExecutionsInLayer(String layer) {
 169  0
         return repository.getExecutionsInHierarchicalLayer(layer);
 170  
     }
 171  
     
 172  
     public String toString() {
 173  2
         return "Application Statistics[app=" + getApplicationName() + ", inst=" + getInstanceId()
 174  
                 + "] from " + startTime + ", until " + endTime;
 175  
     }
 176  
     
 177  
     public long getStartTime() {
 178  0
         return startTime;
 179  
     }
 180  
     
 181  
     public long getEndTime() {
 182  0
         return endTime;
 183  
     }
 184  
 
 185  
     void addToLastInvocations(Tree opTree) {
 186  4
         if (opTree == null) {
 187  1
             return;
 188  
         }
 189  
         
 190  3
         if (lastInvocationsList.size() >= maxLastInvocations) {
 191  1
             lastInvocationsList.removeLast();
 192  
         }
 193  
 
 194  3
         lastInvocationsList.addFirst(opTree);
 195  
         
 196  3
         hasStatistics = true;
 197  3
     }
 198  
     
 199  
     void addToLastInvocations(List trees) {
 200  0
         for (Iterator i = trees.iterator(); i.hasNext();) {
 201  0
             addToLastInvocations((Tree) i.next());
 202  
         }
 203  0
     }
 204  
 
 205  
     void mergeTree(Tree opTree) {
 206  1
         if (opTree == null) {
 207  1
             return;
 208  
         }
 209  
 
 210  0
         this.tree.merge(opTree);
 211  0
         hasStatistics = true;
 212  0
     }
 213  
     
 214  
     void mergeTree(AggregateOperationTree aggTree) {
 215  0
         if (aggTree == null) {
 216  0
             return;
 217  
         }
 218  
 
 219  0
         this.tree.merge(aggTree);
 220  0
         hasStatistics = true;
 221  0
     }
 222  
     
 223  
     void mergeStartAndEndTimes(OperationStatistics stats) {
 224  1
         mergeStartAndEndTimes(stats.getStartTime(), stats.getEndTime());
 225  1
     }
 226  
     
 227  
     void mergeStartAndEndTimes(ApplicationStatistics stats) {
 228  0
         mergeStartAndEndTimes(stats.getStartTime(), stats.getEndTime());
 229  0
     }
 230  
     
 231  
     void mergeStartAndEndTimes(long othersStartTime, class="keyword">long othersEndTime) {
 232  1
         this.startTime = Math.min(class="keyword">this.startTime, othersStartTime);
 233  1
         this.endTime = Math.max(class="keyword">this.endTime, othersEndTime);
 234  1
     }
 235  
     
 236  
     void mergeLayerAndExecutionTimes(OperationStatistics stats) {
 237  1
         String[] layers = stats.getLayers();
 238  1
         for (int i = 0; i < layers.length; i++) {
 239  0
             String aLayer = layers[i];
 240  0
             mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer));
 241  0
             mergeExecutionTimes(aLayer, stats.getExecutions(aLayer));
 242  
         }
 243  1
     }
 244  
     
 245  
     void mergeLayerAndExecutionTimes(ApplicationStatistics stats) {
 246  0
         String[] layers = stats.getLayers();
 247  0
         for (int i = 0; i < layers.length; i++) {
 248  0
             String aLayer = layers[i];
 249  0
             mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer));
 250  0
             mergeExecutionTimes(aLayer, stats.getExecutionsInLayer(aLayer));
 251  
         }
 252  0
     }
 253  
         
 254  
     void mergeLayerTime(String layer, long time) {
 255  0
         repository.mergeHierarchicalLayerTime(layer, time);
 256  0
         hasStatistics = true;
 257  0
     }
 258  
     
 259  
     void mergeExecutionTimes(String layerName, ExecutionTimer[] times) {
 260  0
         repository.mergeExecutionTimes(layerName, times);
 261  0
         hasStatistics = true;
 262  0
     }  
 263  
     
 264  
     void mergeExecutionTimes(String layerName, AggregateExecutionTime[] times) {
 265  0
         repository.mergeExecutionTimes(layerName, times);
 266  0
         hasStatistics = true;
 267  0
     }  
 268  
 }

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