简体   繁体   English

如何提高套接字读取性能?

[英]How to improve socket reading performance?

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. 我发现读取本身大约有9000纳秒(9 us)的延迟。 How can I improve its performance? 如何改善其性能? I have tried JAVA default buffer size and something a bit bigger than that. 我尝试过JAVA默认缓冲区大小,并且比这更大。 But its performance is not much changed. 但是其性能并没有太大变化。 In this case, I counted every 1600 samples from the client. 在这种情况下,我对来自客户端的每1600个样本进行了计数。

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();' 这是没有'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, 谢谢,

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. 如果您有一些默认字符串,在每次迭代中都save.writesave.write ,那么您的微基准测试(一开始似乎有点狡猾)可能会揭示更好的信息。

Good and relevant question and answer: 良好且相关的问答:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM