LMAX Disruptor SPSC - 每秒600万次操作

时间:2013-12-01 18:42:13

标签: message-queue disruptor-pattern

使用Disruptor环形缓冲区,我每秒只能实现600万次操作。我想知道我哪里出错了。我的事件处理程序只是一个计数器。这是单生产者和单一消费者。有人可以告诉我,如果我对语义本身有误。该程序创建一个生成器线程,该线程将添加到缓冲区。并创建一个事件处理程序来处理发布事件。每次发布事件时,事件处理程序都会增加一个易失性计数器。

public class MainClass{

   public static class globalVariables
   {
        static int NUMBER_OF_ITERATIONS = 33554432; // 2 power 25     
        static int NUMBER_OF_THREADS;
        static int RING_SIZE = NUMBER_OF_ITERATIONS;
        static int WRITE_CODE = 1;

        static volatile int keep_going = 1; 
   };

   public void start_execution()
   {            
        int remainder = globalVariables.NUMBER_OF_ITERATIONS % globalVariables.NUMBER_OF_THREADS;               
        int iterations_per_thread = ( globalVariables.NUMBER_OF_ITERATIONS - remainder ) / globalVariables.NUMBER_OF_THREADS ;


      /* New Shared Object */
      final sharedObject newSharedObject = new sharedObject();


    ExecutorService exec = Executors.newFixedThreadPool(1);
      Disruptor<valueEvent> disruptor = new Disruptor<valueEvent>( valueEvent.EVENT_FACTORY, globalVariables.RING_SIZE, exec );


      /* Creating event handler whenever an item is published in the queue */

           final EventHandler<valueEvent> handler = new EventHandler<valueEvent>()
           {
              public void onEvent(final valueEvent event, final long sequence, 
                          final boolean endOfBatch) throws Exception
              {         
                 newSharedObject.shared_variable++; // increment the shared variable
              }
           };


          /* Use the above handler to handler events */
          disruptor.handleEventsWith(handler);


     /* start Disruptor */
     final RingBuffer<valueEvent> ringBuffer = disruptor.start();


     final long[] runtime = new long [globalVariables.NUMBER_OF_THREADS];
     /* Code the producer thread */
     final class ProducerThread extends Thread {
        int i;

        public ProducerThread( int i )
        {
            this.i = i;
        }

        public void run()
        {
           long idle_counter = 0;
           long count;

           System.out.println("In thread "+i );

           long startTime = System.nanoTime();

           //while( globalVariables.keep_going == 1 )
           for( int counter=0; counter<globalVariables.NUMBER_OF_ITERATIONS; counter++ )
           {
              // Publishers claim events in sequence
              long sequence = ringBuffer.next();
              valueEvent event = ringBuffer.get(sequence);

              event.setValue(globalVariables.WRITE_CODE); 

              // make the event available to EventProcessors
              ringBuffer.publish(sequence);  
           }

           long stopTime = System.nanoTime();
           runtime[i] = (stopTime - startTime)/1000; 
        }
     };

     /* ------------------------------------------------------------------------------- */     
     //final class AlarmHandler extends TimerTask {     
          /*** Implements TimerTask's abstract run method.   */
    //    @Override public void run(){
    //      globalVariables.keep_going = 0;
    //    }
   //  };

     /* ------------------------------------------------------------------------------- */
     /* Creating Producer threads */
     ProducerThread[] threads = new ProducerThread[globalVariables.NUMBER_OF_THREADS];
     for (int i = 0; i < globalVariables.NUMBER_OF_THREADS; i++) {
        threads[i] = new ProducerThread( i );
        threads[i].start();
     }

     // Waiting for the threads to finish
     for (int i = 0; i < globalVariables.NUMBER_OF_THREADS; i++) {
        try
        {
         threads[i].join();
        } catch ( InterruptedException e ) { System.out.println("hi exception :)"); }
     } 

     /* shutdown */     
     disruptor.shutdown();
     exec.shutdown();

     /* Printing Statistics */
     System.out.println( "Shared Variable: " + newSharedObject.shared_variable );
     for ( int i=0; i<globalVariables.NUMBER_OF_THREADS; i++ )
     {
         System.out.println("Runtime="+ runtime[i] + "; Operations per second = " + (globalVariables.NUMBER_OF_ITERATIONS  / runtime[i] )*1000000 +"ops/sec" );         
     }     

   }

    public static void main( String args[] )
    {
        globalVariables.NUMBER_OF_THREADS = Integer.parseInt( args[0] );    

        System.out.println( "Number of Threads = "+ globalVariables.NUMBER_OF_THREADS );

        MainClass mainObj = new MainClass();
        mainObj.start_execution();
        System.exit(0);
    }
};

这是程序的输出

共享变量:33554432; 运行时间= 5094139微秒;每秒操作数= 6000000

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

由于您在单个线程中运行事件处理程序,并且不共享该状态,因此通过使事件处理程序在非易失性字段上工作,您应该获得明显更好的性能(但仍然是正确的函数)。破坏程序确保您的处理程序一次只处理一个事件,因此您不必担心丢失增量。

如果系统中的另一个组件依赖于以特定顺序出现的此值(例如:它是一个控制值),那么您应该考虑使用类似AtomicInteger和lazySet [1]的东西。

[1] http://psy-lob-saw.blogspot.com.au/2012/12/atomiclazyset-is-performance-win-for.html