Search in sources :

Example 6 with Consumer

use of com.alibaba.middleware.race.mom.Consumer in project alibaba-mom by younfor.

the class MomStressTest method testBasic.

private static void testBasic() {
    final int code = random.nextInt(100000);
    final ConsumeResult consumeResult = new ConsumeResult();
    consumeResult.setStatus(ConsumeStatus.SUCCESS);
    final String topic = TOPIC + code;
    try {
        String ip = System.getProperty("SIP");
        Consumer consumer = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
        consumer.setGroupId(CID + code);
        consumer.subscribe(topic, "", new MessageListener() {

            @Override
            public ConsumeResult onMessage(Message message) {
                if (!message.getTopic().equals(topic)) {
                    testResult.setSuccess(false);
                    testResult.setInfo("expect topic:" + topic + ", actual topic:" + message.getTopic());
                }
                long delay = System.currentTimeMillis() - message.getBornTime();
                /*					if (delay>1000) {
						testResult.setSuccess(false);
						testResult.setInfo("msg "+message.getMsgId()+" delay "+(System.currentTimeMillis()-message.getBornTime())+" ms");
					}*/
                totalDelay.addAndGet(delay);
                recvCount.incrementAndGet();
                return consumeResult;
            }
        });
        consumer.start();
        final Producer producer = (Producer) Class.forName("com.alibaba.middleware.race.mom.DefaultProducer").newInstance();
        producer.setGroupId(PID + code);
        producer.setTopic(topic);
        producer.start();
        long start = System.currentTimeMillis();
        for (int i = 0; i < c; i++) {
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        try {
                            Message msg = new Message();
                            msg.setBody(BODY.getBytes(charset));
                            msg.setProperty("area", "hz" + code);
                            final long startRt = System.currentTimeMillis();
                            SendResult result = producer.sendMessage(msg);
                            if (result.getStatus() == SendStatus.SUCCESS) {
                                sendCount.incrementAndGet();
                                totalRT.addAndGet(System.currentTimeMillis() - startRt);
                            }
                        /*producer.asyncSendMessage(msg, new SendCallback() {
									
									@Override
									public void onResult(SendResult result) {
										if (result.getStatus()==SendStatus.SUCCESS) {
											sendCount.incrementAndGet();
											totalRT.addAndGet(System.currentTimeMillis()-startRt);
										}
									}
								});*/
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        Thread.sleep(30000);
        if (!testResult.isSuccess()) {
            return;
        }
        long totalTime = System.currentTimeMillis() - start;
        long sendTps = sendCount.get() * 1000 / totalTime;
        long recvTps = recvCount.get() * 1000 / totalTime;
        long rt = totalRT.get() / sendCount.get();
        long delay = totalDelay.get() / recvCount.get();
        testResult.setInfo("send tps:" + sendTps + ", recv tps:" + recvTps + ", send rt:" + rt + ", avg delay:" + delay);
    } catch (Exception e) {
        testResult.setSuccess(false);
        testResult.setInfo(e.getMessage());
    }
}
Also used : Consumer(com.alibaba.middleware.race.mom.Consumer) Message(com.alibaba.middleware.race.mom.Message) Producer(com.alibaba.middleware.race.mom.Producer) SendResult(com.alibaba.middleware.race.mom.SendResult) MessageListener(com.alibaba.middleware.race.mom.MessageListener) ConsumeResult(com.alibaba.middleware.race.mom.ConsumeResult)

Aggregations

ConsumeResult (com.alibaba.middleware.race.mom.ConsumeResult)6 Consumer (com.alibaba.middleware.race.mom.Consumer)6 Message (com.alibaba.middleware.race.mom.Message)6 MessageListener (com.alibaba.middleware.race.mom.MessageListener)6 Producer (com.alibaba.middleware.race.mom.Producer)5 SendResult (com.alibaba.middleware.race.mom.SendResult)5 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1