Slow Performance using AspNetCore Kestrel Host\WebAPI\HttpClient.SendAsync
Sample code:
[Route("api/[controller]")]
public class ValuesController : Controller
{
private static readonly HttpClient client = InitHttpClient();
[HttpGet]
public async Task<long> Get()
{
var r = await HttpInternal();
return r;
}
private async Task<long> HttpInternal()
{
string uuid = Guid.NewGuid().ToString();
var request = new HttpRequestMessage(HttpMethod.Get, $"http://www.bing.com/?uuid={uuid}");
try
{
var sw = new Stopwatch();
sw.Start();
// This appears to be the location of the delay; with greater load, the worse random delays
var response = await client.SendAsync(request);
sw.Stop();
// Logging code removed…
return sw.ElapsedMilliseconds;
}
catch (Exception)
{
// swallow
}
return -1;
}
Some sample data which might be helpful as well (using Visual Studio Load Test Project to generate load)
10x User Load for 6000 Request: AVG: 472 Milliseconds and Max: 10 Seconds
As load increases these numbers get worse:
100x User Load for 6000 Request: AVG: 3.7 Seconds and MAX: 33 Seconds
*I can provide sample project and load project as well.
Question: Are we calling the HttpClient correctly? Is there anyway to avoid the spike in call time accessing: var response = await client.SendAsync(request);?
What are we missing here?
[EDIT] Code formatting markdown added by @karelz
Are you hitting Bing with 600,000 requests from a single IP address (your server)?
Hey Ben. A few points... even if we lower test numbers, we see the delay spike. Also, even if we change endpoints to a local server, we see the same delay. Finally, my customer has a dedicated load environment, not just a single single server host like my test environment, which shows the same behavior.
So, 10 users running queries 6000 times was just one example and I can replicate with fewer irritations. I'll A smaller test completed tomorrow.
I was concerned that we might not be calling the asynchronous httpclient correctly. Do you have any thoughts on that? Also, how do we profile the application? Will tools like xperf be our best choice? Thanks!
Desktop or Core? Desktop you may be throttled on outbound connection limits
Which you can change with connectionmanagement in .config
<configuration>
<system.net>
<connectionManagement>
<add address = "www.bing.com" maxconnection = "1000" />
<add address = "*" maxconnection = "2000" />
</connectionManagement>
</system.net>
</configuration>
Or with ServicePointManager.DefaultConnectionLimit
Not sure about Core.
This is Core.
I put a condition in the Async call to check CurrentConnections vs ConnectionLimit and the delay occurs even when we're well below the limit. Actually, current connections never exceeds the # of users defined in the load test.
Even on low numbers for the test (users and iterations), the delay can occur but it's far less often and I cannot reproduce the behavior consistently. Also, when I hit between 80-100 concurrent users, the issue will happen several times during the test period. I'd like to be able to capture performance metrics at those delays.
Normally, I'd collect debugging data at the moment the asp.net response time hits some value but, I can't see way to measure active response time in ASPNETCORE\Kestrel. I could try using a timer event to fire at a given interval but that's not ideal.
I would think the next layer would be to look into something like XPERF or some .Net Profiler?
Thanks!
So you want to do a standalone console app using HttpClient which creates the connections out; and see how that performs; if that's the issue then add the repo here.
If its not the issue; but only occurs when using Kestrel, then you want to file the issue here https://github.com/aspnet/KestrelHttpServer
May be related https://github.com/dotnet/corefx/issues/23009 And https://github.com/dotnet/corefx/issues/23012
@tdevere One note. HttpRequestMessage implements IDisposable and it is better to dispose request message if you don't want to get memory leaks. This does not apply for HttpClient.
I can confirm now this issue is resolved. I don't have RCA but moving away from bing.com and going to a localhost or remote IP on a different cloud endpoint, the delay never happens. I would expect the problem has to do with some sort of defensive measures at bing.com.
Thank you to everyone who took the time to investigate this problem with me.