01: /*
02: * $Id: MemoryStopwatchEngine.java,v 1.2 2006/03/06 11:30:53 azzazzel Exp $
03: *
04: * Copyright 2006 Commsen International
05: *
06: * Licensed under the Common Public License, Version 1.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.opensource.org/licenses/cpl1.0.txt
11: *
12: * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13: * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
14: * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15: *
16: */
17: package com.commsen.stopwatch.engines;
18:
19: import org.apache.log4j.Logger;
20:
21: import com.commsen.stopwatch.storages.MemoryHSQLInMemoryStorage;
22:
23: /**
24: * This class extends default Stopwatch's engine to provide information about memory usage.
25: * <b>Note: the memory usage measurements performed by this class are FAR FROM ACCURATE.</b>
26: *
27: * <p>As of now I'm not aware of how one can measure the actual amount of memory given object uses.
28: * This class simply remembers the amount of memory used by JVM at the time of starting the measurement
29: * and compares it to the amount of memory used by JVM at the time of completing the measurement.
30: * This of course may have nothing to do the truth if some other threads are running and consuming memory
31: * or for example GC is started while measuring.</p>
32: *
33: * <p>So the results may vary from "almost correct" in the case of single threaded application
34: * to "pure fiction" in case of heavily loaded, multi threaded application</p>
35: *
36: * <p>To use MemoryStopwatchEngine with Stopwatch one can :
37: * <ul>
38: * <li>run application with <code>-Dcom.commsen.stopwatch.engine=com.commsen.stopwatch.engines.MemoryStopwatchEngine</code> JVM parameter</li>
39: * <li>create "stopwatch.properties" file on classpath and set <code>engine=com.commsen.stopwatch.engines.MemoryStopwatchEngine</code></li>
40: * </ul>
41: * </p>
42: *
43: * @author Milen Dyankov
44: */
45: public class MemoryStopwatchEngine extends DefaultStopwatchEngine {
46:
47: /**
48: * Logger for this class
49: */
50: private static final Logger log = Logger
51: .getLogger(MemoryStopwatchEngine.class);
52:
53: private static Runtime runtime = Runtime.getRuntime();
54:
55: /**
56: *
57: */
58: public MemoryStopwatchEngine() {
59: setStorage(new MemoryHSQLInMemoryStorage());
60: }
61:
62: /**
63: * @see com.commsen.stopwatch.engines.DefaultStopwatchEngine#end(long)
64: */
65: public void end(long id) {
66: // first of all get the time
67: Long timestamp = new Long(System.currentTimeMillis());
68: getStorageManager().completeRecord(id,
69: new Object[] { timestamp, usedMemory() });
70: }
71:
72: /**
73: * @see com.commsen.stopwatch.engines.DefaultStopwatchEngine#begin(java.lang.String, java.lang.String)
74: */
75: public long begin(String group, String label) {
76: // first of all get the time
77: Long timestamp = new Long(System.currentTimeMillis());
78: return getStorageManager().newRecord(
79: new Object[] { group, label, timestamp, usedMemory() });
80: }
81:
82: private Long usedMemory() {
83: return new Long(runtime.totalMemory() - runtime.freeMemory());
84: }
85:
86: protected Logger getLogger() {
87: return log;
88: }
89:
90: }
|