java disruptor压测_Disruptor VS BlockingQueue

ArrayBlockingQueue与Disruptor进行压测

ArrayBlockingQueue4Test.java

package com.bfxy.disruptor.ability;

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueue4Test {

public static void main(String[] args) {

final ArrayBlockingQueue queue = new ArrayBlockingQueue(100000000);

final long startTime = System.currentTimeMillis();

//向容器中添加元素

new Thread(new Runnable() {

public void run() {

long i = 0;

while (i < Constants.EVENT_NUM_OHM) {

Data data = new Data(i, "c" + i);

try {

queue.put(data);

} catch (InterruptedException e) {

e.printStackTrace();

}

i++;

}

}

}).start();

new Thread(new Runnable() {

public void run() {

int k = 0;

while (k < Constants.EVENT_NUM_OHM) {

try {

queue.take();

} catch (InterruptedException e) {

e.printStackTrace();

}

k++;

}

long endTime = System.currentTimeMillis();

System.out.println("ArrayBlockingQueue costTime = " + (endTime - startTime) + "ms");

}

}).start();

}

}

DisruptorSingle4Test.java

package com.bfxy.disruptor.ability;

import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;

import com.lmax.disruptor.BusySpinWaitStrategy;

import com.lmax.disruptor.EventFactory;

import com.lmax.disruptor.RingBuffer;

import com.lmax.disruptor.YieldingWaitStrategy;

import com.lmax.disruptor.dsl.Disruptor;

import com.lmax.disruptor.dsl.ProducerType;

public class DisruptorSingle4Test {

public static void main(String[] args) {

int ringBufferSize = 65536;

final Disruptor disruptor = new Disruptor(

new EventFactory() {

public Data newInstance() {

return new Data();

}

},

ringBufferSize,

Executors.newSingleThreadExecutor(),

ProducerType.SINGLE,

//new BlockingWaitStrategy()

new YieldingWaitStrategy()

);

DataConsumer consumer = new DataConsumer();

//消费数据

disruptor.handleEventsWith(consumer);

disruptor.start();

new Thread(new Runnable() {

public void run() {

RingBuffer ringBuffer = disruptor.getRingBuffer();

for (long i = 0; i < Constants.EVENT_NUM_OHM; i++) {

long seq = ringBuffer.next();

Data data = ringBuffer.get(seq);

data.setId(i);

data.setName("c" + i);

ringBuffer.publish(seq);

}

}

}).start();

}

}

Constants.java

package com.bfxy.disruptor.ability;

public interface Constants {

int EVENT_NUM_OHM = 100000000;

int EVENT_NUM_FM = 50000000;

int EVENT_NUM_OM = 10000000;

}

Data.java

package com.bfxy.disruptor.ability;

import java.io.Serializable;

public class Data implements Serializable {

private static final long serialVersionUID = 2035546038986494352L;

private Long id ;

private String name;

public Data() {

}

public Data(Long id, String name) {

super();

this.id = id;

this.name = name;

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

DataConsumer

package com.bfxy.disruptor.ability;

import com.lmax.disruptor.EventHandler;

public class DataConsumer implements EventHandler {

private long startTime;

private int i;

public DataConsumer() {

this.startTime = System.currentTimeMillis();

}

public void onEvent(Data data, long seq, boolean bool)

throws Exception {

i++;

if (i == Constants.EVENT_NUM_OHM) {

long endTime = System.currentTimeMillis();

System.out.println("Disruptor costTime = " + (endTime - startTime) + "ms");

}

}

}

处理1亿次

47be1cc1b3d1b32161ae20d38093839c.png

f8be131ac7432f3615f8f2ce7ea89da1.png

很明显,Disruptor完胜!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值