Source code: juju/reattore/util/DurationStat.java
1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: DurationStat.java,v 1.6 2003/03/03 05:14:39 michaelh Exp $
20 */
21 package juju.reattore.util;
22
23
24 /** Statistic module that manages durations.
25 */
26 public class DurationStat extends BaseStat {
27 private long count;
28 private long nSamples;
29 private long max = 0;
30 private long min = Long.MAX_VALUE;
31 private long[] running = new long[100];
32 private int runningIdx;
33 private long runningCount;
34
35 /** @see BaseStat */
36 public DurationStat(Class clazz, String leaf) {
37 super(clazz, leaf);
38 }
39
40 /** Start recording a new event for this stat.
41
42 @return A reference to pass into #end
43 */
44 public long start() {
45 return System.currentTimeMillis();
46 }
47
48 /** Commit a event using a reference from #start
49
50 @param ref A reference from #start
51 */
52 public void end(long ref) {
53 long diff = System.currentTimeMillis() - ref;
54
55 count += diff;
56 nSamples++;
57
58 if (diff > max) {
59 max = diff;
60 }
61
62 if (diff < min) {
63 min = diff;
64 }
65
66 runningCount -= running[runningIdx];
67 runningCount += diff;
68 running[runningIdx++] = diff;
69
70 if (runningIdx >= running.length) {
71 runningIdx = 0;
72 }
73 }
74
75 /** Number of hits
76
77 @return Number of hits.
78 */
79 public long getNumSamples() {
80 return nSamples;
81 }
82
83 /** Maximum value.
84
85 @return Maximum value.
86 */
87 public long getMaximum() {
88 return max;
89 }
90
91 /** Minimum value.
92
93 @return Minimum value.
94 */
95 public long getMinimum() {
96 return (nSamples != 0) ? min : 0;
97 }
98
99 /** Average over the whole run.
100
101 @return Average.
102 */
103 public double getAverage() {
104 return (nSamples != 0) ? ((double) count / nSamples) : 0.0;
105 }
106
107 /** Running average over a recent set of samples. Result is
108 undefined if enough samples have not been collected.
109
110 @return The averge.
111 */
112 public double getRunningAverage() {
113 return (double) runningCount / running.length;
114 }
115
116 /** @see BaseStat */
117 protected String getDefaultFormat() {
118 return "{0} Count: {1} Average: {2} Max: {3} Min: {4} Running: {5}";
119 }
120
121 /** @see BaseStat */
122 protected Object[] getFormatArgs() {
123 return new Object[] {
124 getName(), new Long(getNumSamples()), new Double(getAverage()),
125 new Long(getMaximum()), new Long(getMinimum()),
126 new Double(getRunningAverage())
127 };
128 }
129 }