秒表类显示最小值,最大值,平均值,......次?

时间:2011-03-11 12:47:44

标签: java timer stopwatch

我正在寻找一个模拟StopWatch的java类(如Spring's StopWatchCommons' StopWatch),但它会给出最小,最大和平均时间。也优选在最后n次运行中的平均值。

有这样的事情还是我自己建造它?

的问候,

维姆

2 个答案:

答案 0 :(得分:4)

我认为你应该分开这两个概念:

  • 负责计时的一个班级(例如,使用start()/ stopped()/ elapsedMillis()方法)
  • 一个负责根据数据生成有趣统计数据的班级

这两个是非常独立的东西,IMO - 并且更容易单独测试而不是一起测试。您可能能够为每个单独的组件找到第三方库 - 或者可能使用现有的StopWatch类,但编写自己的统计信息器。

答案 1 :(得分:3)

追随Jon Skeet的回答,这就是我所提出的,以防其他人想要它。它使用Spring框架中的StopWatch类:

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;
    }
}
}

如果你运行stopWatch.prettyPrint(),它看起来像这样:

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