基准测试中的许多 TCPClient 没有正确关闭
我目前正在为我的 TCP-Socket 服务器编写基准测试。 基本概念如下:
- 客户端创建 10000 个连接
- 有 2500 个并发连接
- 它们都向服务器发送 10 秒乒乓消息并接收 pong
- 10 秒后它们都断开连接
当我使用较小数量的连接(100 个并发和 1000 个连接)一切正常,但通过上述设置,某些连接在服务器上保持连接状态。 这意味着关闭呼叫根本不会到达服务器。
以下是上述解释的代码:
class Program {
static List<Thread> mConnectionThreads_ = new List<Thread>(); //!< The list of the Threads for all textloaders
static List<TCPConnection> mConnections_ = new List<TCPConnection>(); //!< The list of TextsXMLParser
static void Main(string[] args) {
int numConnections = 10000;
int numConcurrentConnections = 2500;
for( int k = 0; k < numConnections/numConcurrentConnections; ++k) {
for( int i = 0; i < numConcurrentConnections; ++i ) {
TCPConnection connection = new TCPConnection();
connection.connect(((k+1)*numConcurrentConnections)+i);
mConnections_.Add(connection);
mConnectionThreads_.Add(new Thread(connection.pingLoop));
}
Console.WriteLine(((k+1)*numConcurrentConnections) + "/" + numConnections + " Threads connected");
// start all threads
foreach (Thread t in mConnectionThreads_)
t.Start();
foreach (Thread t in mConnectionThreads_)
t.Join();
foreach (TCPConnection c in mConnections_)
c.disconnect();
Console.WriteLine(((k+1)*numConcurrentConnections) + "/" + numConnections + " Threads disconnected " + cnt + " calls");
mConnections_.Clear();
mConnectionThreads_.Clear();
}
}
}
断开连接函数如下所示:
public void disconnect() {
if( mClient_.Client != null ) {
mClient_.Client.Disconnect(false);
//mClient_.GetStream().Close();
//mClient_.Close();
Console.WriteLine("closed " + mConnectionId_);
}
else if( mClient_.Client == null )
Console.WriteLine("closed invalid " + mConnectionId_);
}
正如您所看到的,我已经尝试了很多不同的关闭方法,但更有效。
在这种情况下我能做些什么吗?还有其他人有同样的问题吗?
I'm currently programing a benchmark for my TCP-Socket Server.
The basic concept is the following:
- The Client creates 10000 connections
- There are 2500 connections concurrent
- They all send 10 seconds ping-pong messages to the server and receive the pong
- After the 10 seconds they all disconnect
When I use smaller numbers of connections (100 concurrent and 1000 connections) everything works fine, but with the setup above, some of the connections remain connected at the server.
This means that the close call never reaches the server at all.
Here is the code for the explanation above:
class Program {
static List<Thread> mConnectionThreads_ = new List<Thread>(); //!< The list of the Threads for all textloaders
static List<TCPConnection> mConnections_ = new List<TCPConnection>(); //!< The list of TextsXMLParser
static void Main(string[] args) {
int numConnections = 10000;
int numConcurrentConnections = 2500;
for( int k = 0; k < numConnections/numConcurrentConnections; ++k) {
for( int i = 0; i < numConcurrentConnections; ++i ) {
TCPConnection connection = new TCPConnection();
connection.connect(((k+1)*numConcurrentConnections)+i);
mConnections_.Add(connection);
mConnectionThreads_.Add(new Thread(connection.pingLoop));
}
Console.WriteLine(((k+1)*numConcurrentConnections) + "/" + numConnections + " Threads connected");
// start all threads
foreach (Thread t in mConnectionThreads_)
t.Start();
foreach (Thread t in mConnectionThreads_)
t.Join();
foreach (TCPConnection c in mConnections_)
c.disconnect();
Console.WriteLine(((k+1)*numConcurrentConnections) + "/" + numConnections + " Threads disconnected " + cnt + " calls");
mConnections_.Clear();
mConnectionThreads_.Clear();
}
}
}
The disconnect function looks like the following:
public void disconnect() {
if( mClient_.Client != null ) {
mClient_.Client.Disconnect(false);
//mClient_.GetStream().Close();
//mClient_.Close();
Console.WriteLine("closed " + mConnectionId_);
}
else if( mClient_.Client == null )
Console.WriteLine("closed invalid " + mConnectionId_);
}
As you can see I've already tried a lot of different close methods, but neighter works.
Is there anything I can do in this case? Anybody else having the same issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我错过了一些东西,但是 mClient_.Client 是什么类型?
通常如果您使用 TCP 客户端(TCPClient 类),您可以调用 Close 来关闭连接。以同样的方式,当直接使用 Socket 或 NetworkStream 时,您也可以调用 Close。
另一方面,您正在检测和调试服务器上的连接打开/关闭连接,对吧?服务器代码可能无法正确处理连接关闭,因此您会得到不正确的统计信息。
此外,在重负载下,服务器可能没有足够的 CPU 时间来更新连接状态,因此您可能会出现一些延迟。您的服务器是否使用异步 I/O 或每线程连接原则?
Maybe I'm missing something but what type has mClient_.Client?
Usually if you use TCP client (TCPClient class) you can call Close to close the connection. In the same fashion when using directly Socket or NetworkStream you can also call Close.
On the other hand you're detecting and debugging connection open/closed connections on the server, right? There can be the possibility that server code does not handle connection close properly and thus you get incorrect statistics.
Also under heavy load, server may not have enough CPU time to update the state for the connections so you can expect some delays. Does your server uses asynchronous I/O or connection per thread principle?