开发者

Stopwatch class that shows min, max, average, ... times?

开发者 https://www.devze.com 2023-02-16 18:29 出处:网络
I am lo开发者_StackOverflow社区oking for a java class that simulates a StopWatch (like Spring\'s StopWatch or Commons\' StopWatch) but that would give minimum, maximum and average times. Preferable al

I am lo开发者_StackOverflow社区oking for a java class that simulates a StopWatch (like Spring's StopWatch or Commons' StopWatch) but that would give minimum, maximum and average times. Preferable also average over last n runs as well.

Is there such a thing or do I just build it myself?

regards,

Wim


I think you should split up the two concepts:

  • One class responsible for the timing (e.g. with start()/stopped()/elapsedMillis() methods)
  • One class responsible for producing interesting stats based on data

The two are really pretty separate things, IMO - and definitely easier to test separately than together. You may well be able to find third party libraries for each separate component - or perhaps use an existing StopWatch class, but write your own stats cruncher.


Following up to what Jon Skeet answered, this is what I have came up wiht in case somebody else wants it. It uses the StopWatch class from the Spring framework:

public class StopWatch extends org.springframework.util.StopWatch
{
private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
// ------------------------------ FIELDS ------------------------------

org.springframework.util.StopWatch.TaskInfo m_minimumTimeTask = null;
org.springframework.util.StopWatch.TaskInfo m_maximumTimeTask = null;
private final String m_id;

// --------------------------- CONSTRUCTORS ---------------------------

public StopWatch()
{
    this( "" );
}

public StopWatch( String id )
{
    super( id );
    m_id = id;
}

@Override
public void setKeepTaskList( boolean keepTaskList )
{
    throw new UnsupportedOperationException( "The task list is always kept to be able to calculate the min, max and average" );
}
// -------------------------- PUBLIC METHODS --------------------------

public long getMinimumTimeMillis()
{
    if (m_minimumTimeTask != null)
    {
        return m_minimumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}

public long getMaximumTimeMillis()
{
    if (m_maximumTimeTask != null)
    {
        return m_maximumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}


public void stop() throws IllegalStateException
{
    super.stop();
    updateMinimumTime();
    updateMaximumTime();
}


public String shortSummary()
{
    StringBuilder builder = new StringBuilder();
    builder.append( "StopWatch '" ).append( m_id )
            .append( "': running time (millis) = " ).append( getTotalTimeMillis() );

    if (getTaskCount() > 0)
    {
        builder.append( LINE_SEPARATOR ).append( "-----------------------------------------" ).append( LINE_SEPARATOR );
        builder.append( "min: " ).append( m_minimumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_minimumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "max: " ).append( m_maximumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_maximumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "avg: " ).append( getAverageTimeMillis() ).append( " ms" );
    }
    return builder.toString();
}

// -------------------------- PRIVATE METHODS --------------------------

private void updateMinimumTime()
{
    if (m_minimumTimeTask == null)
    {
        m_minimumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() < m_minimumTimeTask.getTimeMillis())
        {
            m_minimumTimeTask = getLastTaskInfo();
        }
    }
}

private void updateMaximumTime()
{
    if (m_maximumTimeTask == null)
    {
        m_maximumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() > m_maximumTimeTask.getTimeMillis())
        {
            m_maximumTimeTask = getLastTaskInfo();
        }
    }
}

public long getAverageTimeMillis()
{
    if( getTaskCount() > 0)
    {
        return getTotalTimeMillis() / getTaskCount();
    }
    else
    {
        return -1L;
    }
}
}

If you run stopWatch.prettyPrint(), it look like this:

StopWatch 'TestMinMaxAndAverage': running time (millis) = 1100
-----------------------------------------
min: 100 ms (run3)
max: 500 ms (run4)
avg: 275 ms
-----------------------------------------
ms     %     Task name
-----------------------------------------
00200  018%  run1
00300  027%  run2
00100  009%  run3
00500  045%  run4
0

精彩评论

暂无评论...
验证码 换一张
取 消