001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.workingset.lib;
018:
019: import org.objectweb.util.monolog.api.Logger;
020: import org.objectweb.util.monolog.api.BasicLevel;
021: import org.objectweb.util.monolog.api.Loggable;
022: import org.objectweb.util.monolog.api.LoggerFactory;
023: import org.objectweb.speedo.api.TransactionListener;
024:
025: import java.util.Map;
026: import java.util.HashMap;
027: import java.util.GregorianCalendar;
028: import java.util.Calendar;
029:
030: /**
031: * This class is a simple implementation of the TransactionListener interface
032: * permitting to listen events concerning the transaction life cycle.
033: *
034: * @author S.Chassande-Barrioz
035: */
036: public class WorkingSetStatistic implements TransactionListener,
037: Loggable {
038:
039: public static int LOG_LEVEL = BasicLevel.INFO;
040:
041: public static WorkingSetStatistic instance = null;
042: public int maxWSSize;
043: public int minWSSize;
044: public int nbWS;
045: public int averageWSSize;
046: public long averageWSduration;
047: public int committedWS;
048: public Logger logger;
049: private Calendar cal;
050:
051: public Map currentCtx;
052:
053: public WorkingSetStatistic() {
054: instance = this ;
055: cal = GregorianCalendar.getInstance();
056: init();
057: }
058:
059: public void init() {
060: maxWSSize = 0;
061: minWSSize = Integer.MAX_VALUE;
062: nbWS = 0;
063: averageWSSize = 0;
064: committedWS = 0;
065: if (currentCtx == null) {
066: currentCtx = new HashMap(10);
067: } else {
068: currentCtx.clear();
069: }
070: }
071:
072: protected StringBuffer getStatistic() {
073: StringBuffer sb = new StringBuffer();
074: sb.append("JDOTransactionItf statistic:");
075: sb.append("\n\t- Max size: ").append(maxWSSize);
076: sb.append("\n\t- Min size: ").append(minWSSize);
077: sb.append("\n\t- Average size: ").append(averageWSSize);
078: sb.append("\n\t- Average duration: ").append(averageWSduration);
079: sb.append("\n\t- JDOTransactionItf number: ").append(nbWS);
080: sb.append("\n\t- Commited JDOTransactionItf: ").append(
081: ((committedWS * 100) / nbWS)).append("%");
082: sb.append("\n\t- current JDOTransactionItf: ").append(
083: currentCtx.size());
084: return sb;
085: }
086:
087: public void printStatistic() {
088: printStatistic(logger);
089: }
090:
091: public void printStatistic(Logger l) {
092: l.log(LOG_LEVEL, getStatistic().toString());
093: }
094:
095: public void transactionEnded(Object tx, int s, Boolean validate) {
096: long d2 = cal.getTime().getTime();
097: Long l = (Long) currentCtx.get(tx);
098: if (l != null) {
099: currentCtx.remove(tx);
100: averageWSduration = ((averageWSduration * nbWS) + d2 - l
101: .longValue())
102: / (nbWS + 1);
103: }
104: maxWSSize = Math.max(maxWSSize, s);
105: minWSSize = Math.min(minWSSize, s);
106: averageWSSize = ((averageWSSize * nbWS) + s) / (nbWS + 1);
107: nbWS++;
108: if (validate != null && validate.booleanValue())
109: committedWS++;
110: }
111:
112: // IMPLEMENTATION OF THE TransactionListener INTERFACE //
113: //-----------------------------------------------------//
114:
115: public synchronized void transactionBegun(Object tx) {
116: Long l = new Long(cal.getTime().getTime());
117: currentCtx.put(tx, l);
118: }
119:
120: public synchronized void transactionCommitted(Object tx, int s) {
121: transactionEnded(tx, s, Boolean.TRUE);
122: printStatistic();
123: }
124:
125: public synchronized void transactionAborted(Object tx, int s) {
126: transactionEnded(tx, s, Boolean.FALSE);
127: printStatistic();
128: }
129:
130: public synchronized void transactionPreValidate(Object tx, int s) {
131: transactionEnded(tx, s, null);
132: printStatistic();
133: }
134:
135: // IMPLEMENTATION OF THE Loggable INTERFACE //
136: //------------------------------------------//
137:
138: public Logger getLogger() {
139: return logger;
140: }
141:
142: public void setLogger(Logger logger) {
143: this .logger = logger;
144: }
145:
146: public LoggerFactory getLoggerFactory() {
147: return null;
148: }
149:
150: public void setLoggerFactory(LoggerFactory loggerFactory) {
151: if (loggerFactory != null) {
152: logger = loggerFactory
153: .getLogger("org.objectweb.speedo.txStatistic");
154: }
155: }
156: }
|