c# - Why does waiting once on multiple calls to HttpClient.GetStringAsync() take as long as waiting individually on each call? -
using system.net.http; using microsoft.visualstudio.testtools.unittesting; using system.threading.tasks; using system.diagnostics; [testclass] public class unittest1 { [testmethod] public async task exceutemultiplerequestsinparallel() { stopwatch sw = new stopwatch(); sw.start(); httpclient client = new httpclient(); task<string> ms = client.getstringasync("http://www.microsoft.com"); task<string> msdn = client.getstringasync("http://msdn.microsoft.com"); task<string> blogs = client.getstringasync("http://blogs.msdn.com"); await task.whenall(ms, msdn, blogs); sw.stop(); var result = sw.elapsed.tostring(); } [testmethod] public async task excecutemultiplerequests() { stopwatch sw = new stopwatch(); httpclient client = new httpclient(); string ms = await client.getstringasync("http://www.microsoft.com"); string msdn = await client.getstringasync("http://msdn.microsoft.com"); string blogs = await client.getstringasync("http://blogs.msdn.com"); sw.stop(); var result = sw.elapsed.tostring(); } }
at first used 1 httpclient
. expectation 1 await task.whenall(...)
take time longest task take. expectation take measurably less calling each of 3 tasks await.
however, didn't seem case. had comment 1 otherwise cache distort things.
then used 3 httpclients
, surprisingly method using 3 times await took less time 1 using await task.whenall(...)
. seems strange.
would please explain why not see same improvement in elapsed time using whenall()
in first scenario (calling single httpclient
3 times) in second (calling 3 httpclient
instances once each)?
[testmethod] public async task exceutemultiplerequestsinparallel() { stopwatch sw = new stopwatch(); sw.start(); httpclient client = new httpclient(); httpclient client2 = new httpclient(); httpclient client3 = new httpclient(); task<string> ms = client.getstringasync("http://www.microsoft.com"); task<string> msdn = client2.getstringasync("http://msdn.microsoft.com"); task<string> blogs = client3.getstringasync("http://blogs.msdn.com"); await task.whenall(ms, msdn, blogs); sw.stop(); var result = sw.elapsed.tostring(); } [testmethod] public async task excecutemultiplerequests() { stopwatch sw = new stopwatch(); sw.start(); httpclient client = new httpclient(); httpclient client2 = new httpclient(); httpclient client3 = new httpclient(); string ms = await client.getstringasync("http://www.microsoft.com"); string msdn = await client2.getstringasync("http://msdn.microsoft.com"); string blogs = await client3.getstringasync("http://blogs.msdn.com"); sw.stop(); var result = sw.elapsed.tostring(); }
the behavior describe sounds normal me. see no reason expect single httpclient
instance process more 1 http operation @ given time.
so when use single httpclient
instance, if call getstringasync()
again before first call has completed, class has no choice queue operation , not start 1 until previous 1 has completed. likewise third operation.
when use 3 different instances, each able proceed independently , 3 requests can happen concurrently.
the fact wait on 3 operations concurrently (i.e. call whenall()
) cannot , not change order in executed internally httpclient
. httpclient
able give 3 different tasks wait on doesn't change fact single instance of able process 1 @ time. , since last operation cannot start until of enqueued operations have completed, same basic elapsed time if had waited each operation complete in turn before enqueuing next 1 yourself.
Comments
Post a Comment