Author: nicolas Date: Tue Jan 6 07:17:41 2009 New Revision: 731969 URL: http://svn.apache.org/viewvc?rev=731969&view=rev Log: a simplier stopwatch that only computes elapsed time
Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java Modified: commons/sandbox/monitoring/branches/modules/core/ (props changed) commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java Propchange: commons/sandbox/monitoring/branches/modules/core/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Tue Jan 6 07:17:41 2009 @@ -0,0 +1,4 @@ +.settings +target +.classpath +.project Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java?rev=731969&r1=731968&r2=731969&view=diff ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java (original) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java Tue Jan 6 07:17:41 2009 @@ -46,9 +46,9 @@ } @Override - protected void doStart( Monitor monitor ) + protected void doStart() { - super.doStart( monitor ); + super.doStart(); ThreadMXBean mx = ManagementFactory.getThreadMXBean(); if ( mx.isCurrentThreadCpuTimeSupported() ) { Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java?rev=731969&r1=731968&r2=731969&view=diff ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java (original) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java Tue Jan 6 07:17:41 2009 @@ -5,32 +5,14 @@ import org.apache.commons.monitoring.Unit; /** - * Simple implementation of StopWatch that estimate monitored element execution time. + * Implementation of StopWatch that maintains a Gauge of concurrent threads accessing the monitored resource. * * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> */ -public class DefaultStopWatch +public class DefaultStopWatch extends SimpleStopWatch implements StopWatch { - /** Time the probe was started */ - protected final long startedAt; - - /** Time the probe was stopped */ - private long stopedAt; - - /** Time the probe was paused */ - private long pauseDelay; - - /** flag for stopped probe */ - private boolean stoped; - - /** flag for paused probe */ - private boolean paused; - - /** Monitor that is notified of process execution state */ - protected final Monitor monitor; - /** * Constructor. * <p> @@ -40,213 +22,25 @@ */ public DefaultStopWatch( Monitor monitor ) { - super(); - this.monitor = monitor; - startedAt = nanotime(); - doStart( monitor ); + super( monitor ); + doStart(); } - protected void doStart( Monitor monitor ) + protected void doStart() { monitor.getGauge( Monitor.CONCURRENCY ).increment( Unit.UNARY ); } - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#getElapsedTime() - */ - public long getElapsedTime() - { - if ( stoped || paused ) - { - return stopedAt - startedAt - pauseDelay; - } - else - { - // Still running ! - return nanotime() - startedAt - pauseDelay; - } - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#pause() - */ - public void pause() - { - if ( !paused && !stoped ) - { - stopedAt = nanotime(); - paused = true; - } - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#resume() - */ - public void resume() - { - if ( paused && !stoped ) - { - pauseDelay = nanotime() - stopedAt; - paused = false; - stopedAt = 0; - } - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#stop() - */ - public void stop() - { - if ( !stoped ) - { - long t = nanotime(); - if ( paused ) - { - pauseDelay = t - stopedAt; - } - stopedAt = t; - stoped = true; - doStop(); - } - } - protected void doStop() { + super.doStop(); monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY ); - monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), Unit.NANOS ); } - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#stop(boolean) - */ - public void stop( boolean canceled ) - { - if ( canceled ) - { - cancel(); - } - else - { - stop(); - } - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#cancel() - */ - public void cancel() - { - if ( !stoped ) - { - stoped = true; - doCancel(); - } - } protected void doCancel() { monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY ); } - /** - * {...@inheritdoc} - * <p> - * Monitored application should use a <code>try/finally</code> block to ensure on of {...@link #stop()} or - * {...@link #cancel()} method is invoked, even when an exception occurs. To avoid StopWatches to keep running if the - * application didn't follow this recommendation, the finalizer is used to cancel the StopWatch and will log a - * educational warning. - * - * @see java.lang.Object#finalize() - */ - protected void finalize() - { - // This probe is reclaimed by garbage-collector and still running, - // the monitored code "forgot" to stop/cancel it properly. - if ( !stoped && ( monitor != null ) ) - { - System.err.println( "WARNING : Execution for " + monitor.getKey().toString() + - " was not stoped properly. " + "This can result in wrong concurrency monitoring. " + - "Use try/finally blocks to avoid this warning" ); - } - } - - /** - * Returns the current value of the most precise available system timer, in nanoseconds. The real precision depends - * on the JVM and the underlying system. On JRE before java5, <tt>backport-util-concurrent</tt> provides some - * limited support for equivalent timer. - * - * @see System#nanoTime() - * @return time in nanosecond - */ - protected long nanotime() - { - return System.nanoTime(); - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#isStoped() - */ - public boolean isStoped() - { - return stoped; - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#isPaused() - */ - public boolean isPaused() - { - return paused; - } - - @Override - public String toString() - { - StringBuffer stb = new StringBuffer(); - if ( monitor != null ) - { - stb.append( "Execution for " ).append( monitor.getKey().toString() ).append( " " ); - } - if ( paused ) - { - stb.append( "paused after " ).append( getElapsedTime() ).append( "ns" ); - } - else if ( stoped ) - { - stb.append( "stoped after " ).append( getElapsedTime() ).append( "ns" ); - } - else - { - stb.append( "running for " ).append( getElapsedTime() ).append( "ns" ); - } - return stb.toString(); - - } - - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.StopWatch#getMonitor() - */ - public Monitor getMonitor() - { - return monitor; - } - } \ No newline at end of file Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java?rev=731969&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java (added) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java Tue Jan 6 07:17:41 2009 @@ -0,0 +1,250 @@ +package org.apache.commons.monitoring.stopwatches; + +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Unit; + +/** + * Simple implementation of StopWatch that estimate monitored element execution time. + * + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public class SimpleStopWatch +{ + /** Monitor that is notified of process execution state */ + public final Monitor monitor; + + /** Time the probe was started */ + protected final long startedAt; + + /** Time the probe was stopped */ + protected long stopedAt; + + /** Time the probe was paused */ + protected long pauseDelay; + + /** flag for stopped probe */ + protected boolean stoped; + + /** flag for paused probe */ + protected boolean paused; + + /** + * Constructor. + * <p> + * The monitor can be set to null to use the StopWatch without the monitoring infrastructure. + * + * @param monitor the monitor associated with the process to be monitored + */ + public SimpleStopWatch( Monitor monitor ) + { + super(); + this.monitor = monitor; + startedAt = nanotime(); + doStart(); + } + + /** + * To be overriden by subclasses to add some features on stopWath start + */ + protected void doStart() + { + + } + + /** + * Returns the current value of the most precise available system timer, in nanoseconds. The real precision depends + * on the JVM and the underlying system. On JRE before java5, <tt>backport-util-concurrent</tt> provides some + * limited support for equivalent timer. + * + * @see System#nanoTime() + * @return time in nanosecond + */ + protected long nanotime() + { + return System.nanoTime(); + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#getElapsedTime() + */ + public long getElapsedTime() + { + if ( stoped || paused ) + { + return stopedAt - startedAt - pauseDelay; + } + else + { + // Still running ! + return nanotime() - startedAt - pauseDelay; + } + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#pause() + */ + public void pause() + { + if ( !paused && !stoped ) + { + stopedAt = nanotime(); + paused = true; + } + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#resume() + */ + public void resume() + { + if ( paused && !stoped ) + { + pauseDelay = nanotime() - stopedAt; + paused = false; + stopedAt = 0; + } + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#stop() + */ + public void stop() + { + if ( !stoped ) + { + long t = nanotime(); + if ( paused ) + { + pauseDelay = t - stopedAt; + } + stopedAt = t; + stoped = true; + doStop(); + } + } + + protected void doStop() + { + monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), Unit.NANOS ); + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#stop(boolean) + */ + public void stop( boolean canceled ) + { + if ( canceled ) + { + cancel(); + } + else + { + stop(); + } + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#cancel() + */ + public void cancel() + { + if ( !stoped ) + { + stoped = true; + doCancel(); + } + } + + protected void doCancel() + { + + } + + /** + * {...@inheritdoc} + * <p> + * Monitored application should use a <code>try/finally</code> block to ensure on of {...@link #stop()} or + * {...@link #cancel()} method is invoked, even when an exception occurs. To avoid StopWatches to keep running if the + * application didn't follow this recommendation, the finalizer is used to cancel the StopWatch and will log a + * educational warning. + * + * @see java.lang.Object#finalize() + */ + protected void finalize() + { + // This probe is reclaimed by garbage-collector and still running, + // the monitored code "forgot" to stop/cancel it properly. + if ( !stoped && ( monitor != null ) ) + { + System.err.println( "WARNING : Execution for " + monitor.getKey().toString() + " was not stoped properly. " + + "This can result in wrong concurrency monitoring. " + "Use try/finally blocks to avoid this warning" ); + } + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#isStoped() + */ + public boolean isStoped() + { + return stoped; + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#isPaused() + */ + public boolean isPaused() + { + return paused; + } + + @Override + public String toString() + { + StringBuffer stb = new StringBuffer(); + if ( monitor != null ) + { + stb.append( "Execution for " ).append( monitor.getKey().toString() ).append( " " ); + } + if ( paused ) + { + stb.append( "paused after " ).append( getElapsedTime() ).append( "ns" ); + } + else if ( stoped ) + { + stb.append( "stoped after " ).append( getElapsedTime() ).append( "ns" ); + } + else + { + stb.append( "running for " ).append( getElapsedTime() ).append( "ns" ); + } + return stb.toString(); + + } + + /** + * {...@inheritdoc} + * + * @see org.apache.commons.monitoring.StopWatch#getMonitor() + */ + public Monitor getMonitor() + { + return monitor; + } + +} \ No newline at end of file