All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.siddhi.sample.benchmark.server.ServerThread Maven / Gradle / Ivy

The newest version!
/**
 * 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 org.siddhi.sample.benchmark.server;

import org.siddhi.core.event.Event;
import org.siddhi.sample.benchmark.SimpleStockQuoteEventGenerator;
import org.siddhi.sample.benchmark.query.types.QueryProvider;
import org.siddhi.sample.benchmark.query.types.SimpleStockQuoteVWAPQueryProvider;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.channels.SocketChannel;

public class ServerThread extends Thread {

    SocketChannel socket;
    QueryProvider queryProvider;
    private boolean eventBased;
    private int limit = 0;

    public ServerThread(SocketChannel socket, QueryProvider queryProvider, int limit, boolean eventBased) {
        this.socket = socket;
        this.queryProvider = queryProvider;
        this.limit = limit;
        this.eventBased = eventBased;
    }

    public void run() {
        try {
            ObjectInputStream ois = new ObjectInputStream(socket.socket().getInputStream());
            do {
                Event event = (Event) ois.readObject();   // reads events from the connected client
                queryProvider.sendEvent(event);
                if (SimpleStockQuoteVWAPQueryProvider.EVENT_COUNTER == limit) { // if the limit exceeds, it exits processing events
                    queryProvider.sendEvent(new SimpleStockQuoteEventGenerator("CSEStream").generateDefinedEvent("S0", 4.0, 9999));
                    RunningTimeStats.FINISHED_SENDING = true;
                    break;
                }
            } while (true);

            while (!SimpleStockQuoteVWAPQueryProvider.finishedCallBack) { //waits to finish the callback methods to capture all the event before viewing results
            }

            if (eventBased) {
                viewEventBasedStats();
            } else {
                viewTimeBasedStats();
            }

        } catch (IOException e) {
            System.out.println("# One client connection disconnected...");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    private void viewEventBasedStats() {
        System.out.println("");
        System.out.println("");
        System.out.println("<< + + ...... Event Based Bench Mark Evaluation Results ...... + + >>");
        System.out.println("");

        int x = 0;
        long temp = 0;
        for (int i = 0; i < RunningTimeStats.eventMap.size(); i++) {
            double perc;

            if (i == RunningTimeStats.EVENT_BASED_SLOTS.length) {
                perc = (limit - RunningTimeStats.EVENT_BASED_SLOTS[RunningTimeStats.EVENT_BASED_SLOTS.length - 1]) * 100.0 / limit;
                System.out.println(RunningTimeStats.EVENT_BASED_SLOTS[RunningTimeStats.EVENT_BASED_SLOTS.length - 1] + " <" + " #Events   Time(ms)  "
                        + (RunningTimeStats.EVENT_BASED_SLOTS[i]) + " " + perc + "%");
            } else {
                if (x == 0) {
                    x = 1;
                    System.out.println(x + " < " + RunningTimeStats.EVENT_BASED_SLOTS[i] + " #Events   Time(ms) "
                            + RunningTimeStats.eventMap.get(RunningTimeStats.EVENT_BASED_SLOTS[i]) + " " + RunningTimeStats.EVENT_BASED_SLOTS[i] * 100.0 / limit + "%");

                } else {
                    System.out.println(RunningTimeStats.EVENT_BASED_SLOTS[i - 1] + " < " + RunningTimeStats.EVENT_BASED_SLOTS[i] + " #Events   Time(ms)  "
                            + (RunningTimeStats.eventMap.get(RunningTimeStats.EVENT_BASED_SLOTS[i]) + " " + (RunningTimeStats.EVENT_BASED_SLOTS[i] - RunningTimeStats.EVENT_BASED_SLOTS[i - 1]) * 100.0 / limit + "%"));
                }
            }
        }
        System.out.println("# Overall Throughput to process " + limit + " events = " + RunningTimeStats.OVERALL_THROUGHPUT + " ms");
    }

    private void viewTimeBasedStats() {

        System.out.println("");
        System.out.println("");
        System.out.println("<< + + ...... Time Based Bench Mark Evaluation Results ....... + + >>");
        System.out.println("");
        
        int x = 0;
        long temp = 0;
        for (int i = 0; i < RunningTimeStats.timeMap.size(); i++) {
            long events;
            double perc;

            if (i == RunningTimeStats.DEFAULT_MS.length) {
                temp = RunningTimeStats.timeMap.get(RunningTimeStats.DEFAULT_MS[RunningTimeStats.DEFAULT_MS.length - 1]);
                events = RunningTimeStats.timeMap.get(RunningTimeStats.DEFAULT_MS[i - 1] + 1);
                perc = (events - temp) * 100.0 / limit;
                System.out.println(RunningTimeStats.DEFAULT_MS[RunningTimeStats.DEFAULT_MS.length - 1] + " <" + " Time(ms)  #Processed Events "
                        + (events - temp) + " " + perc + "%");

            } else {
                events = RunningTimeStats.timeMap.get(RunningTimeStats.DEFAULT_MS[i]);

                if (x == 0) {
                    x = 1;
                    System.out.println(x + " < " + RunningTimeStats.DEFAULT_MS[i] + " Time(ms)  #Processed Events "
                            + RunningTimeStats.timeMap.get(RunningTimeStats.DEFAULT_MS[i]) + " " + events * 100.0 / limit + "%");

                } else {
                    temp = RunningTimeStats.timeMap.get(RunningTimeStats.DEFAULT_MS[i - 1]);
                    System.out.println(RunningTimeStats.DEFAULT_MS[i - 1] + " < " + RunningTimeStats.DEFAULT_MS[i] + " Time(ms)  #Processed Events "
                            + (events - temp) + " " + (events - temp) * 100.0 / limit + "%");
                }

            }

        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy