如何提高插座读取性能?
问题描述:
我一直在测试读取服务器套接字输入流的性能。我发现阅读本身有大约9000纳秒(9 us)的延迟。我怎样才能提高其表现? 我已经尝试过JAVA的默认缓冲区大小和比这大一点的东西。但其表现并没有太大变化。在这种情况下,我计算了来自客户端的每1600个样本。如何提高插座读取性能?
多数延迟在此行中找到:
inputLine = in.readLine();
这里是读线程的代码:
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;
}
}
下面是结果:
TimeTick is [ns]: 9241
TimeTick is [ns]: 9941
TimeTick is [ns]: 6535
.....
下面是结果没有'inputLine = in.readLine();'
TimeTick is [ns]: 0
TimeTick is [ns]: 0
TimeTick is [ns]: 1
......
如何提高阅读效果?客户只需根据请求发送随机双点值。
感谢,
答
String inputLine = null;
while (...) {
...
inputLine = in.readLine(); //I found ~9000 ns delay in this line.
save.write(inputLine);
...
}
如果您注释掉读取行,你就什么都没有写,所以实际上你注释掉两行,不是吗?
如果您有一些save.write
在每次迭代中写入的默认字符串,您的微基准(可能会显得有点不妥)。
好的相关问题的答案:
这样做的目的是使服务器非常敏感。我需要从客户端读取和写入1千个触发器产生1600个采样。然后每次采样点必须为0.001/1600 = 0.000625 ms(0.625 us)。所以,9000 ns(9 us)是一个相当大的值。 – user1098761 2012-03-26 20:17:41
我明白了。祝你好运,你的优化。 – aioobe 2012-03-26 20:24:45
@ user1098761它是带宽的1.5%。这真的是一个很大的价值吗? – EJP 2012-03-26 21:59:32