jkruer01
jkruer01

Reputation: 2205

Why does the Async Code Handle Less Load?

I created a .Net Web Api with two controllers that do the same thing (purposefully very slow network calls). One is synchronous and one is asynchronous. You can find the code here: https://github.com/jkruer01/SyncVsAsync

The important difference between the 2 controllers is this single line of code with a WebClient:

Synchronous

var content = client.DownloadString(url);

Asynchronous

var content = await client.DownloadStringTaskAsync(url);

Pretty basic stuff...

Then I created a console app the fires 50 simultaneous requests to either the sync or async controller. I expected the async controller to complete more simultaneous requests but I found the exact opposite to be true. The sync controller completed about 25 out of 50 successfully. The async controller only completed about 10 out of 50 successfully.

Why is this? I thought the purpose of the async code was that it could handle more simultaneous requests.

I'm stumped.

Upvotes: 0

Views: 199

Answers (2)

jkruer01
jkruer01

Reputation: 2205

I solved the problem. The issue turned out to be the fact that I was running the code from Visual Studio. Even though I was running it in "Release" mode it was doing something that altered the behavior (I don't know what). I "Published" the code and then ran it outside of Visual Studio and it ran exactly as I would have expected it to. Thanks for all the help and suggestions!

Upvotes: 1

Christian Melendez
Christian Melendez

Reputation: 376

Problem is with the http requests, by default you have a limit of two concurrent calls, that's why it's taking longer, see the following docs:

http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx https://msdn.microsoft.com/en-us/library/system.net.servicepoint.aspx

And also this: Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

Try changing that value :)

Upvotes: 3

Related Questions