Excluding @Before/@After execution time

144 views
Skip to first unread message

Jakub Narloch

unread,
Aug 5, 2014, 10:58:30 AM8/5/14
to junit-be...@googlegroups.com
I might have quite interesting, but probably not entirely uncommon use case, 

We are using the JUnitBenchmarks to profile the data access layer and in order to do so we have a huge production data sql dump that is being used for populating the tables in the @Before method. 
This done once and only once for the entire test case. Unfortunately the time taken for the @Before/@After methods is apparently taken into account for each individual test, causing in our case to produce huge spike for the first test that executes. In the contrary all the subsequent tests takes milliseconds to execute in each individual round.

Now, I know how the JUnit Rules work, but can you think of any workaround to output only the time of the actual test method, excluding the time that is spent for setup? We have some integration/setup code that prevents from moving the initialization into @BeforeClass.

Dawid Weiss

unread,
Aug 6, 2014, 3:24:41 AM8/6/14
to junit-benchmarks
If you want to have per-test before/after code that is excluded from
benchmarking (and executed only once), use a rule chain and place
benchmarking rule at the bottom of the chain.

https://github.com/carrotsearch/junit-benchmarks/blob/02bb71e77e90485041c12d47fbad02852b38b956/src/test/java/com/carrotsearch/junitbenchmarks/examples/BeforeAfterChaining.java#L61

There is no other way to do it that wouldn't be hacky or crippled.
Before/after hooks shouldn't have ordering dependencies; if they do
(as it's the case here because benchmarking itself is a rule), you
need to order them manually as shown above.

D.
> --
> You received this message because you are subscribed to the Google Groups
> "JUnitBenchmarks: Performance Benchmarking for JUnit4" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to junit-benchmar...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages