Setup and TearDown executed by different threads
Aleksey Shipilev
aleksey.shipilev at oracle.com
Wed Aug 13 14:54:24 UTC 2014
On 08/13/2014 04:40 PM, Dmitry Vyazelenko wrote:
> On Wednesday, August 13, 2014 1:49 PM, Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:
> On 08/13/2014 03:41 PM, vyazelenko at yahoo.com wrote:
>>> My use-case: start server in @Setup(Level.Trial) and shutdown server
>>> in @TearDown(Level.Trial). And of course I want to setup some data
>>> for entire benchmark and remove it at the end. In other words I'm
>>> looking for a way to perform per-Benchmark work irrespective of
>>> @State configuration.
>
>> Aha. @State(Scope.Benchmark) it is.
>
> So using @State(Scope.Benchmark) is the correct way to setup/tearDown
> for entire benchmark?
Yes.
> I mean I'm thinking in terms of @BeforeClass/@AfterClass from JUnit here.
> Which allow me to do setup/tearDown for all tests inside my class. Maybe
> something like that should be added to JMH. So that it is possible to use
> it when no @State is defined at all. Also JUnit calls @BeforeClass/@AfterClass
> from the same/main thread always.
JUnit has a simplistic view of test state to allow this. In JMH, you
have given up the control over the test state to the harness, and
therefore harness asks you about the state lifecycle with the help of
fixture methods. Everything else is presumed to be inaccessible and
dead. (Up to the point we would like to completely isolate accesses for
everything beyond explicit @State-s).
Suppose we have @BeforeClass. How would you initialize the
@State(Benchmark)? How would @BeforeClass have the access to any state
in the run? Push the reference to @State to @BeforeClass method like DAG
sample does? If so, how's that different from having the auxiliary
@State with fixture methods which reference your @State(Benchmark)? Or,
in simpler way, how's that different from defining the fixture methods
directly in @State(Benchmark)?
>> Thinking out loud now.
>
> Ca you be a bit more specific about JMHSample_29_StatesDAG example? How exactly
> it should help me achieve that @Setup/@TearDown are executed by the same thread?
> Or do you mean I should encapsulate @State(Scope.Benchmark)?
My thought was a straight-forward ripoff of JMHSample_29_StatesDAG
example. But that does not really work since every thread will fire the
@State(Thread) fixture method...
Let me see how messy it would be to track the @Setup/@TearDown callers.
Thanks,
-Aleksey.
More information about the jmh-dev
mailing list