Coverage report

  %line %branch
net.sf.infrared.base.model.StatisticsSnapshot
26% 
81% 

 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.thomas (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.base.model;
 23  
 
 24  
 import java.io.Serializable;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collection;
 27  
 import java.util.Collections;
 28  
 import java.util.HashMap;
 29  
 import java.util.HashSet;
 30  
 import java.util.Iterator;
 31  
 import java.util.LinkedList;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 import java.util.Map.Entry;
 35  
 import java.util.Set;
 36  
 
 37  
 /**
 38  
  * Holds the performance statistics of multiple applications
 39  
  *
 40  
  * @author binil.thomas
 41  
  */
 42  1
 public class StatisticsSnapshot implements Serializable, Cloneable {    
 43  
     private static final int DEFAULT_MAX_LAST_INVOCATIONS = 5;
 44  
     
 45  
     private static final String SEPERATOR = " - on - ";
 46  
     
 47  
     // set of application instances for which the stats is held in this object.
 48  
     // each element would be of the form 'application - on - host'
 49  1
     private Set applicationNames = new HashSet();
 50  
 
 51  
     // aggregated call tree for all applications
 52  1
     private AggregateOperationTree tree = new AggregateOperationTree();
 53  
  
 54  1
     private LayerTimeRepository repository = new LayerTimeRepository();
 55  
 
 56  
     // true when the PerformanceStatistics contains any real statistics
 57  1
     private boolean hasStatistics = false;
 58  
 
 59  
     // number of latest operation trees to store
 60  1
     private int maxLastInvocations = DEFAULT_MAX_LAST_INVOCATIONS;
 61  
 
 62  
     // List of Tree objects. The value of a TreeNode in the tree is an ExecutionTimer object
 63  1
     private LinkedList lastInvocationsList = new LinkedList();
 64  
     
 65  
     // the data held by this object is from this system time
 66  1
     private long startTime = Long.MAX_VALUE;
 67  
     
 68  
     // the data held by this object is until this system time
 69  1
     private long endTime = Long.MIN_VALUE;
 70  
     
 71  
     
 72  
 //    public Object clone() {
 73  
 //        StatisticsSnapshot clone = new StatisticsSnapshot();
 74  
 //        clone.applicationNames = new HashSet(this.applicationNames);
 75  
 //        clone.repository = (LayerTimeRepository) this.repository.clone();
 76  
 //        clone.tree = this.tree; // shallow
 77  
 //        clone.hasStatistics = this.hasStatistics;
 78  
 //        clone.maxLastInvocations = this.maxLastInvocations;
 79  
 //        clone.lastInvocationsList = new LinkedList(this.lastInvocationsList);
 80  
 //        clone.startTime = this.startTime;
 81  
 //        clone.endTime = this.endTime;
 82  
 //        return clone;
 83  
 //    }
 84  
        
 85  
     public synchronized void merge(ApplicationStatistics stats) {
 86  0
         if (stats == null) {
 87  0
             return;
 88  
         }
 89  0
         applicationNames.add(getApplicationName(stats));
 90  
         
 91  0
         mergeStartAndEndTimes(stats);
 92  0
         mergeLayerAndExecutionTimes(stats);
 93  
         
 94  0
         AggregateOperationTree tree = stats.getTree();
 95  0
         mergeTree(tree);        
 96  0
         addToLastInvocations(stats.getLastInvocations());        
 97  0
     }
 98  
     
 99  
     public List getLastInvocations() {
 100  4
         return Collections.unmodifiableList(lastInvocationsList);
 101  
     }
 102  
     
 103  
     /**
 104  
      * Sets the number of actual invocation trees stored
 105  
      */
 106  
     public void setMaxLastInvocations(int max) {
 107  1
         maxLastInvocations = max;
 108  1
     }
 109  
 
 110  
     public AggregateOperationTree getTree() {
 111  0
         return tree;
 112  
     }
 113  
     
 114  
     public long getStartTime() {
 115  0
         return startTime;
 116  
     }
 117  
     
 118  
     public long getEndTime() {
 119  0
         return endTime;
 120  
     }
 121  
         
 122  
     public String[] getHierarchicalLayers() {
 123  0
         return repository.getHierarchicalLayers();
 124  
     }
 125  
     
 126  
     public String[] getAbsoluteLayers() {
 127  0
         return repository.getAbsoluteLayers();
 128  
     }
 129  
     
 130  
     public long getTimeInHierarchicalLayer(String hLayer) {
 131  0
         return repository.getTimeInHierarchicalLayer(hLayer);
 132  
     }
 133  
     
 134  
     public long getTimeInAbsoluteLayer(String aLayer) {
 135  0
         return repository.getTimeInAbsoluteLayer(aLayer);
 136  
     }
 137  
     
 138  
     public AggregateExecutionTime[] getExecutionsInHierarchicalLayer(String hLayer) {
 139  0
         return repository.getExecutionsInHierarchicalLayer(hLayer);
 140  
     }
 141  
     
 142  
     public AggregateExecutionTime[] getExecutionsInAbsoluteLayer(String aLayer) {
 143  0
         return repository.getExecutionsInAbsoluteLayer(aLayer);
 144  
     }
 145  
 //    
 146  
 //    /**
 147  
 //     * Gets the layers that executed in this application
 148  
 //     */
 149  
 //    public String[] getLayers() {
 150  
 //        throw new UnsupportedOperationException("Names have been changed");
 151  
 //    }
 152  
 //    
 153  
 //    /**
 154  
 //     * Gets the time spend in a layer
 155  
 //     */
 156  
 //    public long getTimeInLayer(String layer) {
 157  
 //        throw new UnsupportedOperationException();
 158  
 //    }
 159  
     
 160  
     public Set getApplicationNames() {
 161  0
         return Collections.unmodifiableSet(applicationNames);
 162  
     }
 163  
     
 164  
 //    /**
 165  
 //     * Gets the aggregate of executions in that happenend in a given layer
 166  
 //     */
 167  
 //    public AggregateExecutionTime[] getExecutionsInLayer(String layer) {
 168  
 //        return repository.getExecutionsInHierarchicalLayer(layer);
 169  
 //    }
 170  
     
 171  
     public static StatisticsSnapshot createSnapshot(Collection appNames, 
 172  
             Collection instanceIds, Map layerTimes, Map executionTimes, AggregateOperationTree tree, 
 173  
             long startTime, class="keyword">long endTime) {
 174  0
         StatisticsSnapshot stats = new StatisticsSnapshot();
 175  0
         for (Iterator i = layerTimes.entrySet().iterator(); i.hasNext();) {
 176  0
             Map.Entry entry = (Map.Entry) i.next();
 177  0
             String layer = (String) entry.getKey();
 178  0
             LayerTime lt = (LayerTime) entry.getValue();
 179  0
             stats.mergeLayerTime(layer, lt.getTime());
 180  
         }
 181  0
         for (Iterator i = executionTimes.entrySet().iterator(); i.hasNext();) {
 182  0
             Map.Entry entry = (Map.Entry) i.next();
 183  0
             String layer = (String) entry.getKey();
 184  0
             List executions = (List) entry.getValue();
 185  0
             stats.mergeExecutionTimes(layer, 
 186  
                     (AggregateExecutionTime[]) executions.toArray(new AggregateExecutionTime[0]));
 187  
         }
 188  0
         stats.tree = tree;
 189  0
         stats.startTime = startTime;
 190  0
         stats.endTime = endTime;
 191  0
         stats.applicationNames = new HashSet(appNames);
 192  0
         return stats;
 193  
     }
 194  
     
 195  
     String getApplicationName(ApplicationStatistics stats) {
 196  0
         return stats.getApplicationName() + SEPERATOR + stats.getInstanceId();
 197  
     }
 198  
     
 199  
     void mergeTree(AggregateOperationTree tree) {
 200  0
         if (tree != null) {
 201  0
             this.tree.merge(tree);
 202  0
             hasStatistics = true;
 203  
         }
 204  0
     }
 205  
     
 206  
     void mergeLayerAndExecutionTimes(ApplicationStatistics stats) {
 207  0
         String[] layers = stats.getLayers();
 208  0
         for (int i = 0; i < layers.length; i++) {
 209  0
             String aLayer = layers[i];
 210  0
             mergeLayerTime(aLayer, stats.getTimeInLayer(aLayer));
 211  0
             mergeExecutionTimes(aLayer, stats.getExecutionsInLayer(aLayer));
 212  
         }
 213  0
     }
 214  
     
 215  
     void mergeLayerTime(String layerName, long time) {        
 216  0
         repository.mergeHierarchicalLayerTime(layerName, time);
 217  0
         hasStatistics = true;
 218  0
     }
 219  
     
 220  
     void mergeExecutionTimes(String layerName, AggregateExecutionTime[] times) {
 221  0
         repository.mergeExecutionTimes(layerName, times);
 222  0
         hasStatistics = true;        
 223  0
     }
 224  
         
 225  
     // @TODO: improve considering the clocks & time zones of the agents
 226  
     void mergeStartAndEndTimes(ApplicationStatistics stats) {
 227  0
         startTime = Math.min(startTime, stats.getStartTime());
 228  0
         endTime = Math.max(endTime, stats.getEndTime());
 229  0
     }
 230  
     
 231  
     // @TODO: 'last' here is definied as that merged later, which might not be correct
 232  
     void addToLastInvocations(List trees) {
 233  3
         if ( (trees == null) || (trees.isEmpty()) ) {
 234  0
             return;
 235  
         }
 236  
         
 237  3
         lastInvocationsList.addAll(trees);
 238  3
         int size = lastInvocationsList.size();
 239  
         
 240  3
         if (size > maxLastInvocations) {
 241  4
             for (int i = size; i > maxLastInvocations; i--) {
 242  3
                 lastInvocationsList.removeFirst();
 243  
             }
 244  
         }
 245  
                 
 246  3
         hasStatistics = true;
 247  3
     }
 248  
 }

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