Search in sources :

Example 41 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow7.

@Test
public void testExternalTimeBatchWindow7() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 2 sec) " + "output all events; ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(3000);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
    Thread.sleep(3000);
    inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
    inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
    Thread.sleep(3000);
    assertEquals("Event arrived", true, eventArrived);
    assertEquals("In Events ", 4, inEventCount);
    assertEquals("Remove Events ", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 42 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow18.

@Test
public void testExternalTimeBatchWindow18() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test18");
    SiddhiManager siddhiManager = new SiddhiManager();
    String inputStream = "define stream inputStream(currentTime long,value int); " + "define window inputWindow(currentTime long,value int) externalTimeBatch(currentTime,5 sec, 0, 6 sec)" + " output current events; ";
    String query = " " + "@info(name = 'query0') " + "from inputStream " + "insert into inputWindow; " + "" + "@info(name='query') " + "from inputWindow " + "select value, currentTime " + "insert into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + query);
    siddhiAppRuntime.addCallback("query", new QueryCallback() {

        int count = 0;

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            if (count == 0) {
                assertEquals(1, inEvents[0].getData(0));
            } else if (count == 1) {
                assertEquals(6, inEvents[0].getData(0));
            } else if (count == 2) {
                assertEquals(11, inEvents[0].getData(0));
            } else if (count == 3) {
                assertEquals(14, inEvents[0].getData(0));
            } else if (count == 4) {
                assertEquals(15, inEvents[0].getData(0));
            }
            count += 1;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 10000L, 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 11000L, 2 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 12000L, 3 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 13000L, 4 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 14000L, 5 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 15000L, 6 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 16500L, 7 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 17000L, 8 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 18000L, 9 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 19000L, 10 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 20100L, 11 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 20500L, 12 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 22000L, 13 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 25000L, 14 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 32000L, 15 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 33000L, 16 });
    Thread.sleep(6000);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 43 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow15.

@Test
public void testExternalTimeBatchWindow15() throws Exception {
    log.info("ExternalTimeBatchWindow test15");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stream = "define stream jmxMetric(cpu int, memory int, bytesIn long, bytesOut long, timestamp long); " + "define window jmxMetricWindow(cpu int, memory int, bytesIn long, bytesOut long, timestamp long) " + "externalTimeBatch(timestamp, 10 sec) output current events; ";
    String query = "" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name = 'downSample') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, max(cpu) as maxCpu, min(cpu) as minCpu, " + " '|' as s, " + " avg(memory) as avgMem, max(memory) as maxMem, min(memory) as minMem, " + " '|' as s1, " + " avg(bytesIn) as avgBytesIn, max(bytesIn) as maxBytesIn, min(bytesIn) as minBytesIn, " + " '|' as s2, " + " avg(bytesOut) as avgBytesOut, max(bytesOut) as maxBytesOut, min(bytesOut) as minBytesOut, " + " '|' as s3, " + " timestamp as timeWindowEnds, " + " '|' as s4, " + " count(1) as metric_count " + " INSERT INTO tmp;";
    SiddhiManager sm = new SiddhiManager();
    SiddhiAppRuntime plan = sm.createSiddhiAppRuntime(stream + query);
    InputHandler input = plan.getInputHandler("jmxMetric");
    // stream call back doesn't follow the counter
    final AtomicInteger counter = new AtomicInteger();
    {
        // stream callback
        plan.addCallback("jmxMetric", new StreamCallback() {

            @Override
            public void receive(Event[] arg0) {
                counter.addAndGet(arg0.length);
            }
        });
    }
    final AtomicInteger queryWideCounter = new AtomicInteger();
    {
        plan.addCallback("downSample", new QueryCallback() {

            @Override
            public void receive(long timeStamp, Event[] inevents, Event[] removevents) {
                int currentCount = queryWideCounter.addAndGet(inevents.length);
                log.info(MessageFormat.format("Round {0} ====", currentCount));
                log.info(" events count " + inevents.length);
                EventPrinter.print(inevents);
            }
        });
    }
    plan.start();
    int round = 4;
    int eventsPerRound = 0;
    long externalTs = System.currentTimeMillis();
    for (int i = 0; i < round; i++) {
        eventsPerRound = sendEvent(input, i, externalTs);
        Thread.sleep(3000);
    }
    // trigger next round
    sendEvent(input, round, externalTs);
    plan.shutdown();
    Thread.sleep(1000);
    assertEquals(round * eventsPerRound + eventsPerRound, counter.get());
    assertEquals(round, queryWideCounter.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 44 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow1.

@Test
public void testExternalTimeBatchWindow1() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 6 sec) " + "output all events; ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804342L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335814341L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335814345L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335824341L, "192.10.1.7" });
    Thread.sleep(1000);
    assertEquals("Event arrived", true, eventArrived);
    assertEquals("In Events ", 2, inEventCount);
    assertEquals("Remove Events ", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 45 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow14.

@Test
public void testExternalTimeBatchWindow14() throws Exception {
    log.info("ExternalTimeBatchWindow test14");
    SiddhiManager siddhiManager = new SiddhiManager();
    String query = "define stream jmxMetric(cpu int, timestamp long); " + "define window jmxMetricWindow(cpu int, timestamp long) externalTimeBatch(timestamp, 10 sec);" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name='query1') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, count(1) as count insert into tmp;";
    SiddhiAppRuntime runtime = siddhiManager.createSiddhiAppRuntime(query);
    final AtomicInteger recCount = new AtomicInteger(0);
    runtime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            assertEquals(1, inEvents.length);
            recCount.incrementAndGet();
            double avgCpu = (Double) inEvents[0].getData()[0];
            if (recCount.get() == 1) {
                assertEquals(15, avgCpu, 0);
            } else if (recCount.get() == 2) {
                assertEquals(85, avgCpu, 0);
            }
            long count = (Long) inEvents[0].getData()[1];
            assertEquals(3, count);
        }
    });
    InputHandler input = runtime.getInputHandler("jmxMetric");
    runtime.start();
    // external events' time stamp less than the window, should not have event recieved in call back.
    long now = 0;
    int length = 3;
    for (int i = 0; i < length; i++) {
        input.send(new Object[] { 15, now + i * 10 });
    }
    // if the trigger event mix with the last window, we should see the avgValue is not expected
    for (int i = 0; i < length; i++) {
        // the first entity of the second round
        input.send(new Object[] { 85, now + 10000 + i * 10 });
    }
    // to trigger second round
    input.send(new Object[] { 10000, now + 10 * 10000 });
    Thread.sleep(1000);
    assertEquals(2, recCount.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6