1

I have been testing the performance of reading server socket input stream. I found that reading itself has about ~9000 nano second (9 us) delay. How can I improve its performance? I have tried JAVA default buffer size and something a bit bigger than that. But its performance is not much changed. In this case, I counted every 1600 samples from the client.

Most delay is found in this line:

inputLine = in.readLine();

Here is the code of reading thread:

PrintWriter out = new PrintWriter(door.getOutputStream(), true);            
BufferedReader in = new BufferedReader(new InputStreamReader(door.getInputStream()), 10240);           

File file = new File(this.storageDirectory);
BufferedWriter save = new BufferedWriter(new FileWriter(file));

String inputLine = null;

while(Switch)
{
    inputLine = in.readLine(); //I found ~9000 ns delay in this line.
    save.write(inputLine);


    if(iCounter==0)
        StartTime = System.nanoTime();                   

    iCounter++;                      


    if(iCounter>=1600) //need to store them.
    {
        EndTime = System.nanoTime();
        TimeTick = (EndTime - StartTime)/1600;
        System.out.println("TimeTick is [ns]: " + TimeTick);                    
        iCounter = 0;
    }
 }

Here is the result:

TimeTick is [ns]: 9241
TimeTick is [ns]: 9941
TimeTick is [ns]: 6535
.....

Here is the result without 'inputLine = in.readLine();'

TimeTick is [ns]: 0
TimeTick is [ns]: 0
TimeTick is [ns]: 1
......

How can I improve reading performance? The client is just sending random double point values upon request.

Thanks,

user1098761
  • 579
  • 1
  • 5
  • 16

1 Answers1

1
String inputLine = null;

while (...) {
    ...
    inputLine = in.readLine(); //I found ~9000 ns delay in this line.
    save.write(inputLine);
    ...
}

If you comment out the read line, you'll have nothing to write, so in effect you're commenting out both lines, no?

Your micro-benchmarks (which seem a bit dodgy to begin with) would probably reveal better information if you had some default string which save.write wrote in each iteration.

Good and relevant question and answer:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • The purpose of this is making the server very responsive. I need to read and write from the client that generates 1600 samples with 1 kHz trigger. Then each time tick of sample must be 0.001/1600 = 0.000625 ms (0.625 us). So, 9000 ns (9 us) is a quite big value. – user1098761 Mar 26 '12 at 20:17
  • I see. Good luck with your optimizations. – aioobe Mar 26 '12 at 20:24
  • @user1098761 It's 1.5% of the bandwidth. Is that really a big value? – user207421 Mar 26 '12 at 21:59