Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: juju/reattore/util/RateStat.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: RateStat.java,v 1.5 2003/03/03 05:14:39 michaelh Exp $
20  */
21  package juju.reattore.util;
22  
23  
24  /** Statistic module that manages a rate of events.
25   */
26  public class RateStat extends BaseStat {
27      private long count;
28      private long total;
29      private long lastUpdate;
30  
31      /** @see BaseStat */
32      public RateStat(Class clazz, String leaf) {
33          super(clazz, leaf);
34      }
35  
36      /** Get the total number of samples added.
37  
38          @return The number of samples.
39      */
40      public long getCount() {
41          return count;
42      }
43  
44      /** Get the number of hits per second since this method was last
45          called.
46  
47          @return Hits per second.
48      */
49      public double getRunningRate() {
50          long now = System.currentTimeMillis();
51  
52          try {
53              return ((double) total * 1000) / (now - lastUpdate);
54          }
55          finally {
56              total = 0;
57              lastUpdate = now;
58          }
59      }
60  
61      /** Cause a single hit on this stat.
62       */
63      public void inc() {
64          count++;
65          total++;
66      }
67  
68      /** Adds a number of hits to this stat.
69  
70          @param count  The number of hits to add.
71       */
72      public void add(int count) {
73          total += count;
74          this.count++;
75      }
76  
77      /** @see BaseStat */
78      protected String getDefaultFormat() {
79          return "{0} Count: {1} Running Rate: {2}/s";
80      }
81  
82      /** @see BaseStat */
83      protected Object[] getFormatArgs() {
84          return new Object[] { getName(), new Long(getCount()), new Double(getRunningRate()) };
85      }
86  }