我知道这是个奇怪的问题。因为我通常基于“假设”所有用户的网络连接都很慢来开发应用程序。但是,有没有人认为有一种方法可以通过编程模拟慢速互联网连接,这样我就可以“看到”应用程序在不同“连接速度”下的表现?

我不担心使用哪种语言。我不是在寻找代码示例或其他东西,只是对它背后的逻辑感兴趣。


当前回答

此外,为了在某些*nix上模拟慢速连接,您可以尝试使用ipfw。更多信息由Ben Newman在Quora上的回答提供

其他回答

使用TCPMon之类的工具。它可以假装连接很慢。

基本上,你向它请求完全相同的东西,它只是将完全相同的请求转发给真实服务器,然后仅用设置的字节数延迟响应。

Mac OSX从10.10开始就有了一个名为Murus Firewall的应用程序,它作为pf的GUI,替代了ipfw。

它适用于系统范围或特定领域的节流。我只能用它把我的下载速度在300Kbps到30Mbps之间滑动,以测试流媒体视频播放器是如何调整的。

形成单个TCP连接的一种常见情况实际上可以按照UNIX方式从socat和cpipe的对偶组合起来,如下所示:

socat TCP- listen:5555,reuseaddr,reuseport,fork SYSTEM:'cpipe -ngr -b 1 -s 10 | socat - "TCP:localhost:5000" | cpipe -ngr -b 1 -s 300'

这模拟了一个带宽约为300kB/s的连接,从您的服务在:5000到大约10kB/s,并监听:5555的传入连接。注意:注意这是每个连接,所以每个单独的TCP连接都得到这个量。

Explanation: The outer (left) socat listens with the given options on :5555 as a forking server. The first cpipe command in the SYSTEM:... option then throttles data that went into socket :5555 (and comes out of the first, outer socat) to at most 10kByte/s. That data is then forwarding using another socat which connects to localhost:5000 (where the service you want to slow down should be listening). Data from localhost:5000 is then put into the right cpipe command, which (with the given values) throttles it to about 300kB/s.

cpipe的-ngr选项很重要。它导致cpipe非贪婪地从其输入文件描述符中读取。否则,您可能会遇到缓冲区中的数据未被转发并等待回复的情况。

使用更常见的缓冲工具而不是cpipe也是可能的。

(图片来源:这是基于socat文档中Christophe Loor的“双tee”配方)

我使用http://www.netlimiter.com/,它工作得很好。不仅限制单个进程的速度,而且还显示实际的传输速率。

在Linux机器上你可以使用wondershaper

apt-get install wondershaper

$ sudo wondershaper {interface} {down} {up}

{down}和{up}是KPBS中的带宽

例如,如果你想限制接口eth1的上行带宽为256kbps,下行带宽为128kbps,

$ sudo wondershaper eth1 256 128

为了清除极限,

$ sudo wondershaper clear eth1