|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
     | 
  |  18 |     | 
     | 
  |  19 |     | 
     | 
  |  20 |     | 
     | 
  |  21 |     | 
     | 
  |  22 |     | 
     | 
  |  23 |     | 
     | 
  |  24 |     | 
     | 
  |  25 |     | 
   package net.sf.jperfprobe;  | 
  |  26 |     | 
     | 
  |  27 |     | 
   import org.slf4j.*;  | 
  |  28 |     | 
     | 
  |  29 |     | 
   import java.io.*;  | 
  |  30 |     | 
   import java.util.*;  | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
   public class Result implements Serializable { | 
  |  43 |    2261 |        final Logger log = LoggerFactory.getLogger(Result.class);  | 
  |  44 |     | 
     | 
  |  45 |     | 
       private int samplesToSkip;  | 
  |  46 |     | 
     | 
  |  47 |     | 
       private int currentSamplesSkip;  | 
  |  48 |     | 
         | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |    1 |        static int DEFAULT_MAXSAMPLES = 300;  | 
  |  52 |     | 
         | 
  |  53 |    2261 |        private long[] samples = new long[DEFAULT_MAXSAMPLES];  | 
  |  54 |     | 
     | 
  |  55 |    2261 |        final transient private Map<String, Probe> probeMap = new HashMap<String, Probe>();  | 
  |  56 |     | 
     | 
  |  57 |     | 
         | 
  |  58 |     | 
       private int nSamples;  | 
  |  59 |     | 
     | 
  |  60 |     | 
         | 
  |  61 |     | 
       private int sampleIndex;  | 
  |  62 |     | 
     | 
  |  63 |     | 
         | 
  |  64 |     | 
       private double max;  | 
  |  65 |     | 
     | 
  |  66 |     | 
         | 
  |  67 |     | 
       private double min;  | 
  |  68 |     | 
     | 
  |  69 |     | 
         | 
  |  70 |     | 
         | 
  |  71 |     | 
       private double average;  | 
  |  72 |     | 
     | 
  |  73 |     | 
         | 
  |  74 |     | 
       private final String name;  | 
  |  75 |     | 
     | 
  |  76 |     | 
       private long lastSample;  | 
  |  77 |     | 
     | 
  |  78 |     | 
       private Probe singleProbe;  | 
  |  79 |     | 
     | 
  |  80 |     | 
       private long total;  | 
  |  81 |     | 
     | 
  |  82 |     | 
       private double squareSum;  | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |    10 |        Result(String name) { | 
  |  86 |    10 |            this.name = name;  | 
  |  87 |    10 |        }  | 
  |  88 |     | 
     | 
  |  89 |    2251 |        Result(int nSkip, String name) { | 
  |  90 |    2251 |            this.samplesToSkip = nSkip;  | 
  |  91 |    2251 |            this.name = name;  | 
  |  92 |    2251 |        }  | 
  |  93 |     | 
     | 
  |  94 |     | 
       public long[] getSamples() { | 
  |  95 |    5 |            long[] dest = new long[sampleIndex];  | 
  |  96 |    5 |            System.arraycopy(samples, 0, dest, 0, sampleIndex);  | 
  |  97 |     | 
     | 
  |  98 |    5 |            return dest;  | 
  |  99 |     | 
       }  | 
  |  100 |     | 
     | 
  |  101 |     | 
       public int getNSamples() { | 
  |  102 |    29 |            return nSamples;  | 
  |  103 |     | 
       }  | 
  |  104 |     | 
     | 
  |  105 |     | 
         | 
  |  106 |     | 
     | 
  |  107 |     | 
     | 
  |  108 |     | 
     | 
  |  109 |     | 
     | 
  |  110 |     | 
     | 
  |  111 |     | 
       public double getMax() { | 
  |  112 |    24 |            return max;  | 
  |  113 |     | 
       }  | 
  |  114 |     | 
     | 
  |  115 |     | 
       public double getMin() { | 
  |  116 |    24 |            return min;  | 
  |  117 |     | 
     | 
  |  118 |     | 
       }  | 
  |  119 |     | 
     | 
  |  120 |     | 
       public double getAverage() { | 
  |  121 |    27 |            return average;  | 
  |  122 |     | 
       }  | 
  |  123 |     | 
     | 
  |  124 |     | 
         | 
  |  125 |     | 
     | 
  |  126 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
     | 
  |  129 |     | 
       public double getStdev() { | 
  |  130 |    3 |            double nMinus1 = (nSamples <= 1) ? 1 : nSamples - 1;  | 
  |  131 |    3 |            double numerator = squareSum - ((total * total) / nSamples);  | 
  |  132 |     | 
     | 
  |  133 |    3 |            return java.lang.Math.sqrt(numerator / nMinus1);  | 
  |  134 |     | 
       }  | 
  |  135 |     | 
     | 
  |  136 |     | 
         | 
  |  137 |     | 
     | 
  |  138 |     | 
     | 
  |  139 |     | 
     | 
  |  140 |     | 
       Probe getProbe() { | 
  |  141 |    13667 |            return probeMap.get(Thread.currentThread().getName());  | 
  |  142 |     | 
       }  | 
  |  143 |     | 
     | 
  |  144 |     | 
     | 
  |  145 |     | 
         | 
  |  146 |     | 
     | 
  |  147 |     | 
     | 
  |  148 |     | 
     | 
  |  149 |     | 
     | 
  |  150 |     | 
       void addprobe(Probe probe) { | 
  |  151 |    2448 |            synchronized (probeMap) { | 
  |  152 |    2448 |                probeMap.put(Thread.currentThread().getName(), probe);  | 
  |  153 |    2448 |            }  | 
  |  154 |    2448 |        }  | 
  |  155 |     | 
     | 
  |  156 |     | 
         | 
  |  157 |     | 
     | 
  |  158 |     | 
     | 
  |  159 |     | 
       public void clear() { | 
  |  160 |    2224 |            probeMap.clear();  | 
  |  161 |    2224 |            max = 0.0;  | 
  |  162 |    2224 |            min = 0.0;  | 
  |  163 |    2224 |            sampleIndex = 0;  | 
  |  164 |    2224 |            nSamples = 0;  | 
  |  165 |    2224 |            average = 0.0;  | 
  |  166 |    2224 |            samples = new long[DEFAULT_MAXSAMPLES];  | 
  |  167 |    2224 |            total = 0;  | 
  |  168 |     | 
     | 
  |  169 |    2224 |        }  | 
  |  170 |     | 
     | 
  |  171 |     | 
       public void disable() { | 
  |  172 |    10 |            for (Probe p : probeMap.values()) { | 
  |  173 |    10 |                p.disable();  | 
  |  174 |     | 
           }  | 
  |  175 |    10 |        }  | 
  |  176 |     | 
     | 
  |  177 |     | 
       public void enable() { | 
  |  178 |    4 |            for (Probe p : probeMap.values()) { | 
  |  179 |    4 |                p.enable();  | 
  |  180 |     | 
           }  | 
  |  181 |    4 |        }  | 
  |  182 |     | 
     | 
  |  183 |     | 
     | 
  |  184 |     | 
       public String getName() { | 
  |  185 |    1 |            return name;  | 
  |  186 |     | 
       }  | 
  |  187 |     | 
     | 
  |  188 |     | 
         | 
  |  189 |     | 
     | 
  |  190 |     | 
     | 
  |  191 |     | 
     | 
  |  192 |     | 
     | 
  |  193 |     | 
     | 
  |  194 |     | 
         | 
  |  195 |     | 
     | 
  |  196 |     | 
     | 
  |  197 |     | 
     | 
  |  198 |     | 
     | 
  |  199 |     | 
       public void addSample(long time) { | 
  |  200 |    110574 |            if (currentSamplesSkip < samplesToSkip) { | 
  |  201 |    3 |                currentSamplesSkip++;  | 
  |  202 |     | 
           } else { | 
  |  203 |    110571 |                lastSample = time;  | 
  |  204 |    110571 |                if (sampleIndex >= DEFAULT_MAXSAMPLES) { | 
  |  205 |     | 
                     | 
  |  206 |    356 |                    sampleIndex = 0;  | 
  |  207 |     | 
               }  | 
  |  208 |     | 
     | 
  |  209 |    110571 |                samples[sampleIndex++] = time;  | 
  |  210 |    110571 |                total += time;  | 
  |  211 |    110571 |                average = (average * (double) nSamples + (double) time) / (double) ++nSamples;  | 
  |  212 |    110571 |                squareSum += time * time;  | 
  |  213 |     | 
     | 
  |  214 |    110571 |                if (time > max) { | 
  |  215 |    101287 |                    max = time;  | 
  |  216 |     | 
               }  | 
  |  217 |    110571 |                if (nSamples > 1) { | 
  |  218 |    108336 |                    if (time < min) { | 
  |  219 |    102 |                        min = time;  | 
  |  220 |     | 
                   }  | 
  |  221 |     | 
               } else { | 
  |  222 |    2235 |                    min = time;  | 
  |  223 |     | 
               }  | 
  |  224 |     | 
           }  | 
  |  225 |    110574 |        }  | 
  |  226 |     | 
     | 
  |  227 |     | 
         | 
  |  228 |     | 
     | 
  |  229 |     | 
     | 
  |  230 |     | 
     | 
  |  231 |     | 
     | 
  |  232 |     | 
       public Probe getSingleProbe() { | 
  |  233 |    9822 |            return singleProbe;  | 
  |  234 |     | 
       }  | 
  |  235 |     | 
     | 
  |  236 |     | 
       public void setSingleProbe(Probe singleProbe) { | 
  |  237 |    4 |            this.singleProbe = singleProbe;  | 
  |  238 |    4 |        }  | 
  |  239 |     | 
     | 
  |  240 |     | 
         | 
  |  241 |     | 
     | 
  |  242 |     | 
     | 
  |  243 |     | 
     | 
  |  244 |     | 
     | 
  |  245 |     | 
       public long getLastSample() { | 
  |  246 |    1 |            return lastSample;  | 
  |  247 |     | 
       }  | 
  |  248 |     | 
     | 
  |  249 |     | 
       public long getTotal() { | 
  |  250 |    15 |            return total;  | 
  |  251 |     | 
       }  | 
  |  252 |     | 
     | 
  |  253 |     | 
         | 
  |  254 |     | 
     | 
  |  255 |     | 
     | 
  |  256 |     | 
     | 
  |  257 |     | 
     | 
  |  258 |     | 
       @Override  | 
  |  259 |     | 
       public String toString() { | 
  |  260 |    2 |            return "probe name=" + name + ", #samples=" + nSamples + " , average=" + average + " , stdev=" + getStdev() + " ,max=" + max + " , min=" + min;  | 
  |  261 |     | 
       }  | 
  |  262 |     | 
   }  |