Setup and TearDown executed by different threads
vyazelenko at yahoo.com
vyazelenko at yahoo.com
Wed Aug 13 15:43:01 UTC 2014
> On Aug 13, 2014, at 16:54, Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:
>
>> 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.
Ok, my comparison of JUnit annotation and JMH was simplistic to say the least. But one thing out if JUnit seems to be valuable - notion of the main thread.
What if JMH would simply have a main thread that would be used to execute @Setup/@TearDown calls on @State(Scope.Benchmark)?
Then you won't need to track callers, because there will be only one caller.
Having such thread will basically make @Setup(Level.Iteration) like @BeforeClass in Junit and @Setup(Level.Iteration) like @Before.
One thing I'm not sure about is @Setup(Level.Invocation). Logically for Scope.Benchmark this method should also be called by main thread. But that would mean that threads would ping pong, i.e. setup in one thread and immediate execution in another.
This is my view on the issue. But I'm definitely missing all the JMH-internal context to know better.
>
> Thanks,
> -Aleksey.
>
Aleksey, thanks for looking into it. :)
Regards,
Dmitry
More information about the jmh-dev
mailing list