Skip to content
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

Merged
merged 10 commits into from
Sep 7, 2017

Conversation

navin22
Copy link
Member

@navin22 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.
@navin22 navin22 requested a review from harshjain2 September 5, 2017 12:34
@navin22 navin22 assigned codito and unassigned codito Sep 5, 2017
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());
Copy link
Contributor

@codito codito Sep 6, 2017

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}",
Copy link
Contributor

@codito codito Sep 6, 2017

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

Copy link
Member Author

@navin22 navin22 Sep 6, 2017

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
Copy link
Contributor

@codito codito Sep 6, 2017

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);
Copy link
Contributor

@codito codito Sep 6, 2017

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);
Copy link
Contributor

@codito codito Sep 6, 2017

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);
Copy link
Contributor

@harshjain2 harshjain2 Sep 6, 2017

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));
Copy link
Contributor

@harshjain2 harshjain2 Sep 6, 2017

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

Copy link
Contributor

@harshjain2 harshjain2 Sep 6, 2017

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 };
Copy link
Contributor

@harshjain2 harshjain2 Sep 6, 2017

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

Copy link
Member Author

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;
Copy link
Contributor

@harshjain2 harshjain2 Sep 6, 2017

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);
Copy link
Contributor

@smadala smadala Sep 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment. #Closed

Copy link
Member Author

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)

@@ -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.
Copy link
Contributor

@codito codito Sep 7, 2017

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;
Copy link
Contributor

@codito codito Sep 7, 2017

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

Copy link
Contributor

@codito codito left a 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;
Copy link

@IanKemp IanKemp Sep 7, 2017

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Thanks !


In reply to: 137451610 [](ancestors = 137451610)

@navin22 navin22 merged commit cffd8a8 into microsoft:master Sep 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants