1 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 ; 27 import java.util.HashMap ; 28 import java.util.GregorianCalendar ; 29 import java.util.Calendar ; 30 31 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 cal; 51 52 public Map 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 (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 tx, int s, boolean validate) { 92 long d2 = cal.getTime().getTime(); 93 Long l = (Long ) 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 110 public synchronized void transactionBegun(Object tx) { 111 Long l = new Long (cal.getTime().getTime()); 112 currentCtx.put(tx, l); 113 } 114 115 public synchronized void transactionCommitted(Object tx, int s) { 116 transactionEnded(tx, s, true); 117 printStatistic(); 118 } 119 120 public synchronized void transactionAborted(Object tx, int s) { 121 transactionEnded(tx, s, false); 122 printStatistic(); 123 } 124 125 126 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 |