gpt4 book ai didi

java - 加快我的 Java tcp 传输速度!

转载 作者:行者123 更新时间:2023-12-01 16:11:10 25 4
gpt4 key购买 nike

我需要加快千兆位以太网连接的传输速度。现在,我正在做几乎与此完全相同的事情,但当我运行下面的代码时,我只看到了大约 40%。

在测试之前,我还在我的所有(Mac Pro)机器上运行了这个脚本

#!/bin/bash

sudo sysctl -w net.inet.tcp.win_scale_factor=8
sudo sysctl -w kern.ipc.maxsockbuf=16777216
sudo sysctl -w net.inet.tcp.sendspace=8388608
sudo sysctl -w net.inet.tcp.recvspace=8388608

实际代码如下:

import java.io.*;
import java.nio.*;
import java.net.*;

public class BandwidthTester {
private static final int OUT_BUF = (1 << 17),
IN_BUF = (1 << 17), SEND_BUF = (1 << 22), RECV_BUF = (1 << 22);
public static void main(String[] args) {
try {
// server
if (args.length == 0) {
ServerSocket sock = new ServerSocket();
sock.bind(new InetSocketAddress(41887));

// wait for connection
Socket s = sock.accept();

s.setSendBufferSize(SEND_BUF);

System.out.println("Buffers: " + s.getSendBufferSize() + " and " + s.getReceiveBufferSize());

sock.close();

BufferedOutputStream bOut = new BufferedOutputStream(s.getOutputStream(), OUT_BUF);

// send lots of data
sendLotsOfData(bOut);
} else if (args.length == 2) {
String host = args[0];
int port = Integer.parseInt(args[1]);

System.out.println("Connecting to " + args[0] + ":" + args[1]);

Socket sock = new Socket();
sock.setReceiveBufferSize(RECV_BUF);
sock.connect(new InetSocketAddress(host, port));

System.out.println("Buffers: " + sock.getSendBufferSize() + " and " + sock.getReceiveBufferSize());

BufferedInputStream bIn = new BufferedInputStream(sock.getInputStream(), IN_BUF);
getLotsOfData(bIn);
}
} catch (Exception e) {
e.printStackTrace();
}
}


public static void getLotsOfData(InputStream in) {
System.out.println("Getting data...");
try {
long start = System.currentTimeMillis();

ByteBuffer intConv = ByteBuffer.allocate(4);

in.read(intConv.array());
int len = intConv.getInt(0);
for (int i=0; i < len; i++) {
in.read(intConv.array());
int val = intConv.getInt(0);
}

long end = System.currentTimeMillis();

double elapsed = ((double)(end - start)) / (1000.0);

System.out.println("Read in " + elapsed + " seconds: " + ( (4.0*8.0*len/elapsed) + " bits per second"));
} catch (Exception e) {
e.printStackTrace();
}
}

public static void sendLotsOfData(OutputStream out) {
System.out.println("Sending data...");
try {
long start = System.currentTimeMillis();

int len = (1 << 29);

ByteBuffer intConv = ByteBuffer.allocate(4);
intConv.putInt(0, len);
out.write(intConv.array());
for (int i=0; i < len; i++) {
intConv.putInt(0, i);
out.write(intConv.array());
}

out.flush();

long end = System.currentTimeMillis();

double elapsed = ((double)(end - start)) / (1000.0);

System.out.println("Sent in " + elapsed + " seconds: " + ( (4.0*8.0*len/elapsed) + " bits per second"));
}
catch (Exception e) {
e.printStackTrace();
}
}
}

有什么建议吗?发送所有这些数据大约需要 42 秒,但即使是 10% 的改进也会对我的程序产生巨大影响。

最佳答案

您可以尝试的一件事是为 ByteBuffer 使用更大的缓冲区。从 4 字节变为 16 字节,传输时间从 12 秒变为 9 秒。 (使用 2^26 而不是 2^29 进行长度测试)

也就是说,它是在本地运行的;因此应该不会遇到实际的网络问题。

有些肮脏的修改发送代码:

ByteBuffer intConv = ByteBuffer.allocate(16);
intConv.putInt(0, len);
out.write(intConv.array(),0,4);
for (int i=0; i < len; i+=4) {
for(int j=0; j<4; j++)
intConv.putInt(4*j, i);
out.write(intConv.array());
}

并接收:

ByteBuffer intConv = ByteBuffer.allocate(16);
in.read(intConv.array(),0,4);
int len = intConv.getInt(0);
for (int i=0; i < len; i+=4) {
in.read(intConv.array());
for(int j=0; j<4; j++)
{
int val=intConv.getInt(j*4);
}
}

显然,接收端需要进行一些修改来处理奇怪的情况,例如“如果只有 3 个剩余/从流中读取的整数会怎样”,但我认为这足以看看它是否可以提高性能。

关于java - 加快我的 Java tcp 传输速度!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1186388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com