我正在尝试构建一个带宽测试工具,有点像 IPerf,但在 Java 中,我得到的数据包丢失似乎比预期的要多,但是在略高的带宽下(从大约 30-40Mb/s 开始),我希望有人可能会指出一些优化或我做错的事情会导致我丢失数据包。
这是接收代码,它将大小为 2000 的队列交给另一个收集指标的类,它只传递数据包中的相关信息。使用 NIO
while (data.isRunning())
{
if(channel.receive(buf) != null)
{
int j = buf.array().length;
//add the packets important information to the queue
packet_info.add(new PacketInfoContainer(buf.getLong(j-12), System.nanoTime(), buf.getInt(j-4)));
// if we have 2000 packets worth of information, time to handle it!
if((packet_info.size() == 2000))
{
Runnable r1;
//if this is running on the client side, do it this way so that we can calculate progress
if(client_side)
{
if(data_con.isUserRequestStop())
{
System.out.println("suposed to quit");
data.stopTest();
break;
}
if(packets_expected > 0)
{
total_packets_received+=1000;
setChanged();
notifyObservers("update_progress" + Integer.toString( (int) (((double)total_packets_received/(double)packets_expected) * 1000) ) );
}
r1 = new PacketHandler(packet_info, results, buffer_size, client);
}
//server side, no nonsense
else
{
r1 = new PacketHandler(packet_info, results, buffer_size);
}
pool.submit(r1);
packet_info = new LinkedList<PacketInfoContainer>();
}
}
buf.clear();
最佳答案
UDP 不是很好......也许你可以使用 TCP 并检查 S.O. 的 tcp 统计信息。查看重传...
netstat -s
你可以使用CharacterGenerator , 将 BufferedOutputStream 更改为 64KB 并删除 os.flush();加速和测试...
关于java高速udp流量包接收带宽测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770553/