micro benchmark hell

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

micro benchmark hell

studdugie
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
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: micro benchmark hell

Kasper Nielsen-2
  > 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

Hi Dane,

Lets say:

Thread1-Thread8 uses 1 s each on Processor 1
Thread9-Thread16 uses 1 s each on Processor 2

That's 16 seconds in total. But only 8 seconds in wall clock time
(excluding time spent on starting/joining threads)

- Kasper


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: micro benchmark hell

studdugie
Thank you Kasper. It just clicked.

New rule! No coding past 2AM.

On 9/19/06, Kasper Nielsen <[hidden email]> wrote:

>   > 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
>
> Hi Dane,
>
> Lets say:
>
> Thread1-Thread8 uses 1 s each on Processor 1
> Thread9-Thread16 uses 1 s each on Processor 2
>
> That's 16 seconds in total. But only 8 seconds in wall clock time
> (excluding time spent on starting/joining threads)
>
> - Kasper
>
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest