当消息大小很大时,socketchannel.write()变得非常慢 [英] socketchannel.write() becomes very slow when message size is large

查看:519
本文介绍了当消息大小很大时,socketchannel.write()变得非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用java nio的程序中,socketchannel.write()在尝试连续写入10 KB消息时变得非常慢。写入完整10 KB消息的测量时间介于160 ms和200 ms之间。但是编写一个完整的5 KB消息的时间只需要0.8毫秒。

In my program using java nio, the socketchannel.write() becomes very slow when it tries to write 10 KB messages consecutively. The measured time for writing a complete 10 KB message is between 160 ms and 200 ms. But the time for writing a complete 5 KB message is only takes 0.8 ms.

在选择器中,我只有Selection.OP_READ并且不处理Selection.OP_WRITE。当收到大的完整消息时,它会被写入另一个接收器4次。

In the selector, I only have Selection.OP_READ and do not handle Selection.OP_WRITE. When a large complete message is received, it is written to another receiver 4 times.

有人遇到同样的问题吗?有关于socketchannel.write()的帖子很慢。我的问题是如何在OP_READ和OP_WRITE之间进行替换?

Is anyone accounter same problem? There is a post about socketchannel.write() slow. My question is how to alternate change between OP_READ and OP_WRITE?

如果我添加一个inerval,例如150毫秒,则响应时间会减少。有什么办法可以找到缓冲区已满的时候,我可以让程序等待。我的操作系统是windows xp。

If I add an inerval e.g, 150 ms, the response time is reduced. Is there any way to find when the buffer is full so I can let the program waits. My operating system is windows xp.

谢谢。

我通过检查写入的字节数来遵循EPJ的建议。但响应时间仍然很长。我在这里发布了部分代码,并想检查我的代码是否有问题。

I follow EPJ suggestion by checking the number of written bytes. But the response time is still high. I post part of my code here and would like to examine whether there is wrong with my code.

//这是使用nio的writeData()部分:

// this is the writeData() part using nio:

      while (buffer.hasRemaining()) {   
        try {           
                buffer.flip();                  
                n = socket.write(buffer);           
                if(n == 0) {                
                    key.interestOps(SelectionKey.OP_WRITE);
                    key.attach(buffer);             
                    break;
                }                               
        } catch (IOException e) {               
            e.printStackTrace();
        } finally {
            buffer.compact();
        }
    }   

    if(buffer.position()==0) {                  
        key.interestOps(SelectionKey.OP_READ);
    }


推荐答案

如果写入超过20微秒,我建议你有一个缓冲区问题。我假设您正在使用阻止NIO。当发送缓冲区未满时,通常需要5到20微秒。在过去,我已经配置我的服务器来杀死任何需要2毫秒写入的慢速消费者。 (可能有点激进。;)

If write takes more than 20 micro-seconds, I would suggest you have a buffer full issue. I assume you are using blocking NIO. When the send buffer is not full it usually takes between 5 - 20 micro-seconds. In the past I have configured my server to kill any slow consumer which takes 2 ms to write. (Possibly a bit aggressive. ;)

您可以尝试增加发送缓冲区的大小(Socket.setSendBufferSize(int),也可用于SocketChannels),但是看起来你试图发送的数据超过你的带宽所允许的数据。

You could try increasing the size of the send buffer (Socket.setSendBufferSize(int), which is also available for SocketChannels), but it would appear you are trying to send more data than your bandwidth allows.

10 KB不是一个大的消息,典型的发送缓冲区大小是64 KB,所以对它来说为了满满,您需要有6-7条消息未发送。这可能解释了5KB相对较快的方式。

10 KB is not a large message, the typical send buffer size is 64 KB, so for it to be full you would need to have 6-7 messages unsent. This might explain way 5KB is relatively fast.

这篇关于当消息大小很大时,socketchannel.write()变得非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆