KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > 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.web.tomcat.statistics;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /** A web context invocation statistics collection class.
31  *
32  * @author Scott.Stark@jboss.org
33  * @version $Revision: 37459 $
34  */

35 public class InvocationStatistics implements Serializable JavaDoc
36 {
37    /** The serial version ID */
38    private static final long serialVersionUID = 9153807780893455734L;
39
40    /** A HashMap<String, TimeStatistic> of the method invocations */
41    private Map JavaDoc ctxStats;
42    /** The number of concurrent request across all contexts */
43    public volatile int concurrentCalls = 0;
44    /** The maximum number of concurrent request across all contexts */
45    public volatile int maxConcurrentCalls = 0;
46    /** Time of the last resetStats call */
47    public long lastResetTime = System.currentTimeMillis();
48
49    public static class TimeStatistic
50    {
51       public long count;
52       public long minTime = Long.MAX_VALUE;
53       public long maxTime;
54       public long totalTime;
55
56       public void reset()
57       {
58          count = 0;
59          minTime = Long.MAX_VALUE;
60          maxTime = 0;
61          totalTime = 0;
62       }
63    }
64
65    public InvocationStatistics()
66    {
67       ctxStats = Collections.synchronizedMap(new HashMap JavaDoc());
68    }
69
70    /** Update the TimeStatistic for the given ctx. This does not synchronize
71     * on the TimeStatistic so the results are an approximate values.
72     *
73     * @param ctx the method to update the statistics for.
74     * @param elapsed the elapsed time in milliseconds for the invocation.
75     */

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

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

126    public Map JavaDoc getStats()
127    {
128       return ctxStats;
129    }
130
131    public String JavaDoc toString()
132    {
133       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("(concurrentCalls: ");
134       tmp.append(concurrentCalls);
135       tmp.append(", maxConcurrentCalls: ");
136       tmp.append(maxConcurrentCalls);
137
138       HashMap JavaDoc copy = new HashMap JavaDoc(ctxStats);
139       Iterator JavaDoc iter = copy.entrySet().iterator();
140       while( iter.hasNext() )
141       {
142          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
143          TimeStatistic stat = (TimeStatistic) entry.getValue();
144          tmp.append("[webCtx: ");
145          tmp.append(entry.getKey());
146          tmp.append(", count=");
147          tmp.append(stat.count);
148          tmp.append(", minTime=");
149          tmp.append(stat.minTime);
150          tmp.append(", maxTime=");
151          tmp.append(stat.maxTime);
152          tmp.append(", totalTime=");
153          tmp.append(stat.totalTime);
154          tmp.append("];");
155       }
156       tmp.append(")");
157       return tmp.toString();
158    }
159
160    /** Generate an XML fragement for the InvocationStatistics. The format is
161     * <InvocationStatistics concurrentCalls="c" maxConcurrentCalls="x">
162     * <webCtx name="ctx" count="x" minTime="y" maxTime="z" totalTime="t" />
163     * ...
164     * </InvocationStatistics>
165     *
166     * @return an XML representation of the InvocationStatistics
167     */

168    public String JavaDoc toXML()
169    {
170       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc("<InvocationStatistics concurrentCalls='");
171       tmp.append(concurrentCalls);
172       tmp.append("' maxConcurrentCalls='");
173       tmp.append(maxConcurrentCalls);
174       tmp.append("' >\n");
175
176       HashMap JavaDoc copy = new HashMap JavaDoc(ctxStats);
177       Iterator JavaDoc iter = copy.entrySet().iterator();
178       while( iter.hasNext() )
179       {
180          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
181          TimeStatistic stat = (TimeStatistic) entry.getValue();
182          tmp.append("<webCtx name='");
183          tmp.append(entry.getKey());
184          tmp.append("' count='");
185          tmp.append(stat.count);
186          tmp.append("' minTime='");
187          tmp.append(stat.minTime);
188          tmp.append("' maxTime='");
189          tmp.append(stat.maxTime);
190          tmp.append("' totalTime='");
191          tmp.append(stat.totalTime);
192          tmp.append("' />\n");
193       }
194       tmp.append("</InvocationStatistics>");
195       return tmp.toString();
196    }
197 }
198
Popular Tags