KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > statistics > InvocationStatistics


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb3.statistics;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
31
32 /** A method invocation statistics collection class.
33  *
34  * @author Scott.Stark@jboss.org
35  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
36  */

37 public class InvocationStatistics implements Serializable JavaDoc
38 {
39    /** A HashMap<Method, TimeStatistic> of the method invocations */
40    private Map JavaDoc methodStats;
41
42    public long concurrentCalls = 0;
43    public long maxConcurrentCalls = 0;
44    public long lastResetTime = System.currentTimeMillis();
45
46    public class TimeStatistic implements Serializable JavaDoc
47    {
48       public volatile long count;
49       public volatile long minTime = Long.MAX_VALUE;
50       public volatile long maxTime;
51       public volatile long totalTime;
52
53       public void reset()
54       {
55          count = 0;
56          minTime = Long.MAX_VALUE;
57          maxTime = 0;
58          totalTime = 0;
59       }
60    }
61
62    public InvocationStatistics()
63    {
64       methodStats = new ConcurrentReaderHashMap();
65    }
66
67    /** Update the TimeStatistic for the given method. This synchronizes on
68     * m to ensure that the TimeStatistic for m is updated atomically.
69     *
70     * @param m the method to update the statistics for.
71     * @param elapsed the elapsed time in milliseconds for the invocation.
72     */

73    public void updateStats(Method JavaDoc m, long elapsed)
74    {
75       TimeStatistic stat = (TimeStatistic) methodStats.get(m.getName());
76       if (stat == null)
77       {
78          stat = new TimeStatistic();
79          methodStats.put(m.getName(), stat);
80       }
81       stat.count++;
82       stat.totalTime += elapsed;
83       if (stat.minTime > elapsed)
84          stat.minTime = elapsed;
85       if (stat.maxTime < elapsed)
86          stat.maxTime = elapsed;
87    }
88
89    public synchronized void callIn()
90    {
91       concurrentCalls++;
92       if (concurrentCalls > maxConcurrentCalls)
93          maxConcurrentCalls = concurrentCalls;
94    }
95
96    public synchronized void callOut()
97    {
98       concurrentCalls--;
99    }
100
101    /** Resets all current TimeStatistics.
102     *
103     */

104    public void resetStats()
105    {
106       synchronized (methodStats)
107       {
108          Iterator JavaDoc iter = methodStats.values().iterator();
109          while (iter.hasNext())
110          {
111             TimeStatistic stat = (TimeStatistic) iter.next();
112             stat.reset();
113          }
114       }
115       maxConcurrentCalls = 0;
116       lastResetTime = System.currentTimeMillis();
117    }
118
119    /** Access the current collection of method invocation statistics
120     *
121     * @return A HashMap<Method, TimeStatistic> of the method invocations
122     */

123    public Map JavaDoc getStats()
124    {
125       return methodStats;
126    }
127
128    /** Generate an XML fragement for the InvocationStatistics. The format is
129     * <InvocationStatistics concurrentCalls="c">
130     * <method name="aMethod" count="x" minTime="y" maxTime="z" totalTime="t" />
131     * ...
132     * </InvocationStatistics>
133     *
134     * @return an XML representation of the InvocationStatistics
135     */

136    public String JavaDoc toString()
137    {
138       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("<InvocationStatistics concurrentCalls='");
139       tmp.append(concurrentCalls);
140       tmp.append("' >\n");
141
142       HashMap JavaDoc copy = new HashMap JavaDoc(methodStats);
143       Iterator JavaDoc iter = copy.entrySet().iterator();
144       while (iter.hasNext())
145       {
146          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
147          TimeStatistic stat = (TimeStatistic) entry.getValue();
148          if (stat != null)
149          {
150             tmp.append("<method name='");
151             tmp.append(entry.getKey());
152             tmp.append("' count='");
153             tmp.append(stat.count);
154             tmp.append("' minTime='");
155             tmp.append(stat.minTime);
156             tmp.append("' maxTime='");
157             tmp.append(stat.maxTime);
158             tmp.append("' totalTime='");
159             tmp.append(stat.totalTime);
160             tmp.append("' />\n");
161          }
162       }
163       tmp.append("</InvocationStatistics>");
164       return tmp.toString();
165    }
166 }
167
Popular Tags