确定我的 junit 的业务相关代码的执行时间的最佳做法是什么? [英] What is the best practice to determine the execution time of the business relevant code of my junit?

查看:6
本文介绍了确定我的 junit 的业务相关代码的执行时间的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

eclipse->junit-view 中显示的测试执行时间取决于整个测试用例的执行,包括:

The test execution time which is displayed in eclipse->junit-view depends on the whole test case execution which includes:

  • 测试数据准备
  • 执行业务逻辑
  • 断言结果

我需要更详细的关于我的业务逻辑的执行时间的声明,并且只需要我的业务逻辑.这就是我在测试用例中所做的:

I need a more detailed statement about the execution time of my businesslogic and only my businesslogic. That is what I have done inside my testcase:

Date lNow = new Date();
List<Geo> lAllBasisGeo = mGeoEvaluator.evaluateAllGeo(lGeoFixture.getGeo(), lAllGeo);
Date lStop = new Date();
System.out.println("Time of execution in seconds:"+((lStop.getTime()-lNow.getTime())/1000));

嗯...我想我以一种非常尴尬的方式确定时间.此外,我认为没有必要声明两个 Date 变量.

Well... I think I determine the time in a realy awkward way. Furthermore I dont think that it is necessary to declare two Date variables.

我需要更有效地编写代码的建议...

I need advice writing that code more efficiently...

推荐答案

在单元测试中,我更喜欢使用 JUnit 4 注释向测试添加超时,以确定测试是否通过(足够快):

in a unit test I would prefer to add a timeout to the Test with a JUnit 4 annotation, to determine whether the test passes (fast enough) or not:

@Test(timeout=100)//let the test fail after 100 MilliSeconds
public void infinity() {
  while(true);
}

为了确定您的业务逻辑的确切运行时,我会像您一样在关键代码路径之前和之后添加 Time 语句,重复几次以获得学术上正确的结果,然后再次删除这些语句,从而精简代码.

To determine the exact Runtime of your business logic I would add the Time statements right before and after your critical Codepath like you did, repeat it several times to get scholastically correct results, and remove the statements again, so slim the code.

long start = System.currentTimeMillis();
//execute logic in between
long end = System.currentTimeMillis();
System.out.println("DEBUG: Logic A took " + (end - start) + " MilliSeconds");

这篇关于确定我的 junit 的业务相关代码的执行时间的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆