Search in sources :

Example 61 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest13.

@Test
public void persistenceTest13() throws InterruptedException {
    log.info("Persistence test 13 - partitioned sum with group-by on length windows.");
    final int inputEventCount = 10;
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "@app:name('incrementalPersistenceTest10') " + "define stream cseEventStreamOne (symbol string, price float,volume int);" + "partition with (price>=100 as 'large' or price<100 as 'small' of cseEventStreamOne) " + "begin @info(name " + "= 'query1') from cseEventStreamOne#window.length(4) select symbol,sum(price) as price " + "group by symbol insert into " + "OutStockStream ;  end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            if (events != null) {
                for (Event event : events) {
                    count++;
                    lastValue = ((Double) event.getData(1)).longValue();
                }
            }
        }
    };
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 95f + i, 100 });
        Thread.sleep(100);
        siddhiAppRuntime.persist();
    }
    inputHandler.send(new Object[] { "IBM", 205f, 100 });
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    Thread.sleep(1000);
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    Thread.sleep(1000);
    inputHandler.send(new Object[] { "IBM", 105f, 100 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(new Long(414), lastValue);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PersistenceStore(org.wso2.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 62 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class StatisticsTestCase method statisticsTest5.

/**
 * To not enable stats if no Stats manager enabled
 */
@Test
public void statisticsTest5() throws InterruptedException {
    log.info("statistics test 5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.enableStats(true);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0)));
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    PrintStream old = System.out;
    System.setOut(ps);
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3010);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    System.out.flush();
    String output = baos.toString();
    AssertJUnit.assertFalse(output.contains("Gauges"));
    log.info(output);
    System.setOut(old);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PrintStream(java.io.PrintStream) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 63 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class StatisticsTestCase method statisticsTest1.

@Test
public void statisticsTest1() throws InterruptedException {
    log.info("statistics test 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:statistics(reporter = 'console', interval = '2' )" + " " + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0)));
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    PrintStream old = System.out;
    System.setOut(ps);
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3010);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    System.out.flush();
    String output = baos.toString();
    AssertJUnit.assertTrue(output.contains("Gauges"));
    AssertJUnit.assertTrue(output.contains("org.wso2.siddhi." + SiddhiConstants.METRIC_INFIX_SIDDHI_APPS));
    AssertJUnit.assertTrue(output.contains("query1.memory"));
    AssertJUnit.assertTrue(output.contains("Meters"));
    AssertJUnit.assertTrue(output.contains(SiddhiConstants.METRIC_INFIX_SIDDHI + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_STREAMS + SiddhiConstants.METRIC_DELIMITER + "cseEventStream"));
    AssertJUnit.assertTrue(output.contains("Timers"));
    AssertJUnit.assertTrue(output.contains("query1.latency"));
    log.info(output);
    System.setOut(old);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PrintStream(java.io.PrintStream) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 64 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class StatisticsTestCase method statisticsTest4.

/**
 * To test stats dynamic enabling
 */
@Test
public void statisticsTest4() throws InterruptedException {
    log.info("statistics test 4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:statistics(reporter = 'console', interval = '2' )" + " " + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.enableStats(false);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0)));
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    PrintStream old = System.out;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    System.setOut(ps);
    siddhiAppRuntime.start();
    siddhiAppRuntime.enableStats(false);
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3010);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    System.out.flush();
    String output = baos.toString();
    baos.reset();
    log.info(output);
    AssertJUnit.assertFalse(output.contains("Gauges"));
    // reset
    eventArrived = false;
    count = 0;
    siddhiAppRuntime.enableStats(true);
    Thread.sleep(100);
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3030);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(1, count);
    System.out.flush();
    output = baos.toString();
    baos.reset();
    log.info(output);
    AssertJUnit.assertTrue(output.contains("Gauges"));
    siddhiAppRuntime.shutdown();
    System.setOut(old);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PrintStream(java.io.PrintStream) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 65 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class StatisticsTestCase method statisticsTest6.

@Test
public void statisticsTest6() throws InterruptedException {
    log.info("statistics test 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:statistics(reporter = 'console', interval = '2', include='*query2.*,*cseEventStream2*' )" + " " + "define stream cseEventStream (symbol string, price float, volume int);" + "define stream cseEventStream2 (symbol string, price float, volume int);" + "" + "@info(name = 'query1') " + "from cseEventStream[70 > price] " + "select * " + "insert into outputStream ;" + "" + "@info(name = 'query2') " + "from cseEventStream[volume > 90] " + "select * " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            for (Event event : events) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0)));
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    PrintStream old = System.out;
    System.setOut(ps);
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(3010);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    System.out.flush();
    String output = baos.toString();
    AssertJUnit.assertTrue(output.contains("Gauges"));
    AssertJUnit.assertTrue(output.contains("org.wso2.siddhi." + SiddhiConstants.METRIC_INFIX_SIDDHI_APPS));
    AssertJUnit.assertFalse(output.contains("query1.memory"));
    AssertJUnit.assertTrue(output.contains("cseEventStream2.throughput"));
    AssertJUnit.assertTrue(output.contains("Meters"));
    AssertJUnit.assertTrue(output.contains(SiddhiConstants.METRIC_INFIX_SIDDHI + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_STREAMS + SiddhiConstants.METRIC_DELIMITER + "cseEventStream"));
    AssertJUnit.assertTrue(output.contains("Timers"));
    AssertJUnit.assertFalse(output.contains("query1.latency"));
    AssertJUnit.assertTrue(output.contains("query2.memory"));
    log.info(output);
    System.setOut(old);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PrintStream(java.io.PrintStream) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)218 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)213 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)213 Event (org.wso2.siddhi.core.event.Event)213 Test (org.testng.annotations.Test)207 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)202 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)17 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)14 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)6 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)6 Query (org.wso2.siddhi.query.api.execution.query.Query)6 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)3 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)2 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)2