[英]Does async/await affects tcp server performance?
I am creating a Tcp server in C# 5.0 and I am using the await keyword when calling tcpListener.AcceptTcpClientAsync
and networkStream.ReadAsync
我在C#5.0中创建一个Tcp服务器,我在调用tcpListener.AcceptTcpClientAsync
和networkStream.ReadAsync
时使用了await关键字
However when I check the CPU usage of my server with Process Explorer I have the following results: 但是,当我使用Process Explorer检查我的服务器的CPU使用率时,我得到以下结果:
Tcp Sync version: 10% CPU usage Tcp Sync版本: CPU使用率为10%
Tcp Async Version: 30% CPU usage Half of the usage is kernel usage. Tcp异步版本: 30%的CPU使用率一半的使用是内核使用。
Moreover, I measured how many time I received data by adding a counter inside the while look of the network stream, and the async version loops 120,000 times while the sync version loops 2,500,000 times. 此外,我通过在网络流的外观中添加计数器来测量我接收数据的时间,并且异步版本循环120,000次,而同步版本循环2,500,000次。
In term of message received/second the async version is 15% slower than the sync version when receiving messages from 3 different clients. 在收到的消息/秒中,当从3个不同的客户端接收消息时,异步版本比同步版本慢15%。
Why does the Async Version use a lot more CPU than the Sync version? 为什么Async Version比Sync版本使用更多的CPU?
Is this because of the async/await keyword ? 这是因为async / await关键字?
Is this normal that an Async Tcp server is slower than its sync counterpart? Async Tcp服务器比其同步速度慢吗?这是正常的吗?
EDIT: Here is an example of the async tcp server code 编辑:这是异步tcp服务器代码的示例
public class AsyncTcpListener : ITcpListener
{
private readonly ServerEndpoint _serverEndPoint; // Custom class to store IpAddress and Port
public bool IsRunning { get; private set; }
private readonly List<AsyncTcpClientConnection> _tcpClientConnections = new List<AsyncTcpClientConnection>();
private TcpListener _tcpListener;
public AsyncTcpMetricListener()
{
_serverEndPoint = GetServerEndpoint();
}
public async void Start()
{
IsRunning = true;
RunTcpListener();
}
private void MessageArrived(byte[] buffer)
{
// Deserialize
}
private void RunTcpListener(){
_tcpListener = null;
try
{
_tcpListener = new TcpListener(_serverEndPoint.IpAddress, _serverEndPoint.Port);
_tcpListener.Start();
while (true)
{
var tcpClient = await _tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);
var asyncTcpClientConnection = new AsyncTcpClientConnection(tcpClient, MessageArrived);
_tcpClientConnections.Add(asyncTcpClientConnection);
}
}
finally
{
if (_tcpListener != null)
_tcpListener.Stop();
IsRunning = false;
}
}
public void Stop()
{
IsRunning = false;
_tcpListener.Stop();
_tcpClientConnections.ForEach(c => c.Close());
}
}
For each new client we create a new AsyncTcpConnection 对于每个新客户端,我们创建一个新的AsyncTcpConnection
public class AsyncTcpClientConnection
{
private readonly Action<byte[]> _messageArrived;
private readonly TcpClient _tcpClient;
public AsyncTcpClientConnection(TcpClient tcpClient, Action<byte[]> messageArrived)
{
_messageArrived = messageArrived;
_tcpClient = tcpClient;
ReceiveDataFromClientAsync(_tcpClient);
}
private async void ReceiveDataFromClientAsync(TcpClient tcpClient)
{
var readBuffer = new byte[2048];
// PacketProtocol class comes from http://blog.stephencleary.com/2009/04/sample-code-length-prefix-message.html
var packetProtocol = new PacketProtocol(2048);
packetProtocol.MessageArrived += _messageArrived;
try
{
using (tcpClient)
using (var networkStream = tcpClient.GetStream())
{
int readSize;
while ((readSize = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) != 0)
{
packetProtocol.DataReceived(readBuffer, readSize);
}
}
}
catch (Exception ex)
{
// log
}
}
public void Close()
{
_tcpClient.Close();
}
}
EDIT2: Synchronous server EDIT2:同步服务器
public class TcpListener : ITcpListener
{
private readonly ObserverEndpoint _serverEndPoint;
private readonly List<TcpClientConnection> _tcpClientConnections = new List<TcpClientConnection>();
private Thread _listeningThread;
private TcpListener _tcpListener;
public bool IsRunning { get; private set; }
public TcpMetricListener()
{
_serverEndPoint = GetServerEndpoint();
}
public void Start()
{
IsRunning = true;
_listeningThread = BackgroundThread.Start(RunTcpListener);
}
public void Stop()
{
IsRunning = false;
_tcpListener.Stop();
_listeningThread.Join();
_tcpClientConnections.ForEach(c => c.Close());
}
private void MessageArrived(byte[] buffer)
{
// Deserialize
}
private void RunTcpListener()
{
_tcpListener = null;
try
{
_tcpListener = new TcpListener(_serverEndPoint.IpAddress, _serverEndPoint.Port);
_tcpListener.Start();
while (true)
{
var tcpClient = _tcpListener.AcceptTcpClient();
_tcpClientConnections.Add(new TcpClientConnection(tcpClient, MessageArrived));
}
}
finally
{
if (_tcpListener != null)
_tcpListener.Stop();
IsRunning = false;
}
}
}
And the connection 和连接
public class TcpClientConnection
{
private readonly Action<byte[]> _messageArrived;
private readonly TcpClient _tcpClient;
private readonly Task _task;
public TcpClientConnection(TcpClient tcpClient, Action<byte[]> messageArrived)
{
_messageArrived = messageArrived;
_tcpClient = tcpClient;
_task = Task.Factory.StartNew(() => ReceiveDataFromClient(_tcpClient), TaskCreationOptions.LongRunning);
}
private void ReceiveDataFromClient(TcpClient tcpClient)
{
var readBuffer = new byte[2048];
var packetProtocol = new PacketProtocol(2048);
packetProtocol.MessageArrived += _messageArrived;
using (tcpClient)
using (var networkStream = tcpClient.GetStream())
{
int readSize;
while ((readSize = networkStream.Read(readBuffer, 0, readBuffer.Length)) != 0)
{
packetProtocol.DataReceived(readBuffer, readSize);
}
}
}
public void Close()
{
_tcpClient.Close();
_task.Wait();
}
}
I have also issues with an async
and these are my findings: https://stackoverflow.com/a/22222578/307976 我也有async
问题,这些是我的发现: https : //stackoverflow.com/a/22222578/307976
Also, I have an asynchronous TCP server/client using async
example here that scales well. 另外,我有使用异步TCP客户端/服务器async
例如这里扩展良好。
Try the following implementation of ReceiveInt32Async
and ReceiveDataAsync
for receiving your length-prefixed messages directly, instead of using tcpClient.GetStream
and networkStream.ReadAsync
: 尝试使用ReceiveInt32Async
和ReceiveDataAsync
的以下实现来直接接收长度为前缀的消息,而不是使用tcpClient.GetStream
和networkStream.ReadAsync
:
public static class SocketsExt
{
static public async Task<Int32> ReceiveInt32Async(
this TcpClient tcpClient)
{
var data = new byte[sizeof(Int32)];
await tcpClient.ReceiveDataAsync(data).ConfigureAwait(false);
return BitConverter.ToInt32(data, 0);
}
static public Task ReceiveDataAsync(
this TcpClient tcpClient,
byte[] buffer)
{
return Task.Factory.FromAsync(
(asyncCallback, state) =>
tcpClient.Client.BeginReceive(buffer, 0, buffer.Length,
SocketFlags.None, asyncCallback, state),
(asyncResult) =>
tcpClient.Client.EndReceive(asyncResult),
null);
}
}
See if this gives any improvements. 看看这是否有任何改进。 On a side note, I also suggest making ReceiveDataFromClientAsync
an async Task
method and storing the Task
it returns inside AsyncTcpClientConnection
(for status and error tracking). 另外,我还建议将ReceiveDataFromClientAsync
为async Task
方法,并将其返回的Task
存储在AsyncTcpClientConnection
(用于状态和错误跟踪)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.