What is the best practice to determine the execution time of the business relevant code of my junit?

前端 未结 3 1855
心在旅途
心在旅途 2020-12-08 08:05

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

  • Testdata preperation
  • Execut
3条回答
  •  -上瘾入骨i
    2020-12-08 08:58

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

    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");
    

提交回复
热议问题