测试Activemq artemis队列生产消费

概要

ActiveMQ Artemis是一个开源的消息中间件,它实现了JMS规范,支持多种协议和传输方式。它提供了一个高性能、可扩展、可靠的消息传递系统,适用于各种场景,包括云计算、大数据、企业集成等。

  1. 可靠性高:ActiveMQ Artemis使用持久化存储来保证消息的可靠性,如果有任何失败的情况,系统会自动重试。此外,ActiveMQ Artemis还提供了事务支持,保证消息的原子性和一致性。

  2. 性能优秀:ActiveMQ Artemis具有高性能的消息传递能力,支持异步消息传递,可以同时处理数百万个消息。

  3. 高可用性:ActiveMQ Artemis支持主从备份、集群模式等高可用性特性,可以保证消息系统的可用性和稳定性。

  4. 可扩展性强:ActiveMQ Artemis支持水平和垂直扩展,可以根据需求进行扩容,保证系统的可扩展性。

  5. 多种协议支持:ActiveMQ Artemis支持多种协议和传输方式,包括AMQP、OpenWire、STOMP、REST等,可以更好地满足不同场景下的需求。

整体设计流程

从客户端发送到服务端进行消费,主要测试队列的传输情况和服务器消费情况的测试。在测试中,我们将主要发送以下内容:字符串“hello world”、当前时间戳以及一个唯一的UUID。这些信息将作为消息的有效载荷,通过队列进行传输,并由服务器进行接收和消费。通过这种测试,我们可以评估队列系统的传输性能以及服务器的处理能力,以确保在真实环境中能够有效地处理和传递这些信息。

技术代码

客户端

        int cores = Runtime.getRuntime().availableProcessors();
    @Test
    public void testSendMsg() {
//        String topic = "localhost"+"/multiple";
        String topic = "localhost";
        final MessageHeader header = new MessageHeader(topic);
        String world = new String("hello world");
        final BinaryMessage message = new BinaryMessage(header, world.getBytes());
        try {
            Messenger.send(message);
        } catch (final IOException e) {
            log.error("error", e);
        }
    }

    @Test
    public void testMultipleSendMsg() throws InterruptedException {
        log.info("cores:{}", cores);
        ThreadPoolExecutor threadPoolExecutor
                = new ThreadPoolExecutor(cores+2, cores+2, 60, TimeUnit.SECONDS,
//                    new LinkedBlockingQueue());
//                new ArrayBlockingQueue(10), new BlockPolicy());
                new SynchronousQueue(),new ThreadPoolExecutor.CallerRunsPolicy());
//                new SynchronousQueue(), new BlockPolicy());

//        String textMessage = MsgConstant.LARGE_MESSAGE;
        long start = System.nanoTime();
        int count = 50000;
        for (int i = 0; i < count; i++) {
            threadPoolExecutor.execute(() -> {
                testSendMsg();
            });
        }
        log.info("生产{}数据总共耗时:{}ms", count, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
        //Wait for sending to complete
//		Thread.sleep(500000);
        Thread.currentThread().join();
    }

服务端

@Log4j2
@Component
public class ListenerMq {

    private static final String UPDATE_API_STRATEGY_AND_BASKET = "localhost";

    public static AtomicInteger atomicInteger = new AtomicInteger();
    public static AtomicInteger atomicIntegerSingle = new AtomicInteger();
    AtomicReference<Long> start = new AtomicReference<>(0L);
    int cores = Runtime.getRuntime().availableProcessors();

    ThreadPoolExecutor threadPoolExecutor
            = new ThreadPoolExecutor(cores + 2, cores + 2, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());
//            new ArrayBlockingQueue(10), new BlockPolicy());
//              new SynchronousQueue(), new BlockPolicy());
//            new SynchronousQueue(), new ThreadPoolExecutor.CallerRunsPolicy());

    public static int count = 50000;

    @PostConstruct
    public void receiveApiStrategyCache() {

        log.info("====================receiveApiStrategyCache========cores{}============", cores);
        Messenger.subscribe(UPDATE_API_STRATEGY_AND_BASKET, new MessageConsumer() {
            @Override
            public void onMessage(final Message<?> message) {
                if (message instanceof BinaryMessage) {
                    try {
                        final BinaryMessage msg = (BinaryMessage) message;
                        final MessageHeader header = msg.getHeader();
                        final String payload = new String(msg.getPayload());
                        long timestamp = header.getTimestamp();
                        log.info("offset:{}ms", System.currentTimeMillis() - timestamp);
                        if (atomicIntegerSingle.get() == 0) {
                            start.set(System.nanoTime());
                        }
                        if (atomicIntegerSingle.incrementAndGet() >= count) {
                            log.info("消费{}数据总共耗时:{}ms", count, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start.get()));
                        }
                    } catch (Exception e) {
                        log.error(e);
                    }
                }
            }

            @Override
            public void onError(final Message<?> message) {
                log.error("from TOPIC {} error: {}", message.getHeader().getTopic(), message.getPayload());
            }
        });
        Messenger.subscribe(UPDATE_API_STRATEGY_AND_BASKET+"/multiple", new MessageConsumer() {
            @Override
            public void onMessage(final Message<?> message) {
                if (message instanceof BinaryMessage) {
                    threadPoolExecutor.execute(() -> {
                        final BinaryMessage msg = (BinaryMessage) message;
                        final MessageHeader header = msg.getHeader();
//                        final String payload = new String(msg.getPayload());
                        long timestamp = header.getTimestamp();
                        log.info("offset:{}ms", System.currentTimeMillis() - timestamp);
                        if (atomicInteger.get() == 0) {
                            start.set(System.nanoTime());
                        }
                        if (atomicInteger.incrementAndGet() >= count) {
                            log.info("消费{}数据总共耗时:{}ms", count, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start.get()));
                        }
                    });
                }
            }

            @Override
            public void onError(final Message<?> message) {
                log.error("from TOPIC {} error: {}", message.getHeader().getTopic(), message.getPayload());
            }
        });
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            atomicInteger.get();
            int i1 = atomicInteger.incrementAndGet();
            System.out.println(i1);
        }
    }

}

测试结果

1万数据

异步接收
只考虑接收的情况,假定队列消费很顺畅的情况,不存在处理消息导致消费耗时的情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
10000mac612+2971mscentos811046ms471ms
10000mac612+2911mscentos81735ms199ms
10000mac612+2902mscentos81674ms162ms
10000mac612+2810mscentos88+2542ms
10000mac612+2832mscentos88+2628ms
10000mac612+2888mscentos88+2674ms

异步消费
这里主要是边处理消息和边统计的情况,假定按照正常的消费的耗时情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
10000mac612+2922mscentos81758ms244ms
10000mac612+2922mscentos81625ms96ms
10000mac612+21101mscentos81742ms182ms
10000mac612+21134mscentos88+2694ms90ms
10000mac612+21055mscentos88+2773ms216ms
10000mac612+2956mscentos88+2811ms206ms
10000mac6101057msmac6201224ms659ms
10000mac610965msmac6201020ms438ms
10000mac610996msmac6201359ms792ms

2万数据

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
20000mac612+21355mscentos88+22093ms1139ms
20000mac612+21442mscentos88+22821ms1728ms

3万数据

异步接收
只考虑接收的情况,假定队列消费很顺畅的情况,不存在处理消息导致消费耗时的情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
30000mac612+21841mscentos816181ms4757ms
30000mac612+21874mscentos816707ms5356ms
30000mac612+21849mscentos815548ms4087ms
30000mac612+22028mscentos816852ms5238ms
30000mac612+21617mscentos815644ms4526ms
30000mac612+22049mscentos816872ms5289ms
30000mac612+21925mscentos88+21685ms
30000mac612+21639mscentos88+21442ms
30000mac612+21639mscentos88+21489ms

异步消费
这里主要是边处理消息和边统计的情况,假定按照正常的消费的耗时情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
30000mac612+21937mscentos815592ms4088ms
30000mac612+22243mscentos816245ms4563ms
30000mac612+21953mscentos816874ms5409ms
30000mac612+21905mscentos88+26490ms4975ms
30000mac612+22020mscentos88+25307ms3857ms
30000mac612+21811mscentos88+23847ms5329ms
30000mac6101980mscentos8207113ms5523ms
30000mac6102059mscentos8207730ms6289ms
30000mac6102286mscentos8208035ms6376ms
30000mac6101539msmac6205623ms4551ms
30000mac6101678msmac6203520ms2264ms
30000mac6102258msmac6203440ms1643ms

5万数据

异步接收
只考虑接收的情况,假定队列消费很顺畅的情况,不存在处理消息导致消费耗时的情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
50000mac612+22752mscentos8112130ms9964ms
50000mac612+22690mscentos8113970ms11699ms
50000mac612+22460mscentos8113399ms11294ms
50000mac612+22285mscentos88+22345ms
50000mac612+22420mscentos88+22475ms
50000mac612+22014mscentos88+22382ms
80000mac612+22891mscentos88+24542ms
60000mac612+23179mscentos88+23241ms
70000mac612+22610mscentos88+23179ms

异步消费
这里主要是边处理消息和边统计的情况,假定按照正常的消费的耗时情况。

数据量是否处理消息客户端类型cpu线程数发送耗时服务器端类型cpu线程数接收耗时最后消息延时
50000mac612+22681mscentos8112319ms10197ms
50000mac612+22577mscentos8113705ms11529ms
50000mac612+22752mscentos8114943ms12713ms
50000mac612+22750mscentos88+213109ms10797ms
50000mac612+22730mscentos88+213906ms10681ms
50000mac612+22102mscentos88+211829ms8540ms
50000mac6103343mscentos82017352ms14520ms
50000mac6102802mscentos82015144ms12736ms
50000mac6102836msmac6206517ms4089ms
50000mac6123403msmac6126027ms3183ms

总结

根据测试结果,我们可以得出以下结论:

  • 在考虑消费情况时,1万数据量可以轻松处理,没有任何压力,但这只是单机测试的结果,只能作为参考。
  • 如果消费没有耗时,3万数据量也可以处理,但前提是所有消费都堆积在缓存中。

因此,在实际应用中,需要根据具体场景和需求来确定数据量的大小和处理方式。同时,还需要考虑系统的其他性能指标,如响应时间、错误率等来综合评估系统的性能表现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

查拉图斯特拉talk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值