[concurrency-interest] micro benchmark hell

studdugie studdugie at gmail.com
Tue Sep 19 04:35:02 EDT 2006


Hello all. I've written a micro benchmark for a little pool class as a
JUnit testcase. I'm posting it next:

6    import com.tvtaxi.library.util.Nanos;
7    import com.tvtaxi.library.util.PrivateAccessor;
8    import junit.framework.TestCase;
9    import java.io.IOException;
10   import java.nio.channels.Selector;
11   import java.util.concurrent.BrokenBarrierException;
12   import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.CyclicBarrier;
14   import java.util.concurrent.atomic.AtomicLong;
15
16   public class TestSynchronizedSelectorPool extends TestCase
17   {
18       private static final int THREADS = 16;
19
20       public void testGet() throws Exception
21       {
22           final SynchronizedSelectorPool pool = new
SynchronizedSelectorPool();
23           final CountDownLatch start = new CountDownLatch( 1 );
24           final CyclicBarrier start2 = new CyclicBarrier( THREADS + 1 );
25           final AtomicLong sum = new AtomicLong();
26           final CountDownLatch fin = new CountDownLatch( THREADS );
27           for( int i = THREADS; i-- > 0; )
28           {
29               new Thread()
30               {
31                   public void run()
32                   {
33                       try
34                       {
35                           start2.await();
36                           long nanos = System.nanoTime();
37                           Selector selector = pool.get();
38                           assertTrue( sum.addAndGet(
System.nanoTime() - nanos ) > 0 );
39                           assertTrue( selector.isOpen() );
40                       }
41                       catch( BrokenBarrierException ex )
42                       {
43                           ex.printStackTrace();
44                       }
45                       catch( InterruptedException ignore )
46                       {
47                           ignore.printStackTrace();
48                       }
49                       catch( IOException e )
50                       {
51                           e.printStackTrace();
52                       }
53                       finally
54                       {
55                           fin.countDown();
56                       }
57                   }
58               }.start();
59           }
60           try
61           {
62               long nanos = System.nanoTime();
63               start2.await();
64               fin.await();
65               nanos = System.nanoTime() - nanos;
66               assertTrue( (Boolean)PrivateAccessor.getField( pool,
"full" ) );
67               assertEquals( 8, ((Integer)PrivateAccessor.getField(
pool, "slot" )).intValue() );
68               assertEquals( 8, ((Integer)PrivateAccessor.getField(
pool, "next" )).intValue() );
69               assertTrue( pool.get().isOpen() );
70               assertEquals( 1, ((Integer)PrivateAccessor.getField(
pool, "next" )).intValue() );
71               System.out.println( "Sum: " + new Nanos( sum.get() ) );
72               System.out.println( "Avg: " + new Nanos( sum.get() /
THREADS ) );
73               System.out.println( "Total time elapsed: " + new
Nanos( nanos ) );
74           }
75           finally
76           {
77               pool.close();
78           }
79       }
80   }

I'm running it on a 2 way Opterton w/ Linux kernel v2.6.17.11 w/ Sun
JVM 1.5.0_08.  The problem I'm having is if I execute it often enough
eventually "Sum" will be greater than "Total time elapsed". Which as
far as I'm concerned is supposed to be impossible.

Now it could be that because it's 4:30 AM and my brain is fried I'm
overlooking the obvious but I doubt it.  Any feedback would be greatly
appreciated.

Regards,

Dane Foster


More information about the Concurrency-interest mailing list