KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > 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.invocation;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
30
31 /** A method invocation statistics collection class.
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 42607 $
35  */

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

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

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

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

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