| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 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 3 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 | |
| 19 | package hu.netmind.beankeeper.logging.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.logging.AggregatorLogger; |
| 22 | import org.apache.log4j.Logger; |
| 23 | import java.util.*; |
| 24 | |
| 25 | /** |
| 26 | * This performance logger logs events in given intervals, and calculates |
| 27 | * different values for viewing. |
| 28 | * @author Brautigam Robert |
| 29 | * @version CVS Revision: $Revision$ |
| 30 | */ |
| 31 | public class AggregatorLoggerImpl implements AggregatorLogger |
| 32 | { |
| 33 | private static Logger logger = Logger.getLogger(AggregatorLogger.class); |
| 34 | private static int PERFORMANCE_INTERVAL = 5000; |
| 35 | |
| 36 | private long lastOutputTime = 0; |
| 37 | private Map eventEntries; |
| 38 | |
| 39 | public void init(Map parameters) |
| 40 | { |
| 41 | eventEntries = Collections.synchronizedMap(new HashMap()); |
| 42 | try |
| 43 | { |
| 44 | ResourceBundle config = ResourceBundle.getBundle("beankeeper"); |
| 45 | PERFORMANCE_INTERVAL = Integer.valueOf(config.getString("beankeeper.performance.interval")).intValue(); |
| 46 | } catch ( Exception e ) { |
| 47 | logger.error("could not read configuration file, using hardcoded defaults.",e); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public void release() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Log an event. |
| 57 | * @param message The message of given type. |
| 58 | * @param values The values for given message. |
| 59 | */ |
| 60 | public void log(String message, int[] values) |
| 61 | { |
| 62 | // If not enabled, do nothing |
| 63 | if ( ! logger.isDebugEnabled() ) |
| 64 | return; |
| 65 | // Adjust count |
| 66 | AggregateEntry entry = (AggregateEntry) eventEntries.get(message); |
| 67 | if ( entry == null ) |
| 68 | { |
| 69 | entry = new AggregateEntry(); |
| 70 | eventEntries.put(message,entry); |
| 71 | } |
| 72 | // Modify entry |
| 73 | entry.count++; |
| 74 | if ( entry.valuesSum == null ) |
| 75 | { |
| 76 | entry.valuesSum=values; |
| 77 | entry.valuesMin=new int[values.length]; |
| 78 | System.arraycopy(values,0,entry.valuesMin,0,values.length); |
| 79 | entry.valuesMax=new int[values.length]; |
| 80 | System.arraycopy(values,0,entry.valuesMax,0,values.length); |
| 81 | } else { |
| 82 | for ( int i=0; i<values.length; i++ ) |
| 83 | { |
| 84 | entry.valuesSum[i]+=values[i]; |
| 85 | if ( values[i] > entry.valuesMax[i] ) |
| 86 | entry.valuesMax[i] = values[i]; |
| 87 | if ( values[i] < entry.valuesMin[i] ) |
| 88 | entry.valuesMin[i] = values[i]; |
| 89 | } |
| 90 | } |
| 91 | // Decide what to do |
| 92 | long currentTime = System.currentTimeMillis(); |
| 93 | if ( currentTime > PERFORMANCE_INTERVAL + entry.lastOutputTime ) |
| 94 | { |
| 95 | // Time's up, output now |
| 96 | StringBuffer logline = new StringBuffer(message+": "+entry.count+" times, values: "); |
| 97 | if ( entry.valuesSum == null ) |
| 98 | { |
| 99 | logline.append("none"); |
| 100 | } else { |
| 101 | for ( int i=0; i<entry.valuesSum.length; i++ ) |
| 102 | logline.append(""+(entry.valuesSum[i]/entry.count)+"-"+ |
| 103 | entry.valuesMin[i]+"/"+entry.valuesMax[i]+" "); |
| 104 | } |
| 105 | logger.debug(logline); |
| 106 | // Clear entry |
| 107 | entry.lastOutputTime = currentTime; |
| 108 | entry.count=0; |
| 109 | entry.valuesSum=null; |
| 110 | entry.valuesMin=null; |
| 111 | entry.valuesMax=null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private static class AggregateEntry |
| 116 | { |
| 117 | public long lastOutputTime; |
| 118 | public int count; |
| 119 | public int[] valuesSum; |
| 120 | public int[] valuesMin; |
| 121 | public int[] valuesMax; |
| 122 | |
| 123 | public AggregateEntry() |
| 124 | { |
| 125 | lastOutputTime = 0; |
| 126 | count = 0; |
| 127 | valuesSum = null; |
| 128 | valuesMin = null; |
| 129 | valuesMax = null; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |