Ich bin ein TCP-Server in C# 5.0 Erstellen und ich das await Schlüsselwort bin mit, wenn jedoch tcpListener.AcceptTcpClientAsync
und networkStream.ReadAsync
Beeinflusst async/await die tcp-Serverleistung?
aufrufen, wenn ich die CPU-Auslastung von meinem Server mit Process Explorer überprüfen habe ich die folgenden Ergebnisse:
tcp Sync Version: 10% CPU-Auslastung
tcp Async Version: 30% CPU-Auslastung die Hälfte der Nutzungs Kernel Nutzung ist.
Darüber hinaus habe ich gemessen, wie viel Zeit ich Daten durch Hinzufügen eines Zählers innerhalb der while-Look des Netzwerk-Stream erhalten, und die asynchrone Version Schleifen 120.000 Mal, während die Sync-Version 2.500.000 Schleifen.
In Bezug auf die empfangene Nachricht/Sekunde ist die Async-Version um 15% langsamer als die Synchronisierungsversion, wenn Nachrichten von 3 verschiedenen Clients empfangen werden.
Warum verwendet die Async-Version viel mehr CPU als die Sync-Version?
Liegt das an dem Schlüsselwort async/await?
Ist das normal, dass ein Async-Tcp-Server langsamer als sein Synchron-Gegenstück ist?
EDIT: Hier ist ein Beispiel für die Asynchron-tcp-Server-Code
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());
}
}
Für jeden neuen Kunden haben wir ein neues 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 erstellen: Synchron-Server
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;
}
}
}
Und die Verbindung
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();
}
}
Sie haben keinen Aufruf von 'ReceiveDataFromClientAsync' in' AsyncTcpClientConnection'. Obwohl es nicht mit der Leistung zusammenhängt, ist es immer noch ein Fehler. –
Ich kann 'ReceiveDataFromClientAsync' nicht erwarten, weil das Programm ewig warten würde und niemals auf einen anderen TCP-Client warten würde. – alexandrekow
Sehen Sie, ob dies hilft http://msdn.microsoft.com/en-us/magazine/dn605876.aspx –