KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > statistics > ReplicationStatistics


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.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
29
30 /** A session replication statistics collection class.
31  *
32  * @author Scott.Stark@jboss.org
33  * @version $Revision: 42610 $
34  */

35 public class ReplicationStatistics 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 ConcurrentReaderHashMap ctxStats;
42    /** Time of the last resetStats call */
43    public long lastResetTime = System.currentTimeMillis();
44
45    public static class TimeStatistic
46    {
47       public long replicationCount;
48       public long minPassivationTime = Long.MAX_VALUE;
49       public long maxPassivationTime;
50       public long totalPassivationTime;
51       public long minReplicationTime = Long.MAX_VALUE;
52       public long maxReplicationTime;
53       public long totalReplicationlTime;
54
55       public long loadCount;
56       public long minLoadTime = Long.MAX_VALUE;
57       public long maxLoadTime;
58       public long totalLoadlTime;
59
60       public void reset()
61       {
62          replicationCount = 0;
63          minPassivationTime = Long.MAX_VALUE;
64          maxPassivationTime = 0;
65          totalPassivationTime = 0;
66          minReplicationTime = Long.MAX_VALUE;
67          maxReplicationTime = 0;
68          totalReplicationlTime = 0;
69          loadCount = 0;
70          minLoadTime = Long.MAX_VALUE;
71          maxLoadTime = 0;
72          totalLoadlTime = 0;
73       }
74    }
75
76    public ReplicationStatistics()
77    {
78       ctxStats = new ConcurrentReaderHashMap();
79    }
80
81    public void updatePassivationStats(String JavaDoc ctx, long elapsed)
82    {
83       TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
84       if( stat == null )
85       {
86          stat = new TimeStatistic();
87          ctxStats.put(ctx, stat);
88       }
89       stat.totalPassivationTime += elapsed;
90       if( stat.minPassivationTime > elapsed )
91          stat.minPassivationTime = elapsed;
92       if( stat.maxPassivationTime < elapsed )
93          stat.maxPassivationTime = elapsed;
94    }
95    /** Update the TimeStatistic for the given ctx. This does not synchronize
96     * on the TimeStatistic so the results are an approximate values.
97     *
98     * @param ctx the method to update the statistics for.
99     * @param elapsed the elapsed time in milliseconds for the invocation.
100     */

101    public void updateReplicationStats(String JavaDoc ctx, long elapsed)
102    {
103       TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
104       if( stat == null )
105       {
106          stat = new TimeStatistic();
107          ctxStats.put(ctx, stat);
108       }
109       stat.replicationCount ++;
110       stat.totalReplicationlTime += elapsed;
111       if( stat.minReplicationTime > elapsed )
112          stat.minReplicationTime = elapsed;
113       if( stat.maxReplicationTime < elapsed )
114          stat.maxReplicationTime = elapsed;
115    }
116    public void updateLoadStats(String JavaDoc ctx, long elapsed)
117    {
118       TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
119       if( stat == null )
120       {
121          stat = new TimeStatistic();
122          ctxStats.put(ctx, stat);
123       }
124       stat.loadCount ++;
125       stat.totalLoadlTime += elapsed;
126       if( stat.minLoadTime > elapsed )
127          stat.minLoadTime = elapsed;
128       if( stat.maxLoadTime < elapsed )
129          stat.maxLoadTime = elapsed;
130    }
131
132    /** Resets all current TimeStatistics.
133     *
134     */

135    public void resetStats()
136    {
137       synchronized( ctxStats )
138       {
139          Iterator JavaDoc iter = ctxStats.values().iterator();
140          while( iter.hasNext() )
141          {
142             TimeStatistic stat = (TimeStatistic) iter.next();
143             stat.reset();
144          }
145       }
146       lastResetTime = System.currentTimeMillis();
147    }
148
149    public void removeStats(String JavaDoc id)
150    {
151       ctxStats.remove(id);
152    }
153
154    /** Access the current collection of ctx invocation statistics
155     *
156     * @return A HashMap<String, TimeStatistic> of the ctx invocations
157     */

158    public Map JavaDoc getStats()
159    {
160       return ctxStats;
161    }
162
163    public String JavaDoc toString()
164    {
165       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
166       HashMap JavaDoc copy = new HashMap JavaDoc(ctxStats);
167       Iterator JavaDoc iter = copy.entrySet().iterator();
168       while( iter.hasNext() )
169       {
170          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
171          TimeStatistic stat = (TimeStatistic) entry.getValue();
172          if (stat != null)
173          {
174             tmp.append("[sessionID: ");
175             tmp.append(entry.getKey());
176             tmp.append(", replicationCount=");
177             tmp.append(stat.replicationCount);
178             tmp.append(", minPassivationTime=");
179             tmp.append(stat.minPassivationTime);
180             tmp.append(", maxPassivationTime=");
181             tmp.append(stat.maxPassivationTime);
182             tmp.append(", totalPassivationTime=");
183             tmp.append(stat.totalPassivationTime);
184             tmp.append(", minReplicationTime=");
185             tmp.append(stat.minReplicationTime);
186             tmp.append(", maxReplicationTime=");
187             tmp.append(stat.maxReplicationTime);
188             tmp.append(", totalReplicationlTime=");
189             tmp.append(stat.totalReplicationlTime);
190             tmp.append(", loadCount=");
191             tmp.append(stat.loadCount);
192             tmp.append(", minLoadTime=");
193             tmp.append(stat.minLoadTime);
194             tmp.append(", maxLoadTime=");
195             tmp.append(stat.maxLoadTime);
196             tmp.append(", totaLoadlTime=");
197             tmp.append(stat.totalLoadlTime);
198             tmp.append("];");
199          }
200       }
201       tmp.append(")");
202       return tmp.toString();
203    }
204
205 }
206
Popular Tags