我想测试客户端程序和服务器程序之间的交互是否存在竞争条件。它们通过 tcp 相互连接。客户端在多个线程上对服务器进行阻塞调用(每个线程与服务器建立一次 tcp 连接)。我想测试竞争条件,其中一个阻塞调用先于另一个阻塞调用完成。
为了实现这一点,我希望以不同的量延迟 tcp 连接(这样我就不必明确重写客户端或服务器)。
我希望做这样的事:
socat tcp-listen:$pin system:"delaycat $delay |socat - 'tcp:localhost:$pout'"
其中$pin
是客户端连接的端口,$delay
是延迟的秒数,$pout
是服务器监听的端口。delaycat
是一个将输入流延迟 n 秒的虚构程序。
有没有现成的程序可以实现我想要的功能?或者我应该自己写一个delaycat
?
编辑:请注意,系统范围的延迟实际上并不是理想的,因为如果可能的话,我想将各个插座的延迟量设置为不同的量。
答案1
我找不到任何现有的内容,因此我编写了一个 python 脚本来执行此操作(这可能有缺陷):
#!/usr/bin/env python26
"""Add latency to a tcp connection"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from functools import partial
from twisted.internet.reactor import (
listenTCP, connectTCP, callLater, run
)
from twisted.internet.protocol import (
ServerFactory, ReconnectingClientFactory, Protocol
)
class ConnectionToServer(Protocol):
def __init__(self, connection_from_client, delay_to_client):
self._delay_to_client = delay_to_client
self._connection_from_client = connection_from_client
def connectionMade(self):
self._connection_from_client.connection_to_server = self
if self._connection_from_client.buffer_from_client:
self.transport.write(
self._connection_from_client.buffer_from_client
)
def dataReceived(self, data):
callLater(
self._delay_to_client,
self._connection_from_client.transport.write, data
)
def connectionLost(self, reason):
callLater(
self._delay_to_client,
self._connection_from_client.transport.loseConnection
)
class ConnectionFromClient(Protocol):
def __init__(self, server_host, server_port, delay_to_client, delay_to_server):
self._delay_to_server = delay_to_server
self.connection_to_server = None
self.buffer_from_client = ''
server_connection_factory = ReconnectingClientFactory()
server_connection_factory.protocol = partial(
ConnectionToServer, self, delay_to_client
)
self._server_connector = connectTCP(
server_host, server_port, server_connection_factory
)
def dataReceived(self, data):
callLater(self._delay_to_server, self._write, data)
def connectionLost(self, reason):
callLater(
self._delay_to_server, self._server_connector.disconnect
)
def _write(self, data):
if self.connection_to_server:
self.connection_to_server.transport.write(data)
else:
self.buffer_from_client += data
def main():
"""Add latency to a tcp connection"""
parser = ArgumentParser(
description=main.__doc__,
formatter_class=ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'client_port', type=int, help='client connects to this port'
)
parser.add_argument(
'server_port', type=int, help='server listens on this port'
)
parser.add_argument(
'-t', '--server-host', default='localhost',
help='server is running on this host'
)
parser.add_argument(
'-c', '--delay-to-client', default=0, type=float,
help='messages to client are delayed by this many seconds'
)
parser.add_argument(
'-s', '--delay-to-server', default=0, type=float,
help='messages to server are delayed by this many seconds'
)
args = parser.parse_args()
client_connection_factory = ServerFactory()
client_connection_factory.protocol = partial(
ConnectionFromClient, args.server_host, args.server_port,
args.delay_to_client, args.delay_to_server
)
listenTCP(args.client_port, client_connection_factory)
run()
if __name__ == '__main__':
main()
答案2
在 Linux 机器上,你可以使用 tc奈特姆。
例如,命令tc qdisc add dev eth0 root netem delay 100ms
将延迟全部传出数据包延迟 100 毫秒。要延迟传入数据包,您可以使用ifb——中级功能块