
it.tidalwave.stopwatch.StopWatch Maven / Gradle / Ivy
/*
* #%L
* *********************************************************************************************************************
*
* These Foolish Things - Miscellaneous utilities
* http://thesefoolishthings.java.net - hg clone https://bitbucket.org/tidalwave/thesefoolishthings-src
* %%
* Copyright (C) 2009 - 2013 Tidalwave s.a.s. (http://tidalwave.it)
* %%
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* $Id: StopWatch.java,v e7e97a14be7c 2013/02/16 19:44:07 fabrizio $
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.stopwatch;
import java.util.Stack;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: StopWatch.java,v e7e97a14be7c 2013/02/16 19:44:07 fabrizio $
* @experimental
*
**********************************************************************************************************************/
public class StopWatch
{
private StopWatchStats stats;
private long startTime;
private long partialStartTime;
private long accumulated;
private final static ThreadLocal> stack = new ThreadLocal>()
{
@Override
protected Stack initialValue()
{
return new Stack();
}
};
public static final StopWatch DUMMY_STOPWATCH = new StopWatch()
{
@Override
public void cancel()
{
}
@Override
public void stop()
{
}
};
public static StopWatch create (final Class> clazz, final String name)
{
return new StopWatch(clazz, name);
}
protected StopWatch()
{
partialStartTime = 0;
}
protected StopWatch (final Class> clazz, final String name)
{
final String baseName = clazz.getName().replace('.', '/') + "/" + name;
// Create here the reference to stats, in order to avoid overhead in stop()
stats = StopWatchStats.find(baseName);
final Stack currentStack = stack.get();
if (!currentStack.isEmpty())
{
currentStack.peek().suspend();
}
currentStack.push(this);
startTime = partialStartTime = System.nanoTime();
}
public void stop()
{
final long now = System.nanoTime();
stats.addTimeSample(now - partialStartTime + accumulated, now - startTime);
cancel();
}
public void cancel()
{
stats = null;
final Stack currentStack = stack.get();
currentStack.pop();
if (!currentStack.isEmpty())
{
currentStack.peek().resume();
}
}
protected void suspend()
{
accumulated += System.nanoTime() - partialStartTime;
}
protected void resume()
{
partialStartTime = System.nanoTime();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy