public void testPerformance() throws Exception {
System.out.println("Running Benchmark for destination="+destination+", producers="+PRODUCER_COUNT+", consumers="+CONSUMER_COUNT+", deliveryMode="+deliveryMode);
final int CONSUME_COUNT = destination.isTopic() ? CONSUMER_COUNT*PRODUCE_COUNT : PRODUCE_COUNT;
final Semaphore consumersStarted = new Semaphore(1-(CONSUMER_COUNT));
final Semaphore producersFinished = new Semaphore(1-(PRODUCER_COUNT));
final Semaphore consumersFinished = new Semaphore(1-(CONSUMER_COUNT));
final ProgressPrinter printer = new ProgressPrinter(PRODUCE_COUNT+CONSUME_COUNT, 10);
// Start a producer and consumer
profilerPause("Benchmark ready. Start profiler ");
long start = System.currentTimeMillis();
final AtomicInteger receiveCounter = new AtomicInteger(0);
for( int i=0; i < CONSUMER_COUNT; i++) {
new Thread() {
public void run() {
try {
// Consume the messages
StubConnection connection = new StubConnection(broker);
ConnectionInfo connectionInfo = createConnectionInfo();
connection.send(connectionInfo);
SessionInfo sessionInfo = createSessionInfo(connectionInfo);
ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo, destination);
consumerInfo.setPrefetchSize(1000);
connection.send(sessionInfo);
connection.send(consumerInfo);
consumersStarted.release();
while( receiveCounter.get() < CONSUME_COUNT ) {
int counter=0;
// Get a least 1 message.
Message msg = receiveMessage(connection, 2000);
if( msg!=null ) {
printer.increment();
receiveCounter.incrementAndGet();
counter++;
// Try to piggy back a few extra message acks if they are ready.
Message extra=null;
while( (extra = receiveMessage(connection,0))!=null ) {
msg=extra;
printer.increment();
receiveCounter.incrementAndGet();
counter++;
}
}
if(msg!=null) {
connection.send(createAck(consumerInfo, msg, counter, MessageAck.STANDARD_ACK_TYPE));
} else if ( receiveCounter.get() < CONSUME_COUNT ) {
System.out.println("Consumer stall, waiting for message #"+receiveCounter.get()+1);
}
}
connection.send(closeConsumerInfo(consumerInfo));
} catch (Throwable e) {
e.printStackTrace();
} finally {
consumersFinished.release();
}
}
}.start();
}
// Make sure that the consumers are started first to avoid sending messages
// before a topic is subscribed so that those messages are not missed.
consumersStarted.acquire();
// Send the messages in an async thread.
for( int i=0; i < PRODUCER_COUNT; i++) {
new Thread() {
public void run() {
try {
StubConnection connection = new StubConnection(broker);
ConnectionInfo connectionInfo = createConnectionInfo();
connection.send(connectionInfo);
SessionInfo sessionInfo = createSessionInfo(connectionInfo);
ProducerInfo producerInfo = createProducerInfo(sessionInfo);
connection.send(sessionInfo);
connection.send(producerInfo);
for(int i=0; i < PRODUCE_COUNT/PRODUCER_COUNT; i++) {
Message message = createMessage(producerInfo, destination);
message.setPersistent(deliveryMode);
message.setResponseRequired(false);
connection.send(message);
printer.increment();
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
producersFinished.release();
}
};
}.start();
}
producersFinished.acquire();
long end1 = System.currentTimeMillis();
consumersFinished.acquire();
long end2 = System.currentTimeMillis();
System.out.println("Results for destination="+destination+", producers="+PRODUCER_COUNT+", consumers="+CONSUMER_COUNT+", deliveryMode="+deliveryMode);
System.out.println("Produced at messages/sec: "+ (PRODUCE_COUNT*1000.0/(end1-start)));
System.out.println("Consumed at messages/sec: "+ (CONSUME_COUNT*1000.0/(end2-start)));