Setup and TearDown executed by different threads

Dmitry Vyazelenko vyazelenko at yahoo.com
Wed Aug 13 12:40:20 UTC 2014



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? And what about "executed by the same thread" part?
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.

> > And this work should be done by the same
> > thread. In my case there is a session attached via ThreadLocal in
> > @Setup.

> Thinking out loud now.

> Scope.Benchmark assumes the state is shared between the threads, which
> forces users to do additional housekeeping when accessed from multiple
> threads. My concern is that tracking the @Setup/@TearDown callers would
> blur this for users: instead of blowing up early, users will experience
> weird visibility problems caused by the lack of appropriate
> synchronization.

> I think you can do something like that with:
http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_29_StatesDAG.java

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

P.S. I made a test using JMHSample_29_StatesDAG approach:
@Fork(1) 
@Warmup(iterations = 0) 
@Measurement(iterations = 1) 
@State(Scope.Thread) 
@BenchmarkMode(Mode.SingleShotTime) 
public class SetupAndTearDownThreadBenchmarkScope { 
    @State(Scope.Benchmark) 
    public static class BenchmarkStateHolder { 
        private void printThread(String msg) { 
            System.out.println("\r\n[" + Thread.currentThread().getName() + "] => " + msg); 
        } 
        @Setup(Level.Trial) 
        public void setUpTrial() { 
            printThread("setUpTrial()"); 
        } 
        @TearDown(Level.Trial) 
        public void tearDownTrial() { 
            printThread("tearDownTrial()"); 
        } 
        @Setup(Level.Iteration) 
        public void setUpIteration() { 
            printThread("setUpIteration()"); 
        } 
        @TearDown(Level.Iteration) 
        public void tearDownIteration() { 
            printThread("tearDownIteration()"); 
        } 
        @Setup(Level.Invocation) 
        public void setUpInvocation() { 
            printThread("setUpInvocation()"); 
        } 
        @TearDown(Level.Invocation) 
        public void tearDownInvocation() { 
            printThread("tearDownInvocation()"); 
        } 
    } 
    @Benchmark 
    public void myBench(BenchmarkStateHolder holder) { 
        System.out.println(holder); 
    } 
} 

Here is the output:
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-3] => setUpTrial() 
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-1] => setUpIteration() 
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-1] => setUpInvocation() 
org.sample.generated.SetupAndTearDownThreadBenchmarkScope_myBench$BenchmarkStateHolder_1_jmh at 38cc4364 
org.sample.generated.SetupAndTearDownThreadBenchmarkScope_myBench$BenchmarkStateHolder_1_jmh at 38cc4364 
org.sample.generated.SetupAndTearDownThreadBenchmarkScope_myBench$BenchmarkStateHolder_1_jmh at 38cc4364 
org.sample.generated.SetupAndTearDownThreadBenchmarkScope_myBench$BenchmarkStateHolder_1_jmh at 38cc4364 
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-4] => tearDownInvocation() 
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-3] => tearDownIteration() 
[org.sample.SetupAndTearDownThreadBenchmarkScope.myBench-jmh-worker-4] => tearDownTrial()





> -Aleksey.


Best regards,
Dmitry


More information about the jmh-dev mailing list