KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > workingset > lib > WorkingSetStatistic


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.workingset.lib;
19
20 import org.objectweb.util.monolog.api.Logger;
21 import org.objectweb.util.monolog.api.BasicLevel;
22 import org.objectweb.util.monolog.api.Loggable;
23 import org.objectweb.util.monolog.api.LoggerFactory;
24 import org.objectweb.speedo.api.TransactionListener;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.GregorianCalendar JavaDoc;
29 import java.util.Calendar JavaDoc;
30
31 /**
32  * This class is a simple implementation of the TransactionListener interface
33  * permitting to listen events concerning the transaction life cycle.
34  *
35  * @author S.Chassande-Barrioz
36  */

37 public class WorkingSetStatistic
38         implements TransactionListener, Loggable {
39
40     public static int LOG_LEVEL = BasicLevel.INFO;
41
42     public static WorkingSetStatistic instance = null;
43     public int maxWSSize;
44     public int minWSSize;
45     public int nbWS;
46     public int averageWSSize;
47     public long averageWSduration;
48     public int committedWS;
49     public Logger logger;
50     private Calendar JavaDoc cal;
51
52     public Map JavaDoc currentCtx;
53
54
55
56     public WorkingSetStatistic() {
57         instance = this;
58         cal = GregorianCalendar.getInstance();
59         init();
60     }
61
62     public void init() {
63         maxWSSize = 0;
64         minWSSize = Integer.MAX_VALUE;
65         nbWS = 0;
66         averageWSSize = 0;
67         committedWS = 0;
68         if (currentCtx == null) {
69             currentCtx = new HashMap JavaDoc(10);
70         } else {
71             currentCtx.clear();
72         }
73     }
74
75
76     public void printStatistic() {
77         printStatistic(logger);
78     }
79
80     public void printStatistic(Logger logger) {
81         logger.log(LOG_LEVEL, "Transaction statistic:"
82                 + "\n\t- Max size: " + maxWSSize
83                 + "\n\t- Min size: " + minWSSize
84                 + "\n\t- Average size: " + averageWSSize
85                 + "\n\t- Average duration: " + averageWSduration
86                 + "\n\t- Transaction number: " + nbWS
87                 + "\n\t- Commited Transaction: " + ((committedWS * 100) / nbWS) + "%"
88                 + "\n\t- current Transaction: " + currentCtx.size());
89     }
90
91     public void transactionEnded(Object JavaDoc tx, int s, boolean validate) {
92         long d2 = cal.getTime().getTime();
93         Long JavaDoc l = (Long JavaDoc) currentCtx.get(tx);
94         if (l != null) {
95             currentCtx.remove(tx);
96             averageWSduration = ((averageWSduration * nbWS) + d2 - l.longValue()) / (nbWS + 1);
97         }
98         maxWSSize = Math.max(maxWSSize, s);
99         minWSSize = Math.min(minWSSize, s);
100         averageWSSize = ((averageWSSize * nbWS) + s) / (nbWS + 1);
101         nbWS++;
102         if (validate)
103             committedWS++;
104     }
105
106
107     // IMPLEMENTATION OF THE TransactionListener INTERFACE //
108
//-----------------------------------------------------//
109

110     public synchronized void transactionBegun(Object JavaDoc tx) {
111         Long JavaDoc l = new Long JavaDoc(cal.getTime().getTime());
112         currentCtx.put(tx, l);
113     }
114
115     public synchronized void transactionCommitted(Object JavaDoc tx, int s) {
116         transactionEnded(tx, s, true);
117         printStatistic();
118     }
119
120     public synchronized void transactionAborted(Object JavaDoc tx, int s) {
121         transactionEnded(tx, s, false);
122         printStatistic();
123     }
124
125
126     // IMPLEMENTATION OF THE Loggable INTERFACE //
127
//------------------------------------------//
128

129     public Logger getLogger() {
130         return logger;
131     }
132
133     public void setLogger(Logger logger) {
134         this.logger = logger;
135     }
136
137     public LoggerFactory getLoggerFactory() {
138         return null;
139     }
140
141     public void setLoggerFactory(LoggerFactory loggerFactory) {
142         if (loggerFactory != null) {
143             logger = loggerFactory.getLogger("org.objectweb.speedo.txStatistic");
144         }
145     }
146 }
147
Popular Tags