Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

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

  • Testdata preperation
  • Executing businesslogic
  • assert results

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

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...

like image 561
s_bei Avatar asked Feb 15 '13 09:02

s_bei


People also ask

Which function is used in JUnit to compare expected and actual result?

You use an assert method, provided by JUnit or another assert framework, to check an expected result versus the actual result. These method calls are typically called asserts or assert statements.

Which time unit is provided in time test?

JUnit provides a handy option of Timeout. If a test case takes more time than the specified number of milliseconds, then JUnit will automatically mark it as failed.

What is the class used to run the JUnit test in GUI?

The central class of JUnit is TestCase, which represents a fixture that provides the framework to run unit tests and store the results.


1 Answers

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"); 
like image 124
Simulant Avatar answered Sep 20 '22 09:09

Simulant