Skip to content

Instantly share code, notes, and snippets.

@hoerup
Last active October 16, 2018 21:29
Show Gist options
  • Save hoerup/e2ed46bae757ac7bbc507e26612f4d57 to your computer and use it in GitHub Desktop.
Save hoerup/e2ed46bae757ac7bbc507e26612f4d57 to your computer and use it in GitHub Desktop.
Pircbotx -> influx udp performance log
diff --git a/src/main/java/org/pircbotx/InfluxLogger.java b/src/main/java/org/pircbotx/InfluxLogger.java
new file mode 100644
index 0000000..9a8da03
--- /dev/null
+++ b/src/main/java/org/pircbotx/InfluxLogger.java
@@ -0,0 +1,45 @@
+package org.pircbotx;
+
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+
+import org.apache.commons.lang3.time.StopWatch;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class InfluxLogger {
+ StopWatch sw = new StopWatch();
+ String measurement;
+
+ public InfluxLogger(String measurement) {
+ this.measurement = measurement;
+ sw.start();
+ }
+
+ public void Stop() {
+ sw.stop();
+
+ String line = "" + measurement + " elapsed=" + sw.getNanoTime();
+ byte buffer[] = line.getBytes();
+
+ final short port = 9999;
+
+ byte [] IP={(byte)192,(byte)168,(byte)12,(byte)112};
+
+ try {
+ InetAddress address = InetAddress.getByAddress(IP);
+ DatagramPacket packet = new DatagramPacket(
+ buffer, buffer.length, address, port);
+ ;
+ DatagramSocket datagramSocket = new DatagramSocket();
+ datagramSocket.send(packet);
+ datagramSocket.close();
+
+ } catch (Exception e) {
+ log.warn("Error sending udp packet");
+ }
+
+ }
+}
diff --git a/src/main/java/org/pircbotx/PircBotX.java b/src/main/java/org/pircbotx/PircBotX.java
index 6c1c9d0..a6d986e 100644
--- a/src/main/java/org/pircbotx/PircBotX.java
+++ b/src/main/java/org/pircbotx/PircBotX.java
@@ -17,9 +17,6 @@
*/
package org.pircbotx;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
-import com.google.common.primitives.Ints;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
@@ -35,6 +32,27 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.commons.lang3.StringUtils;
+import org.pircbotx.dcc.DccHandler;
+import org.pircbotx.exception.IrcException;
+import org.pircbotx.hooks.ListenerAdapter;
+import org.pircbotx.hooks.events.ConnectAttemptFailedEvent;
+import org.pircbotx.hooks.events.ConnectEvent;
+import org.pircbotx.hooks.events.DisconnectEvent;
+import org.pircbotx.hooks.events.ExceptionEvent;
+import org.pircbotx.hooks.events.OutputEvent;
+import org.pircbotx.hooks.events.SocketConnectEvent;
+import org.pircbotx.output.OutputCAP;
+import org.pircbotx.output.OutputDCC;
+import org.pircbotx.output.OutputIRC;
+import org.pircbotx.output.OutputRaw;
+import org.pircbotx.snapshot.UserChannelDaoSnapshot;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import com.google.common.primitives.Ints;
+
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@@ -42,16 +60,6 @@
import lombok.Setter;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.pircbotx.dcc.DccHandler;
-import org.pircbotx.exception.IrcException;
-import org.pircbotx.hooks.ListenerAdapter;
-import org.pircbotx.hooks.events.*;
-import org.pircbotx.output.OutputCAP;
-import org.pircbotx.output.OutputDCC;
-import org.pircbotx.output.OutputIRC;
-import org.pircbotx.output.OutputRaw;
-import org.pircbotx.snapshot.UserChannelDaoSnapshot;
/**
* PircBotX is a Java framework for writing IRC bots quickly and easily.
@@ -388,6 +396,7 @@
line = null;
}
}
+ InfluxLogger influx = new InfluxLogger("read");
if (Thread.interrupted()) {
log.error("--- PircBotX interrupted during read, aborting reconnect loop and shutting down ---");
@@ -415,6 +424,8 @@
return false;
}
+ influx.Stop();//stop and save data
+
return true;
}
diff --git a/src/main/java/org/pircbotx/output/OutputRaw.java b/src/main/java/org/pircbotx/output/OutputRaw.java
index 670507f..a921d85 100644
--- a/src/main/java/org/pircbotx/output/OutputRaw.java
+++ b/src/main/java/org/pircbotx/output/OutputRaw.java
@@ -28,6 +28,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
+import org.pircbotx.InfluxLogger;
import org.pircbotx.PircBotX;
import org.pircbotx.Utils;
import org.slf4j.Marker;
@@ -60,6 +61,7 @@
* @param line The raw line to send to the IRC server.
*/
public void rawLine(String line) {
+ InfluxLogger influx = new InfluxLogger("write");
checkArgument(StringUtils.isNotBlank(line), "Cannot send empty line to server: '%s'", line);
checkArgument(bot.isConnected(), "Not connected to server");
@@ -85,6 +87,7 @@
} finally {
writeLock.unlock();
}
+ influx.Stop();
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment