use of org.wso2.siddhi.query.api.SiddhiApp 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();
}
use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class PersistenceTestCase method persistenceTest1.
@Test
public void persistenceTest1() throws InterruptedException {
log.info("persistence test 1 - window query");
PersistenceStore persistenceStore = new InMemoryPersistenceStore();
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setPersistenceStore(persistenceStore);
String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(10) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream ";
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
eventArrived = true;
for (Event inEvent : inEvents) {
count++;
AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
Thread.sleep(100);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(200), lastValue);
// persisting
Thread.sleep(500);
siddhiAppRuntime.persist();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// restarting siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// shutdown siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count <= 6);
AssertJUnit.assertEquals(new Long(400), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
use of org.wso2.siddhi.query.api.SiddhiApp 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);
}
use of org.wso2.siddhi.query.api.SiddhiApp 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);
}
use of org.wso2.siddhi.query.api.SiddhiApp 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);
}
Aggregations