summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/util/StatsEvent.java
blob: 7c510b88902e5d1a9fa3b8b3d04cb6f377fee562 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.util;


import java.util.Enumeration;
import java.util.Vector;

/**
 * A statistics transaction.
 * <P>
 * 
 * @author thomask
 * @version $Revision$, $Date$
 */
public class StatsEvent
{
  private String mName = null;
  private long mMin = -1;
  private long mMax = -1;
  private long mTimeTaken = 0;
  private long mTimeTakenSqSum = 0;
  private long mNoOfOperations = 0;
  private Vector mSubEvents = new Vector();
  private StatsEvent mParent = null;

  public StatsEvent(StatsEvent parent)
  {
    mParent = parent;
  }
  
  public void setName(String name) 
  {
    mName = name;
  }

  /**
   * Retrieves Transaction name.
   */ 
  public String getName() 
  {
    return mName;
  }
    
  public void addSubEvent(StatsEvent st)
  {
    mSubEvents.addElement(st);
  }

  /**
   * Retrieves a list of sub transaction names.
   */
  public Enumeration getSubEventNames()
  {
    Vector names = new Vector();
    Enumeration e = mSubEvents.elements();
    while (e.hasMoreElements()) {
      StatsEvent st = (StatsEvent)e.nextElement();
      names.addElement(st.getName());
    }
    return names.elements();
  }  

  /** 
   * Retrieves a sub transaction.
   */
  public StatsEvent getSubEvent(String name)
  {
    Enumeration e = mSubEvents.elements();
    while (e.hasMoreElements()) {
      StatsEvent st = (StatsEvent)e.nextElement();
      if (st.getName().equals(name)) {
        return st;
      }
    }
    return null;
  }

  public void resetCounters()
  {
    mMin = -1;
    mMax = -1;
    mNoOfOperations = 0;
    mTimeTaken = 0;
    mTimeTakenSqSum = 0;
    Enumeration e = getSubEventNames();
    while (e.hasMoreElements()) {
      String n = (String)e.nextElement();
      StatsEvent c = getSubEvent(n);
      c.resetCounters();
    }
  }

  public long getMax()
  {
    return mMax;
  }

  public long getMin()
  {
    return mMin;
  }

  public void incNoOfOperations(long c)
  {
    mNoOfOperations += c;
  }

  public long getTimeTakenSqSum()
  {
    return mTimeTakenSqSum;
  }

  public long getPercentage()
  {
    if (mParent == null || mParent.getTimeTaken() == 0) {
      return 100;
    } else {
      return (mTimeTaken * 100 / mParent.getTimeTaken());
    }
  }

  public long getStdDev()
  {
    if (getNoOfOperations() == 0) {
      return 0;
    } else {
      long a = getTimeTakenSqSum();
      long b = (-2 * getAvg() *getTimeTaken());
      long c = getAvg() * getAvg() * getNoOfOperations();
      return (long)Math.sqrt((a + b + c)/getNoOfOperations());
    }
  }

  public long getAvg()
  {
    if (mNoOfOperations == 0) {
      return -1;
    } else {
      return mTimeTaken/mNoOfOperations;
    }
  }

  /**
   * Retrieves number of operations performed.
   */
  public long getNoOfOperations()
  {
    return mNoOfOperations;
  }
 
  public void incTimeTaken(long c)
  {
    if (mMin == -1) {
      mMin = c;
    } else {
      if (c < mMin) {
        mMin = c;
      }
    }
    if (mMax == -1) {
      mMax = c;
    } else {
      if (c > mMax) {
        mMax = c;
      }
    }
    mTimeTaken += c;
    mTimeTakenSqSum += (c * c);
  }

  /**
   * Retrieves total time token in msec.
   */
  public long getTimeTaken()
  {
    return mTimeTaken;
  }
}