com.seajas.search.concept.QueuePerformanceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.concept.QueuePerformanceTest.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.concept;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/com/seajas/search/concept/QueuePerformanceTest-context.xml")
public class QueuePerformanceTest {

    Logger logger = LoggerFactory.getLogger(QueuePerformanceTest.class);

    @Resource(name = "jmsReadChannel")
    DirectChannel readChannel;

    @Resource(name = "jmsWriteChannel")
    DirectChannel writeChannel;

    MessageHandler messageHandler;

    @Before
    public void setup() throws Exception {
        messageHandler = Mockito.mock(MessageHandler.class);
        readChannel.subscribe(messageHandler);
    }

    @After
    public void shutdown() throws Exception {
        readChannel.unsubscribe(messageHandler);
    }

    @Test
    public void spool() throws Exception {
        final int threadCount = 4;
        final int messageCount = 20000;
        final Message<String> message = MessageBuilder.withPayload("Hello World!").build();

        Thread[] threads = new Thread[threadCount];
        for (int i = 0; i < threadCount; ++i) {
            threads[i] = new Thread() {
                @Override
                public void run() {
                    for (int i = messageCount / threadCount; i > 0; --i) {
                        if (i % 1000 == 0)
                            logger.debug("Remaining: " + i);
                        writeChannel.send(message);
                    }
                }
            };
        }

        long before = System.currentTimeMillis();
        for (Thread t : threads)
            t.start();
        for (Thread t : threads)
            t.join();
        long elapsed = System.currentTimeMillis() - before;

        String reportFormat = "%d messages took %fs with %d threads (%fms per message).";
        logger.info(String.format(reportFormat, messageCount, elapsed / 1000.0, threadCount,
                (double) elapsed / messageCount));
    }

}