Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 
    }
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.