套接字的速度在电脑上是120MB/s,这是正常的吗?

Fre*_*ind 11 java sockets performance

我测试了在一台计算机上通过套接字将数据从一个程序传输到另一个程序的性能,速度是120MBytes/s,这是正常的吗?

我的服务器和客户端程序都非常简单.

我的电脑是AMD Athlon X2 4000 +,4G DDR2 667内存,带有windows xp sp3.

我的朋友说它很慢,应该更快.但我不知道如何改进它们,或者是否有其他库可以尝试获得更好的速度?

UPDATE

服务器和客户端程序都在我自己的计算机上,一台计算机上.该网卡将限制速度或不?


Server.java

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(6666);
        Socket socket = server.accept();
        OutputStream output = socket.getOutputStream();

        byte[] bytes = new byte[10 * 1024]; // 10K
        for (int i = 0; i < bytes.length; i++) { bytes[i] = 12; } // fill the bytes

        // send them again and again
        while (true) {
            output.write(bytes);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Client.java

public class SimpleClient {

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("127.0.0.1", 6666);
        InputStream input = socket.getInputStream();
        long total = 0;
        long start = System.currentTimeMillis();

        byte[] bytes = new byte[10240]; // 10K

        // read the data again and again
        while (true) {
            int read = input.read(bytes);
            total += read;
            long cost = System.currentTimeMillis() - start;
            if (cost > 0 && System.currentTimeMillis() % 1000 == 0) {
                 System.out.println("Read " + total + " bytes, speed: " + (total / (1024.0*1024)) / (cost / 1000.0) + " MB/s");
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Pet*_*rey 12

你能告诉我这个程序的输出吗?

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(6666);
        Socket socket = server.accept();
        OutputStream output = socket.getOutputStream();

        byte[] bytes = new byte[32*1024]; // 32K
        while (true) {
            output.write(bytes);
        }
    }
}
public class SimpleClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("127.0.0.1", 6666);
        InputStream input = socket.getInputStream();
        long total = 0;
        long start = System.currentTimeMillis();

        byte[] bytes = new byte[32*1024]; // 32K
        for(int i=1;;i++) {
            int read = input.read(bytes);
            if (read < 0) break;
            total += read;
            if (i % 500000 == 0) {
                long cost = System.currentTimeMillis() - start;
                System.out.printf("Read %,d bytes, speed: %,d MB/s%n", total, total/cost/1000);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的机器打印

Read 25,586,204,672 bytes, speed: 5,245 MB/s
Read 53,219,426,304 bytes, speed: 5,317 MB/s
Read 85,018,968,064 bytes, speed: 5,416 MB/s
Read 117,786,968,064 bytes, speed: 5,476 MB/s
Run Code Online (Sandbox Code Playgroud)

尝试多次发送32K块(至少2秒),你应该得到400 MB/s或更多.例如至少10,000次.

在非常快的机器上,您可以在单个客户端上获得1 GB/s的速度.使用多个客户端,您可能会获得8 GB/s.

这是一个例子

使文件传输更高效Java


如果你有100 Mb卡,你可以期望大约11 MB/s(每秒字节数).

类似地,对于1 Gb以太网,您预计大约110 MB/s

对于10 Gig-E以太网,您可能会达到1 GB/s,但是您可能只有一半的这个unles syour系统已经过高度调整.

  • 我使用的是华硕Maximus IV,4.6 GHz的i7 2700K,16 GB的1600 MHz DDR3,OCZ Vertex 3 240GB SSD,以及Centos 5.7. (2认同)

Pau*_*sik 10

您只传输10k字节来执行测试.您正在做的很多事情都会产生开销,而这样一小部分数据可能会受到这种开销的影响.(例如,创建套接字,分配内存等).您应该创建并传输更大的数据集(几十MB +),以更真实地了解正在发生的事情.这样做会使开销成为数据传输过程中较小的,不太重要的部分.

您还应该多次执行此测试(> 10)并取平均值,因为您的计算机将在不同的随机时间点处受到来自服务,网络传输等的负载,这些负载将影响您的传输速率.通过10次迭代,您还可以删除异常缓慢的任何运行时间,例如> 2个标准差.

  • 不,他不是.写入是在无限循环中完成的,也是在读取中完成的. (3认同)
  • +1用于指出用于分析测试的小数据样本.记录采样循环内的运行时间也会以随机,不可预测的方式扭曲测试. (2认同)

Ste*_*n C 5

网卡会限制速度吗?

您正在使用127.0.0.1 - 本地/环回地址.来自/到该地址的流量不通过网卡.

这意味着这不是衡量实际网络吞吐量的有效方法.但是,它是衡量Java通过本地网络堆栈...在您的计算机上移动数据的能力的合理指标.


有趣的是将您获得的结果与用C或C++编写的等效客户端/服务器对进行比较.