-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using BufferedStream over NetworkStream for performance improvement. #1041
Conversation
navin22
commented
Sep 5, 2017
- Used BufferedStream over NetworkStream while writing the data to reduce the number of calls.
- Used BufferedSize of 16KB.
- Didn't use BufferedStream while reading, as this didn't add much perf improvements. And was hanging when we were doing Socket.Poll.
- Used BufferedStream over NetworkStream while writing the data to reduce the number of calls. - Used BufferedSize of 16KB.
this.binaryReader = new BinaryReader(this.stream); | ||
this.socket.NoDelay = true; | ||
this.stream = new PlatformStream().PlatformBufferedStream(client.GetStream(), SocketConstants.BUFFERSIZE); | ||
this.binaryReader = new BinaryReader(client.GetStream()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a test for this case in Platform Tests suite? It is okay if we add this test only for the newer SocketClient and SocketServer. #Closed
@@ -354,8 +355,8 @@ private string TryReceiveRawMessage(CancellationToken cancellationToken) | |||
&& socketException.SocketErrorCode == SocketError.TimedOut) | |||
{ | |||
EqtTrace.Info( | |||
"SocketCommunicationManager ReceiveMessage: failed to receive message because read timeout {0}", | |||
ioException); | |||
"SocketCommunicationManager ReceiveMessage: failed to receive message because read timeout {0}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not required change? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, added the tabs back. #Closed
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest Interfaces/IO
for this. It is independent of system characteristics. #Closed
/// </summary> | ||
/// <param name="stream">Input Stream</param> | ||
/// <returns>Buffered Stream</returns> | ||
Stream PlatformBufferedStream(Stream stream); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't see usage of this API. Can we remove it please? #Closed
/// <param name="stream">Input Stream</param> | ||
/// <param name="bufferSize">Buffer Size</param> | ||
/// <returns>Buffered Stream</returns> | ||
Stream PlatformBufferedStream(Stream stream, int bufferSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be CreateBufferedStream
.
Or we could make it an extension method on Stream
. E.g. Stream.ToBufferedStream(int bufferSize)
. #Closed
this.binaryWriter = new BinaryWriter(this.stream); | ||
|
||
this.clientConnectedEvent.Set(); | ||
|
||
EqtTrace.Info("Using the buffer size of {0} bytes", SocketConstants.BUFFERSIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Using the buffer size of {0} bytes", SocketConstants.BUFFERSIZE);
}
``` #Closed
@@ -69,7 +70,7 @@ private void OnServerConnected(Task connectAsyncTask) | |||
throw connectAsyncTask.Exception; | |||
} | |||
|
|||
this.channel = this.channelFactory(this.tcpClient.GetStream()); | |||
this.channel = this.channelFactory(new PlatformStream().PlatformBufferedStream(this.tcpClient.GetStream(), SocketConstants.BUFFERSIZE)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we are using BufferedStream
for both Read and Write operations. We need to change it to comply with recommended approach. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same applies for SocketServer.cs #Closed
@@ -35,7 +36,7 @@ protected SocketClient(Func<Stream, ICommunicationChannel> channelFactory) | |||
this.cancellation = new CancellationTokenSource(); | |||
this.stopped = false; | |||
|
|||
this.tcpClient = new TcpClient(); | |||
this.tcpClient = new TcpClient { NoDelay = true }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using NoDelay = true
can be a bit inefficient.
More info here :
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.nodelay(v=vs.110).aspx #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, As it also says we need for immediate/important communications.
In reply to: 137197765 [](ancestors = 137197765)
@@ -6,6 +6,8 @@ namespace Microsoft.TestPlatform.PerformanceTests | |||
using System; | |||
using System.Diagnostics; | |||
using System.Net; | |||
using System.Collections.Generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit : ordering of namespaces. #Closed
this.stream = client.GetStream(); | ||
this.binaryReader = new BinaryReader(this.stream); | ||
this.socket.NoDelay = true; | ||
this.stream = new PlatformStream().PlatformBufferedStream(client.GetStream(), SocketConstants.BUFFERSIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comment. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, as we do socket.poll here also. Need to do in Socket.Client and Socket.Server.
In reply to: 137203711 [](ancestors = 137203711)
…into socketbufferfix
@@ -27,7 +27,9 @@ public LengthPrefixCommunicationChannel(Stream stream) | |||
{ | |||
this.stream = stream; | |||
this.reader = new BinaryReader(stream, Encoding.UTF8, true); | |||
this.writer = new BinaryWriter(stream, Encoding.UTF8, true); | |||
|
|||
// Using the Buffered stream while writing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to specify why. #Closed
@@ -4,8 +4,10 @@ | |||
namespace Microsoft.TestPlatform.PerformanceTests | |||
{ | |||
using System; | |||
using System.Collections.Generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are not required? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fantastic. Suggest few nit changes.
public class SocketConstants | ||
{ | ||
// Buffer size for the buffered stream we are using. | ||
public const int BUFFERSIZE = 16384; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Isn't the MS standard to use PascalCase for constant names? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.