5

我正在寻找一个模拟 StopWatch 的 java 类(如Spring 的 StopWatchCommons 的 StopWatch),但这会给出最小、最大和平均时间。最好也是过去 n 次运行的平均值。

有这样的东西还是我自己建造的?

问候,

维姆

4

2 回答 2

4

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

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

这两者真的是非常独立的东西,IMO - 单独测试肯定比一起测试更容易。您很可能能够为每个单独的组件找到第三方库 - 或者可能使用现有的StopWatch类,但编写您自己的统计数据处理程序。

于 2011-03-11T12:50:48.383 回答
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
于 2011-03-11T13:40:13.297 回答