@Test public void testRegexSpeed() { incoming.put("passAllAddresses", true); Map<String, Object> regexParser = new LinkedHashMap<>(); String pattern = "<(\\d{1,3})>([1-9]\\d{0,2}) (?:(-)|(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\." + "\\d{1,6})?(?:Z|[-+]\\d{2}:\\d{2}))) (-|[!-~]{1,255}) (-|[!-~]{1,48}) (-|[!-~]{1,128}) (-|[!-~]{1,32}" + ") (?:(-)|((?:\\[[!-~&&[^ =\\]]]{1,32}(?: [!-~&&[^ =\\]]]{1,32}=\"(?:[\\u0000-\\uffff&&[^\"\\\\\\]]]|" + "(?:\\\\[\\\\\"\\]]))*\")*])+))(?: (?:((?!\\xef\\xbb\\xbf).*)|(?:\\xef\\xbb\\xbf([\\u0000-\\uffff]*))" + "))?"; regexParser.put("pattern", pattern); String[] locations = {"priority", "version", "noTimestamp", "timestamp", "hostName", "appName", "processId", "messageId", "noStructuredData", "structuredData", "standardMessage", "utfMessage"}; regexParser.put("locations", Arrays.asList(locations)); incoming.put("regexParser", regexParser); String testMessage = "<0>198 2018-07-23T15:26:20-07:00 remote iApp user branch [name] Here be a message for ya!"; byte[] packetBytes = testMessage.getBytes(); DatagramPacket packet= new DatagramPacket(packetBytes, packetBytes.length); assert Pattern.matches(pattern, testMessage); nHandler = new UDPNotificationHandler(incoming, fakeClient); nHandler.handleMessage(packet); int runCount = 100 * 1000; long currentTime = System.currentTimeMillis(); for (int i = 0; i < runCount; i ++) { packetBytes = testMessage.getBytes(); packetBytes[1] = (byte) ((i & 7) + 48); packet = new DatagramPacket(packetBytes, packetBytes.length); nHandler.handleMessage(packet); } long timeTaken = System.currentTimeMillis() - currentTime; System.out.println("Time taken for " + runCount + " messages was " + timeTaken + " ms"); System.out.println("This is " + ((double)timeTaken) / runCount + " ms per message and " + (int) (1.0 / timeTaken * runCount) + " thousand messages per second"); }
@Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); }
UDPNotificationHandler extends Handler<DatagramPacket> { @Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); } }
UDPNotificationHandler extends Handler<DatagramPacket> { @Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); } UDPNotificationHandler(Map incoming, ExtensionWebSocketClient client); }
UDPNotificationHandler extends Handler<DatagramPacket> { @Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); } UDPNotificationHandler(Map incoming, ExtensionWebSocketClient client); @Override void handleMessage(DatagramPacket packet); }
UDPNotificationHandler extends Handler<DatagramPacket> { @Override public void handleMessage(DatagramPacket packet) { Map receivedMsg = null; Map<String,Object> sendMsg = new LinkedHashMap<>(); if (bytesLocation != null || regexPattern != null) { } else if (expectingCsv) { try { List<Object> csv = mapper.readerFor(List.class).with(CsvSchema.emptySchema().withHeader()) .readValue(packet.getData()); client.sendNotification(csv); } catch (Exception e){ log.warn("Failed to interpret UDP message as CSV.", e); } return; } else{ try { receivedMsg = mapper.readValue(packet.getData(), Map.class); } catch (Exception e) { if (expectingXml) { log.warn("Failed to interpret UDP message as a XML object.", e); } else { log.warn("Failed to interpret UDP message as a JSON object.", e); } return; } } if (regexPattern != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); sendMsg = getRegexResults(recString); } else if (bytesLocation != null) { String recString = new String(packet.getData()); int endIndex = recString.indexOf("\0"); if (endIndex == -1) {endIndex = packet.getData().length;} recString = new String(packet.getData(), 0, endIndex, Charset.forName("UTF-8")); MapTransformer.createTransformVal(sendMsg, bytesLocation, recString); } else if (passingPureMap) { sendMsg = receivedMsg; } else if (passingUnspecified) { sendMsg = receivedMsg; if (this.transformer != null) { this.transformer.transform(receivedMsg, sendMsg, true); } } else if (transformer == null) { sendMsg = new LinkedHashMap<>(); } else { this.transformer.transform(receivedMsg, sendMsg, false); } if (expectingXml && xmlRootLoc != null) { try { FromXmlParser p = (FromXmlParser) mapper.getFactory().createParser(packet.getData()); MapTransformer.createTransformVal(sendMsg, xmlRootLoc, p.getStaxReader().getLocalName()); } catch (Exception e) { log.error("Failed to interpret name of root", e); } } if (recAddressKey != null) { MapTransformer.createTransformVal(sendMsg, recAddressKey, packet.getAddress().getHostAddress()); } if (recPortKey != null) { MapTransformer.createTransformVal(sendMsg, recPortKey, packet.getPort()); } client.sendNotification(sendMsg); } UDPNotificationHandler(Map incoming, ExtensionWebSocketClient client); @Override void handleMessage(DatagramPacket packet);  }