Java ApacheIgnite队列比LinkedBlockingQueue慢得多
我试图在Ignite中复制一个简单的生产者-消费者场景:Java ApacheIgnite队列比LinkedBlockingQueue慢得多,java,ignite,Java,Ignite,我试图在Ignite中复制一个简单的生产者-消费者场景: public class QueueExample { public static void main(String[] args) { new QueueExample().start(); } private void start() { final AtomicBoolean finishedTest1 = new AtomicBoolean(false);
public class QueueExample {
public static void main(String[] args) {
new QueueExample().start();
}
private void start() {
final AtomicBoolean finishedTest1 = new AtomicBoolean(false);
final BlockingQueue<Double> queue = new LinkedBlockingQueue<>(5);
final CountDownLatch latch = new CountDownLatch(2);
final int MAX = 1000;
new Thread(() -> {
System.out.println("test1 before latch");
latch.countDown();
try {
// wait until other runnable is able to poll
latch.await(20, TimeUnit.SECONDS);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
System.out.println(new Date().getTime() + " start test1");
double test = 2;
Random r = new Random();
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < MAX; i++) {
try {
queue.put(r.nextDouble());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
sw.stop();
finishedTest1.set(true);
//LoggerFactory.getLogger(getClass()).info
System.out.println(new Date().getTime() + " end test1. " + test + ", took:" + sw.getTime() / 1000f);
}).start();
new Thread(() -> {
System.out.println("test2 before latch");
latch.countDown();
try {
// wait until other runnable is able to poll
latch.await(10, TimeUnit.SECONDS);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
System.out.println(new Date().getTime() + " start test2");
StopWatch sw = new StopWatch();
sw.start();
int counter = 0;
try {
for (int i = 0; i < MAX ; i++) {
Double res = queue.poll(1, TimeUnit.SECONDS);
counter++;
}
} catch (InterruptedException e) {
// expected
}
sw.stop();
//LoggerFactory.getLogger(getClass()).info
System.out.println(new Date().getTime() + " end test2. counter " + counter + ", finished:" + finishedTest1.get() + ", took:" + sw.getTime() / 1000f);
}).start();
}
}
公共类队列示例{
公共静态void main(字符串[]args){
新建QueueExample().start();
}
私有void start(){
final AtomicBoolean finishedTest1=新的AtomicBoolean(false);
最终阻塞队列=新的LinkedBlockingQueue(5);
最终倒计时闩锁=新倒计时闩锁(2);
最终整数最大值=1000;
新线程(()->{
System.out.println(“闩锁前测试1”);
倒计时();
试一试{
//等待其他runnable能够轮询
等待(20,时间单位秒);
}捕获(例外情况除外){
抛出新的运行时异常(ex);
}
System.out.println(new Date().getTime()+“start test1”);
双重检验=2;
随机r=新随机();
秒表sw=新秒表();
sw.start();
对于(int i=0;i{
System.out.println(“闩锁前测试2”);
倒计时();
试一试{
//等待其他runnable能够轮询
等待(10,时间单位秒);
}捕获(例外情况除外){
抛出新的运行时异常(ex);
}
System.out.println(new Date().getTime()+“start test2”);
秒表sw=新秒表();
sw.start();
int计数器=0;
试一试{
对于(int i=0;i
为什么这要快100倍(0.02秒vs.你在这里把航空母舰比作玩具船
LinkedBlockingQueue
是一种在单个JVM内存中工作的数据结构
IgniteQueue
是一种基于Ignite的键值存储的分布式结构。它可以在数百台机器上工作,具有不同的一致性级别、备份副本和持久性。当然,它受到许多机器的支持,比简单的本地队列慢。考虑到f预热阶段,并对400k元素执行此操作,我确实观察到更大的因素,速度慢了500倍以上。我尝试了各种内存和存储配置,但迄今为止运气不佳。当然,如果启用存储,我会预期速度慢下来,但在5倍的范围内更慢,特别是在默认配置似乎是持久性的情况下Enabled=False看起来redis+redisson给出了类似的结果(尽管速度快了3倍)
public class MyIgnite {
public static void main(String[] args) {
new MyIgnite().start();
}
private void start() {
IgniteConfiguration icfg = new IgniteConfiguration();
icfg.setIgniteInstanceName("test1");
Ignite ignite1 = Ignition.start(icfg);
final CountDownLatch latch = new CountDownLatch(2);
final int queueSize = 5;
CollectionConfiguration queueCfg = new CollectionConfiguration();
ignite1.compute().runAsync(new IgniteRunnable() {
@IgniteInstanceResource
Ignite ignite;
@Override
public void run() {
IgniteQueue<Double> queue = ignite.queue("test", queueSize, queueCfg);
System.out.println("test1 fetched queue");
latch.countDown();
try {
// wait until other runnable is able to poll
latch.await(20, TimeUnit.SECONDS);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
System.out.println("start test1");
double test = 2;
Random r = new Random();
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000; i++) {
queue.put(r.nextDouble());
}
sw.stop();
//LoggerFactory.getLogger(getClass()).info
System.out.println("end test1. " + test + " at ignite " + ignite.name() + ", took:" + sw.getTime() / 1000f);
}
});
System.out.println("starting test2");
icfg = new IgniteConfiguration();
icfg.setIgniteInstanceName("test2");
Ignite ignite2 = Ignition.start(icfg);
ignite2.compute().runAsync(new IgniteRunnable() {
@IgniteInstanceResource
Ignite ignite;
@Override
public void run() {
IgniteQueue<Double> queue = ignite.queue("test", queueSize, queueCfg);
System.out.println("test2 fetched queue");
latch.countDown();
try {
// wait until other runnable is able to poll
latch.await(10, TimeUnit.SECONDS);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
System.out.println("start test2");
StopWatch sw = new StopWatch();
sw.start();
int counter = 0;
try {
for (int i = 0; i < 1000; i++) {
Double res = queue.poll(5, TimeUnit.SECONDS);
counter++;
}
} catch (IgniteException exc) {
System.out.println("Somehow cannot poll. " + exc);
}
sw.stop();
//LoggerFactory.getLogger(getClass()).info
System.out.println("end test2. counter " + counter + " at ignite " + ignite.name() + ", took:" + sw.getTime() / 1000f);
}
});
System.out.println("oldest node: " + ignite1.cluster().forOldest().hostNames());
System.out.println("nodes: " + ignite1.cluster().nodes().size());
// does it really gracefully shut the nodes down?
// Ignition.stop(ignite1.name(), false);
// Ignition.stop(ignite2.name(), false);
}
}