Package com.commsen.stopwatch.engines

Source Code of com.commsen.stopwatch.engines.MemoryStopwatchEngine

/*
* $Id: MemoryStopwatchEngine.java,v 1.2 2006/03/06 11:30:53 azzazzel Exp $
*
* Copyright 2006 Commsen International
*
* Licensed under the Common Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.opensource.org/licenses/cpl1.0.txt
*
* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
* OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package com.commsen.stopwatch.engines;

import org.apache.log4j.Logger;

import com.commsen.stopwatch.storages.MemoryHSQLInMemoryStorage;


/**
* This class extends default Stopwatch's engine to provide information about memory usage.
* <b>Note: the memory usage measurements performed by this class are FAR FROM ACCURATE.</b>
*
* <p>As of now I'm not aware of how one can measure the actual amount of memory given object uses.
* This class simply remembers the amount of memory used by JVM at the time of starting the measurement
* and compares it to the amount of memory used by JVM at the time of completing the measurement.
* This of course may have nothing to do the truth if some other threads are running and consuming memory
* or for example GC is started while measuring.</p>
*
* <p>So the results may vary from "almost correct" in the case of single threaded application
* to "pure fiction" in case of heavily loaded, multi threaded application</p>
*
* <p>To use MemoryStopwatchEngine with Stopwatch one can :
* <ul>
*   <li>run application with <code>-Dcom.commsen.stopwatch.engine=com.commsen.stopwatch.engines.MemoryStopwatchEngine</code> JVM parameter</li>
*   <li>create "stopwatch.properties" file on classpath and set <code>engine=com.commsen.stopwatch.engines.MemoryStopwatchEngine</code></li>
* </ul>
* </p> 
*
* @author Milen Dyankov
*/
public class MemoryStopwatchEngine extends DefaultStopwatchEngine {

  /**
   * Logger for this class
   */
  private static final Logger log = Logger.getLogger(MemoryStopwatchEngine.class);
 
  private static Runtime runtime = Runtime.getRuntime();

 

  /**
   *
   */
  public MemoryStopwatchEngine() {
    setStorage(new MemoryHSQLInMemoryStorage());
  }


  /**
   * @see com.commsen.stopwatch.engines.DefaultStopwatchEngine#end(long)
   */
  public void end(long id) {
    // first of all get the time
    Long timestamp = new Long(System.currentTimeMillis());
    getStorageManager().completeRecord(id, new Object[] {timestamp, usedMemory()});
  }

  /**
   * @see com.commsen.stopwatch.engines.DefaultStopwatchEngine#begin(java.lang.String, java.lang.String)
   */
  public long begin(String group, String label) {
    // first of all get the time
    Long timestamp = new Long(System.currentTimeMillis());
    return getStorageManager().newRecord(new Object[]{group, label, timestamp, usedMemory()});   
  }

 
  private Long usedMemory() {
    return new Long(runtime.totalMemory() - runtime.freeMemory());
  }

  protected Logger getLogger () {
    return log;
  }

}
TOP

Related Classes of com.commsen.stopwatch.engines.MemoryStopwatchEngine

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.