001: package org.igfay.util;
002:
003: import java.text.DateFormat;
004: import java.util.ArrayList;
005: import java.util.Date;
006: import java.util.HashMap;
007: import java.util.List;
008:
009: import org.apache.log4j.Level;
010: import org.apache.log4j.Logger;
011: import org.igfay.jfig.JFigUtility;
012:
013: /**
014: * Capture metrics Creation date: (12/12/2001 9:35:23 AM)
015: *
016: *@author bconrad
017: *@created December 12, 2001
018: *@author: Bruce Conrad
019: */
020: public class Metric {
021: private static Logger log = Logger.getLogger(Metric.class);
022:
023: public final static String DEFAULT = "DEFAULT";
024: public final static String BENCHMARK = "BENCHMARK";
025: public final static String DB = "DB";
026: public final static String EVENT = "EVENT";
027: public final static String LOGIN = "LOGIN";
028: public final static String QUERY = "QUERY";
029: public final static String DOWNLOAD = "DOWNLOAD";
030: public final static String UPLOAD = "UPLOAD";
031: public final static String ACCEPTANCE_MANAGER = "ACCEPTANCE_MANAGER";
032: public final static String NOTIFY_ACTION = "NOTIFY_ACTION";
033: public final static String RULES_ENGINE = "RULES_ENGINE";
034:
035: private static java.util.HashMap allGroups;
036:
037: private long startTime;
038: private long endTime;
039: private long pauseStartTime;
040: private long pauseElapsedTime;
041: private java.lang.String groupName;
042: private java.lang.String metricName;
043: private boolean isDebugEnabled;
044:
045: /**
046: * Metric constructor comment.
047: */
048: public Metric() {
049: this (false);
050: }
051:
052: /**
053: * Metric constructor comment.
054: *
055: *@param groupName Description of Parameter
056: */
057: public Metric(String groupName) {
058: this (groupName, false);
059: }
060:
061: /**
062: * Metric constructor comment.
063: *
064: *@param groupName Description of Parameter
065: *@param metricName Description of Parameter
066: */
067: public Metric(String groupName, String metricName) {
068: this (groupName, metricName, false);
069: }
070:
071: /**
072: * Metric constructor comment.
073: *
074: *@param groupName Description of Parameter
075: *@param metricName Description of Parameter
076: *@param isStart Description of Parameter
077: */
078: public Metric(String groupName, String metricName, boolean isStart) {
079: this (groupName, metricName, isStart, false);
080: }
081:
082: public Metric(String groupName, String metricName, boolean isStart,
083: boolean isDebugEnabled) {
084: this .groupName = groupName;
085: this .metricName = metricName;
086: this .setIsDebugEnabled(isDebugEnabled);
087: // Don't add to group until we have a use for this (and a way to clear the group)
088: // otw, it will never get garbage collected
089: //addToListForGroup(groupName, this);
090: if (isStart) {
091: start();
092: }
093: }
094:
095: /**
096: * Metric constructor comment.
097: *
098: *@param groupName Description of Parameter
099: *@param isStart Description of Parameter
100: */
101: public Metric(String groupName, boolean isStart) {
102: this (groupName, "unnamed", isStart);
103: }
104:
105: /**
106: * Metric constructor comment.
107: *
108: *@param isStart Description of Parameter
109: */
110: public Metric(boolean isStart) {
111: this (DEFAULT, isStart);
112: }
113:
114: /**
115: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
116: * AM)
117: *
118: *@param groupName The feature to be added to the ToListForGroup attribute
119: *@param metric The feature to be added to the ToListForGroup attribute
120: */
121: public static void addToListForGroup(String groupName, Metric metric) {
122: getListForGroup(groupName).add(metric);
123: }
124:
125: /**
126: * Insert the method's description here. Creation date: (12/12/2001
127: * 11:03:27 AM)
128: *
129: *@param args java.lang.String[]
130: */
131: public static void main(String[] args) {
132: testSimple();
133: }
134:
135: /**
136: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
137: * AM)
138: *
139: *@param groupName Description of Parameter
140: */
141: public static void printGroup(String groupName) {
142: List list = getListForGroup(groupName);
143: for (int i = 0; i < list.size(); i++) {
144: Metric metric = (Metric) list.get(i);
145: log.info(metric.toStringLong());
146: }
147: }
148:
149: /**
150: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
151: * AM)
152: */
153: public static void test() {
154: Metric metric = new Metric(Metric.LOGIN, "BRUCE1");
155: Metric bruce2 = new Metric(Metric.LOGIN, "BRUCE2");
156: Metric bruce3 = new Metric(Metric.LOGIN, "BRUCE3");
157: metric.start();
158: bruce2.start();
159: bruce3.start();
160: try {
161: Thread.sleep(300);
162: } catch (InterruptedException e) {
163: }
164: metric.end();
165: bruce2.pause();
166: try {
167: Thread.sleep(300);
168: } catch (InterruptedException e) {
169: }
170: bruce2.resume();
171: bruce2.end();
172: bruce3.end();
173: Metric bruce4 = new Metric(Metric.LOGIN, "BRUCE4", true);
174: printGroup(Metric.LOGIN);
175: bruce4.end();
176: log.info(bruce4.toStringLong());
177:
178: bruce4.clear();
179: bruce4.start();
180: for (int i = 0; i < 1000000; i++) {
181: }
182: bruce4.end();
183: log.info(bruce4.toStringLong());
184:
185: // if (isMetricLoggingEnabled())
186: bruce4.print();
187: testIsDebugEnabled(bruce4);
188:
189: }
190:
191: public static void testSimple() {
192: Metric metric = new Metric(Metric.LOGIN, "BRUCE1");
193: metric.start();
194: try {
195: Thread.sleep(300);
196: } catch (InterruptedException e) {
197: }
198: metric.endAndPrint();
199: }
200:
201: public static void testIsDebugEnabled(Metric bruce4) {
202: log.info("");
203: bruce4.setIsDebugEnabled(true);
204: log.setLevel(Level.INFO);
205: log.info("should not print");
206: bruce4.print();
207: bruce4.setIsDebugEnabled(false);
208: log.info("should print");
209: bruce4.print();
210: }
211:
212: /**
213: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
214: * AM)
215: *
216: *@param groupName Description of Parameter
217: *@return int
218: */
219: public static List getListForGroup(String groupName) {
220: List list = (List) getAllGroups().get(groupName);
221: if (list == null) {
222: list = new ArrayList();
223: getAllGroups().put(groupName, list);
224: }
225: return list;
226: }
227:
228: /**
229: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
230: * AM)
231: *
232: *@return The MetricLoggingEnabled value
233: */
234: public static boolean isMetricLoggingEnabled() {
235: /*
236:
237: if (isMetricLoggingEnabled == null) {
238: isMetricLoggingEnabled = new Boolean(com.bc.config.BCConfigImpl.getConfig().getValue("Logging", "metricLogging", "false").equalsIgnoreCase("true"));
239: }
240: return isMetricLoggingEnabled.booleanValue();
241: */
242: return true;
243: }
244:
245: /**
246: * Insert the method's description here. Creation date: (12/12/2001
247: * 10:07:54 AM)
248: *
249: *@return java.util.HashMap
250: */
251: private static HashMap getAllGroups() {
252: if (allGroups == null) {
253: allGroups = new HashMap();
254: }
255:
256: return allGroups;
257: }
258:
259: /**
260: * Insert the method's description here. Creation date: (12/12/2001
261: * 10:07:54 AM)
262: *
263: *@param newAllGroups java.util.HashMap
264: */
265: private static void setAllGroups(HashMap newAllGroups) {
266: allGroups = newAllGroups;
267: }
268:
269: /**
270: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
271: * AM)
272: */
273: public void clear() {
274: setStartTime(0);
275: setEndTime(0);
276: setPauseElapsedTime(0);
277: setPauseStartTime(0);
278:
279: }
280:
281: /**
282: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
283: * AM)
284: */
285: public void end() {
286: setEndTime(System.currentTimeMillis());
287:
288: }
289:
290: public void stop() {
291: end();
292:
293: }
294:
295: public void endAndPrint() {
296: end();
297: print();
298:
299: }
300:
301: /**
302: * Insert the method's description here. Creation date: (12/12/2001 9:49:00
303: * AM)
304: */
305: public void pause() {
306: setPauseStartTime(System.currentTimeMillis());
307: }
308:
309: /**
310: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
311: * AM)
312: */
313: public void print() {
314: if (isMetricLoggingEnabled()) {
315: if (isDebugEnabled() && !log.isDebugEnabled())
316: return;
317: log.info(toStringLong());
318: }
319:
320: }
321:
322: /**
323: * Insert the method's description here. Creation date: (12/12/2001 9:49:00
324: * AM)
325: */
326: public void resume() {
327: if (getPauseStartTime() > 0) {
328: setPauseElapsedTime(getPauseElapsedTime()
329: + System.currentTimeMillis() - getPauseStartTime());
330: setPauseStartTime(0);
331: }
332: }
333:
334: /**
335: * Insert the method's description here. Creation date: (12/12/2001 9:46:19
336: * AM)
337: */
338: public void start() {
339: clear();
340: setStartTime(System.currentTimeMillis());
341:
342: }
343:
344: /**
345: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
346: * AM)
347: *
348: *@return int
349: */
350: public String toString() {
351: return toStringBuffer().toString();
352: }
353:
354: /**
355: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
356: * AM)
357: *
358: *@return int
359: */
360: public StringBuffer toStringBuffer() {
361: StringBuffer buffer = new StringBuffer();
362: String blank = " ";
363: String elapsedTimeString = "---";
364: // if (isComplete()) {
365: elapsedTimeString = Long.toString(getElapsedTime());
366: // }
367: buffer.append(getGroupName()).append(blank).append(
368: getMetricName()).append(blank)
369: .append(elapsedTimeString);
370: return buffer;
371: }
372:
373: /**
374: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
375: * AM)
376: *
377: *@return int
378: */
379: public String toStringLong() {
380: StringBuffer buffer = toStringBuffer();
381:
382: DateFormat dateFormat = JFigUtility.getDateFormat();
383: String startDate = dateFormat.format(new Date(getStartTime()));
384: String endDate = dateFormat.format(new Date(getEndTime()));
385: String blank = " ";
386:
387: buffer.append(blank).append(startDate).append(blank).append(
388: endDate).append(blank).append(getPauseElapsedTime())
389: .append(blank);
390: return buffer.toString();
391: }
392:
393: /**
394: * Insert the method's description here. Creation date: (12/12/2001
395: * 10:01:21 AM)
396: *
397: *@return java.lang.String
398: */
399: public String getGroupName() {
400: return groupName;
401: }
402:
403: /**
404: * Insert the method's description here. Creation date: (12/12/2001
405: * 10:01:55 AM)
406: *
407: *@return java.lang.String
408: */
409: public String getMetricName() {
410: return metricName;
411: }
412:
413: /**
414: * Insert the method's description here. Creation date: (12/12/2001 9:38:44
415: * AM)
416: *
417: *@return int
418: */
419: public long getElapsedTime() {
420: return getEndTime() - getStartTime() - getPauseElapsedTime();
421: }
422:
423: /**
424: * Insert the method's description here. Creation date: (12/12/2001 9:36:34
425: * AM)
426: *
427: *@return java.lang.Long
428: */
429: protected long getEndTime() {
430: return endTime;
431: }
432:
433: /**
434: * Insert the method's description here. Creation date: (12/12/2001 9:50:37
435: * AM)
436: *
437: *@return long
438: */
439: protected long getPauseElapsedTime() {
440: return pauseElapsedTime;
441: }
442:
443: /**
444: * Insert the method's description here. Creation date: (12/12/2001 9:49:46
445: * AM)
446: *
447: *@return long
448: */
449: protected long getPauseStartTime() {
450: return pauseStartTime;
451: }
452:
453: /**
454: * Insert the method's description here. Creation date: (12/12/2001 9:36:14
455: * AM)
456: *
457: *@return java.lang.Long
458: */
459: protected long getStartTime() {
460: return startTime;
461: }
462:
463: /**
464: * Insert the method's description here. Creation date: (12/12/2001 9:36:34
465: * AM)
466: *
467: *@param newEndTime java.lang.Long
468: */
469: public void setEndTime(long newEndTime) {
470: endTime = newEndTime;
471: }
472:
473: /**
474: * Insert the method's description here. Creation date: (12/12/2001
475: * 10:01:21 AM)
476: *
477: *@param newGroup java.lang.String
478: */
479: public void setGroupName(String newGroup) {
480: groupName = newGroup;
481: }
482:
483: /**
484: * Insert the method's description here. Creation date: (12/12/2001
485: * 10:01:55 AM)
486: *
487: *@param newName java.lang.String
488: */
489: public void setMetricName(String newName) {
490: metricName = newName;
491: }
492:
493: public void setMetricNameFailed() {
494: metricName += "-failed";
495: }
496:
497: /**
498: * Insert the method's description here. Creation date: (12/12/2001 9:36:14
499: * AM)
500: *
501: *@param newStartTime java.lang.Long
502: */
503: public void setStartTime(long newStartTime) {
504: startTime = newStartTime;
505: }
506:
507: /**
508: * Insert the method's description here. Creation date: (12/12/2001 9:50:37
509: * AM)
510: *
511: *@param newPauseElapsedTime long
512: */
513: protected void setPauseElapsedTime(long newPauseElapsedTime) {
514: pauseElapsedTime = newPauseElapsedTime;
515: }
516:
517: /**
518: * Insert the method's description here. Creation date: (12/12/2001 9:49:46
519: * AM)
520: *
521: *@param newPauseStartTime long
522: */
523: protected void setPauseStartTime(long newPauseStartTime) {
524: pauseStartTime = newPauseStartTime;
525: }
526:
527: /**
528: * Returns the isDebugEnabled.
529: * @return boolean
530: */
531: protected boolean isDebugEnabled() {
532: return isDebugEnabled;
533: }
534:
535: /**
536: * Sets the isDebugEnabled.
537: * @param isDebugEnabled The isDebugEnabled to set
538: */
539: public void setIsDebugEnabled(boolean isDebugEnabled) {
540: this.isDebugEnabled = isDebugEnabled;
541: }
542:
543: }
|