I have a socket which will be receiving more than 100 TPS, so I need the reading to be as fast as possible. In this socket, I read the info I need to build a String and check if it matches a regex. I need to read char by char, so I can know where a new message is sent.
But I'm having performance issues. I'm testing it with JMeter and after some time the throughput starts going down. So far, this is the best way I've found to read the elements and classify them (char by char), because the only way I know when a message new message starts/ends is the NULL.
Here is my code:
//Keep reading info from the socket
while (running) {
try {
Future<Integer> readFuture = getWorker().read(buffer);
Integer option = readFuture.get();
if (option > 0) {
buffer.flip();
//Read all the info from the buffer
while (buffer.hasRemaining()) {
char buf = (char) buffer.get();
if (buf == '/') {
continue;
}
//If the info has one of those char, it's a new message, so I have to manage and keep reading
if (buf == '\0' || buf == ' ') {
trace=sendMessage(trace);
} else {
trace.append(buf);
}
}
buffer.clear();
} else {
trace=sendMessage(trace);
running = false;
close();
}
} catch (Exception e) {
//Error management
}
}