001: /**
002: * Copyright (C) 2007 NetMind Consulting Bt.
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 3 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 hu.netmind.persistence;
018:
019: import org.apache.log4j.Logger;
020: import java.util.*;
021:
022: /**
023: * This performance logger logs events in given intervals, and calculates
024: * different values for viewing.
025: * @author Brautigam Robert
026: * @version CVS Revision: $Revision$
027: */
028: public class PerformanceLogger {
029: private static Logger logger = Logger
030: .getLogger(PerformanceLogger.class);
031: private static PerformanceLogger instance = null;
032: private static int PERFORMANCE_INTERVAL = 5000;
033:
034: private long lastOutputTime = 0;
035: private Map eventEntries;
036:
037: private PerformanceLogger() {
038: eventEntries = Collections.synchronizedMap(new HashMap());
039: }
040:
041: public static PerformanceLogger getLogger() {
042: return instance;
043: }
044:
045: /**
046: * Log an event.
047: * @param message The message of given type.
048: * @param values The values for given message.
049: */
050: public void log(String message, int[] values) {
051: // If not enabled, do nothing
052: if (!logger.isDebugEnabled())
053: return;
054: // Adjust count
055: PerformanceEntry entry = (PerformanceEntry) eventEntries
056: .get(message);
057: if (entry == null) {
058: entry = new PerformanceEntry();
059: eventEntries.put(message, entry);
060: }
061: // Modify entry
062: entry.count++;
063: if (entry.valuesSum == null) {
064: entry.valuesSum = values;
065: entry.valuesMin = new int[values.length];
066: System.arraycopy(values, 0, entry.valuesMin, 0,
067: values.length);
068: entry.valuesMax = new int[values.length];
069: System.arraycopy(values, 0, entry.valuesMax, 0,
070: values.length);
071: } else {
072: for (int i = 0; i < values.length; i++) {
073: entry.valuesSum[i] += values[i];
074: if (values[i] > entry.valuesMax[i])
075: entry.valuesMax[i] = values[i];
076: if (values[i] < entry.valuesMin[i])
077: entry.valuesMin[i] = values[i];
078: }
079: }
080: // Decide what to do
081: long currentTime = System.currentTimeMillis();
082: if (currentTime > PERFORMANCE_INTERVAL + entry.lastOutputTime) {
083: // Time's up, output now
084: StringBuffer logline = new StringBuffer(message + ": "
085: + entry.count + " times, values: ");
086: if (entry.valuesSum == null) {
087: logline.append("none");
088: } else {
089: for (int i = 0; i < entry.valuesSum.length; i++)
090: logline.append(""
091: + (entry.valuesSum[i] / entry.count) + "-"
092: + entry.valuesMin[i] + "/"
093: + entry.valuesMax[i] + " ");
094: }
095: logger.debug(logline);
096: // Clear entry
097: entry.lastOutputTime = currentTime;
098: entry.count = 0;
099: entry.valuesSum = null;
100: entry.valuesMin = null;
101: entry.valuesMax = null;
102: }
103: }
104:
105: private static class PerformanceEntry {
106: public long lastOutputTime;
107: public int count;
108: public int[] valuesSum;
109: public int[] valuesMin;
110: public int[] valuesMax;
111:
112: public PerformanceEntry() {
113: lastOutputTime = 0;
114: count = 0;
115: valuesSum = null;
116: valuesMin = null;
117: valuesMax = null;
118: }
119: }
120:
121: static {
122: instance = new PerformanceLogger();
123: try {
124: ResourceBundle config = ResourceBundle
125: .getBundle("beankeeper");
126: PERFORMANCE_INTERVAL = Integer.valueOf(
127: config.getString("performance.interval"))
128: .intValue();
129: } catch (Exception e) {
130: logger
131: .error(
132: "could not read configuration file, using hardcoded defaults.",
133: e);
134: }
135: }
136: }
|