diff --git a/src/main/java/com/teragrep/rlo_06/RFC5424Parser.java b/src/main/java/com/teragrep/rlo_06/RFC5424Parser.java index c8e9d40..c00ac53 100644 --- a/src/main/java/com/teragrep/rlo_06/RFC5424Parser.java +++ b/src/main/java/com/teragrep/rlo_06/RFC5424Parser.java @@ -54,6 +54,22 @@ public final class RFC5424Parser { private final ParserResultSet resultset; private final Consumer streamConsumer; + public RFC5424Parser() { + this(new InputStream() { + @Override + public int read() { + return -1; + } + }); + } + + public RFC5424Parser(InputStream inputStream) { + this(inputStream, new RFC5424ParserSubscription()); + } + public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscription) { + this(inputStream, subscription, new RFC5424ParserSDSubscription()); + } + public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription) { this(inputStream, subscription, sdSubscription, true); } diff --git a/src/test/java/com/teragrep/rlo_06/EmptyParserInputTest.java b/src/test/java/com/teragrep/rlo_06/EmptyParserInputTest.java new file mode 100644 index 0000000..5b5f744 --- /dev/null +++ b/src/test/java/com/teragrep/rlo_06/EmptyParserInputTest.java @@ -0,0 +1,18 @@ +package com.teragrep.rlo_06; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class EmptyParserInputTest { + @Test + void testEmptyParserInput() throws IOException { + // Should not break but also should not return any data + RFC5424Parser parser = new RFC5424Parser(); + Assertions.assertFalse(parser.next()); + ResultSetAsString resultsetAsString = new ResultSetAsString(parser.get()); + } +} diff --git a/src/test/java/com/teragrep/rlo_06/tests/PerformanceTest.java b/src/test/java/com/teragrep/rlo_06/tests/PerformanceTest.java index 3282d7c..d2f9159 100644 --- a/src/test/java/com/teragrep/rlo_06/tests/PerformanceTest.java +++ b/src/test/java/com/teragrep/rlo_06/tests/PerformanceTest.java @@ -45,10 +45,7 @@ */ package com.teragrep.rlo_06.tests; -import com.teragrep.rlo_06.ParserResultSet; -import com.teragrep.rlo_06.RFC5424Parser; -import com.teragrep.rlo_06.RFC5424ParserSDSubscription; -import com.teragrep.rlo_06.RFC5424ParserSubscription; +import com.teragrep.rlo_06.*; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfSystemProperty; @@ -117,8 +114,110 @@ void testShortPayloadPerformance() throws Exception { long msgsize = (count * SYSLOG_MESSAGE.length())/1024/1024; long spent = instant2.toEpochMilli()-instant1.toEpochMilli(); System.out.println("testShortPayloadPerformance: time taken " + spent + " for " + count + - ", total RPS: " + (float) count/ ((float) spent/1000) + - ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); + ", total RPS: " + (float) count/ ((float) spent/1000) + + ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); + + } + + @Test + @DisabledIfSystemProperty(named = "noPerfTest", matches = "true") + void testOnlyMsgSub() throws Exception { + + String SYSLOG_MESSAGE = "<14>1 2014-06-20T09:14:07.12345+00:00 host01 systemd DEA MSG-01 [ID_A@1 u=\"\\\"3\" e=\"t\"][ID_B@2 n=\"9\"][event_id@48577 hostname=\"sc-99-99-14-247\" uuid=\"0FD92E51B37748EB90CD894CCEE63907\" unixtime=\"1612047600.0\" id_source=\"source\"][event_node_source@48577 hostname=\"sc-99-99-14-247\" source=\"f17_ssmis_20210131v7.nc\" source_module=\"imfile\"][event_node_relay@48577 hostname=\"localhost\" source=\"sc-99-99-14-247\" source_module=\"imrelp\"][event_version@48577 major=\"2\" minor=\"2\" hostname=\"localhost\" version_source=\"relay\"][event_node_router@48577 source=\"logrouter.example.com\" source_module=\"imrelp\" hostname=\"localhost\"][teragrep@48577 streamname=\"log:f17:0\" directory=\"com_teragrep_audit\" unixtime=\"1612047600.0\"] sigsegv\n"; + RFC5424ParserSubscription subscription = new RFC5424ParserSubscription(); + subscription.add(ParserEnum.MSG); + + InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes()); + RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, null); + + + Instant instant1 = Instant.now(); + long count = 10000000; + for (long i = 0; i < count; i++) { + Assertions.assertTrue(parser.next()); + inputStream.reset(); + } + Instant instant2 = Instant.now(); + + long msgsize = (count * SYSLOG_MESSAGE.length())/1024/1024; + long spent = instant2.toEpochMilli()-instant1.toEpochMilli(); + System.out.println("testOnlyMsgSub: time taken " + spent + " for " + count + + ", total RPS: " + (float) count/ ((float) spent/1000) + + ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); + + } + + @Test + @DisabledIfSystemProperty(named = "noPerfTest", matches = "true") + void testNothingSub() throws Exception { + + String SYSLOG_MESSAGE = "<14>1 2014-06-20T09:14:07.12345+00:00 host01 systemd DEA MSG-01 [ID_A@1 u=\"\\\"3\" e=\"t\"][ID_B@2 n=\"9\"][event_id@48577 hostname=\"sc-99-99-14-247\" uuid=\"0FD92E51B37748EB90CD894CCEE63907\" unixtime=\"1612047600.0\" id_source=\"source\"][event_node_source@48577 hostname=\"sc-99-99-14-247\" source=\"f17_ssmis_20210131v7.nc\" source_module=\"imfile\"][event_node_relay@48577 hostname=\"localhost\" source=\"sc-99-99-14-247\" source_module=\"imrelp\"][event_version@48577 major=\"2\" minor=\"2\" hostname=\"localhost\" version_source=\"relay\"][event_node_router@48577 source=\"logrouter.example.com\" source_module=\"imrelp\" hostname=\"localhost\"][teragrep@48577 streamname=\"log:f17:0\" directory=\"com_teragrep_audit\" unixtime=\"1612047600.0\"] sigsegv\n"; + RFC5424ParserSubscription subscription = new RFC5424ParserSubscription(); + InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes()); + RFC5424Parser parser = new RFC5424Parser(inputStream, subscription); + + Instant instant1 = Instant.now(); + long count = 10000000; + for (long i = 0; i < count; i++) { + Assertions.assertTrue(parser.next()); + inputStream.reset(); + } + Instant instant2 = Instant.now(); + + long msgsize = (count * SYSLOG_MESSAGE.length())/1024/1024; + long spent = instant2.toEpochMilli()-instant1.toEpochMilli(); + System.out.println("testNothingSub: time taken " + spent + " for " + count + + ", total RPS: " + (float) count/ ((float) spent/1000) + + ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); + + } + + @Test + @DisabledIfSystemProperty(named = "noPerfTest", matches = "true") + void testDashesFullSub() throws Exception { + + String SYSLOG_MESSAGE = "<2>1 - - - - - "; + InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes()); + RFC5424Parser parser = new RFC5424Parser(inputStream, null); + + Instant instant1 = Instant.now(); + long count = 100000000; + for (long i = 0; i < count; i++) { + Assertions.assertTrue(parser.next()); + inputStream.reset(); + } + Instant instant2 = Instant.now(); + + long msgsize = (count * SYSLOG_MESSAGE.length())/1024/1024; + long spent = instant2.toEpochMilli()-instant1.toEpochMilli(); + System.out.println("testDashesFullSub: time taken " + spent + " for " + count + + ", total RPS: " + (float) count/ ((float) spent/1000) + + ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); + + } + + @Test + @DisabledIfSystemProperty(named = "noPerfTest", matches = "true") + void testDashesNoSub() throws Exception { + + String SYSLOG_MESSAGE = "<2>1 - - - - - "; + RFC5424ParserSubscription subscription = new RFC5424ParserSubscription(); + InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes()); + RFC5424Parser parser = new RFC5424Parser(inputStream, subscription); + + Instant instant1 = Instant.now(); + long count = 100000000; + for (long i = 0; i < count; i++) { + Assertions.assertTrue(parser.next()); + inputStream.reset(); + } + Instant instant2 = Instant.now(); + + long msgsize = (count * SYSLOG_MESSAGE.length())/1024/1024; + long spent = instant2.toEpochMilli()-instant1.toEpochMilli(); + System.out.println("testDashesNoSub: time taken " + spent + " for " + count + + ", total RPS: " + (float) count/ ((float) spent/1000) + + ", " + (float) msgsize + " megabytes (" + (float) (msgsize/((float)spent/1000)) + " MB/s)"); } }