硬體描述:
model name : Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz
cpu core : 4
cache size : 15360 KB
OS : Linux 64bit
JDK : 1.7.0_21-b11
Disruptor 3.2.1:
ringBufferSize : 1024
producerType : ProducerType.MULTI
WaitStrategy : BlockingWaitStrategy
測試代碼(dtest.zip common-disruptor):
| 代碼如下 |
複製代碼 |
package org.chinasb.test; import java.util.concurrent.CountDownLatch; import org.chinasb.common.disruptor.Event; import org.chinasb.common.disruptor.dispatch.Dispatcher; import org.chinasb.common.disruptor.dispatch.RingbufferDispatcher; public class Test { static long LOGIC_LOOP = 0; static long COST = 0; static int NUM = 5000000; public static void main(String[] args) { boolean multithread = false; int bufferSize = 1024; if (args.length > 0) { if (args.length > 0) { NUM = Integer.parseInt(args[0]); } if (args.length > 1) { multithread = Boolean.parseBoolean(args[1]); } if (args.length > 2) { bufferSize = Integer.parseInt(args[2]); } if (args.length > 3) { LOGIC_LOOP = Long.parseLong(args[3]); } } final Dispatcher dispatcher = new RingbufferDispatcher(multithread, "任務執行器", bufferSize); long start = System.nanoTime(); final CountDownLatch countDownLatch = new CountDownLatch(NUM); final int coreSize = Runtime.getRuntime().availableProcessors(); for (int i = 0; i < coreSize; i++) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < NUM / coreSize; i++) { dispatcher.dispatch(Event.wrap(new Runnable() { @Override public void run() { // logic long start = System.nanoTime(); for (long i = 0; i < LOGIC_LOOP; i++) {} double elapsed = (System.nanoTime() - start) / 1000.0; COST+=elapsed; countDownLatch.countDown(); } })); } } }).start(); } try { countDownLatch.await(); double elapsed = (System.nanoTime() - start) / 1000000.0; long throughput = (long) (NUM / (elapsed / 1000.0)); System.out.println("throughput: " + throughput + "/sec in " + (long) elapsed + "ms"); System.out.println("average logic: " + COST / NUM + "us"); } catch (InterruptedException e) { e.printStackTrace(); } dispatcher.shutdown(); } } |
測試1描述:多生產者模式下單個消費者事件消費能力(無耗時商務邏輯,僅測試事件分發吞吐能力),每種類型操作分別測試3次。
測試1結果:
測試2描述:多生產者模式下單個消費者事件消費能力(有耗時商務邏輯,每個事件平均耗時1.8ms,測試事件分發處理吞吐能力),每種類型操作分別測試3次。
測試2結果:
總結:測試1情境中當事件數目量超過1000w,其事件分發吞吐能力穩定在300w左右,測試2情境中在兩種不同事件數目量類型的分發處事過程中,其事件分發處理吞吐能力穩定在540左右。
按照這個測試結果,如果在遊戲中引入Disruptor替代傳統的BlockQueue處理訊息事件,承載數量應該可以提高3倍左右。