rk.java.compute.cep.EventBusTest.java Source code

Java tutorial

Introduction

Here is the source code for rk.java.compute.cep.EventBusTest.java

Source

/**
 *    Copyright 2012 RK
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package rk.java.compute.cep;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.springframework.util.StopWatch;

import rk.java.compute.cep.Compute.IPriceEventSink;
import rk.java.compute.cep.VolumeWeightedAverage.AveragePrice;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * This is a Guava EventBus based prototype - very quick.
 * 
 * @author RK
 */
public class EventBusTest {
    final int numberOfMarketSourceInstances = 10;
    final int numberOfTicksPerProducer = 100000;
    final int printTicksEvery = numberOfTicksPerProducer / 1;
    final EventBus eventBus = new EventBus("VWAP");
    IPriceEventSink priceEventSink = null;

    @Before
    public void setUp() throws Exception {

        priceEventSink = new IPriceEventSink() {
            int count = 0;

            @Override
            public void publish(Object event) {
                count++;
                if (count % printTicksEvery == 0)
                    eventBus.post(event);
            }
        };

        eventBus.register(new Object() {
            @Subscribe
            public void listen(AveragePrice pp) {
                System.out.println(pp);
            }
        });
    }

    @Test
    public void processOnThreadPerSymbolBasis() throws Exception {

        ComputeService dispatcher = new ComputeService(numberOfMarketSourceInstances, priceEventSink);
        dispatcher.subscribeToTickEventsFrom(eventBus);

        for (int i = 0; i < numberOfMarketSourceInstances; i++) {
            TradeFeed market = new TradeFeed(numberOfTicksPerProducer);
            market.setName("Market Maker " + i);
            market.publishTickEventsTo(eventBus);
            market.setDaemon(true);
            market.start();
        }

        StopWatch await = dispatcher.shutDownAndAwaitTermination(1, TimeUnit.MINUTES);
        System.out.println(await.prettyPrint());
        System.out.println(dispatcher);

        /*
         * Rem to add for poison pills when counting...
         */
        assertEquals(numberOfMarketSourceInstances * numberOfTicksPerProducer + numberOfMarketSourceInstances,
                dispatcher.getTicksReceivedCount());
        assertEquals(numberOfMarketSourceInstances * numberOfTicksPerProducer + numberOfMarketSourceInstances,
                dispatcher.getTicksProcessedCount());
    }

    @Test
    public void processOnThreadPerCoreBasis() throws Exception {

        int NUMBER_OF_CORES = 8;
        ComputeService dispatcher = new ComputeService(numberOfMarketSourceInstances, NUMBER_OF_CORES,
                priceEventSink);
        dispatcher.subscribeToTickEventsFrom(eventBus);

        for (int i = 0; i < numberOfMarketSourceInstances; i++) {
            TradeFeed market = new TradeFeed(numberOfTicksPerProducer);
            market.setName("Market Maker " + i);
            market.setDaemon(true);
            market.publishTickEventsTo(eventBus);
            market.start();
        }

        StopWatch await = dispatcher.shutDownAndAwaitTermination(1, TimeUnit.MINUTES);
        System.out.println(await.prettyPrint());
        System.out.println(dispatcher);
        /*
        * Rem to add for poison pills when counting...
        */
        assertEquals(numberOfMarketSourceInstances * numberOfTicksPerProducer + numberOfMarketSourceInstances,
                dispatcher.getTicksReceivedCount());
        assertEquals(numberOfMarketSourceInstances * numberOfTicksPerProducer + numberOfMarketSourceInstances,
                dispatcher.getTicksProcessedCount());
    }
}