c# - Multiple simultaneous requests to the same http host -
i have c# desktop application, needs make multiple simultaneous http requests same web server. here's test performed check whether requests happen simultaneously:
on web server, created test page sleeps 3 seconds (simulates long-running task), returns current date/time. here's code (vb.net):
<%system.threading.thread.sleep(3000)%><%=now%>
in c# app, have function makerequest() uses system.net.http.httpclient make web request, , returns response string.
then in c# app there's function invoked button click, calls makerequest() multiple times asynchronously:
var responses = await task.whenall(makerequest(), makerequest(), makerequest());
in above example, makerequest() called 3 times. see on web server when monitor requests/sec performance counter, gets 2 requests, , 3 sec later 1 more request. that's design, because default value system.net.servicepointmanager.defaultconnectionlimit 2, c# app can send 2 requests @ time, though asked 3. overall time c# app took complete button click 6 sec.
now in c# app, set servicepointmanager.defaultconnectionlimit = 1000000. time requests/sec on web server shows 3, , button click in c# app completes in 3 seconds. far.
next change c# app make 60 simultaneous calls rather 3. that's things interesting. when click button, expect see requests/sec on web server spike 60, , c# app complete button click in 3 sec. instead web server requests/sec showing 5, after 3 seconds 5 more, etc., until 60 requests have been made. when click button again, same picture except requests/sec spike 10 or 15, , button click completes faster. click again - time requests/sec show 2 spikes 30, , button click completes in 6 sec. subsequent button clicks result in requests/sec spiking first 40 20 (for total of 60 requests), 50 , 10, maybe 55 , 5, , c# app starts sending 60 requests in 1 go. button click completes in 3 sec, , requests/sec on web server shows 1 spike 60. if continue pushing button, consistently 60 requests being made simultaneously.
but that's not all. if stop pushing button, c# app seems "forget" it's previous achievement. not restart app, stop pushing button minute, , on next push go 5 requests @ time, , above scenario repeat if keep pushing button continuously.
i performed above test mvc - copied , pasted code c# app mvc page. got same result.
can please explain what's going on? thank you
here's code of c# app. it's winforms app, , code lives inside form class:
<!-- language: lang-c# --> private httpclient _httpclient = new httpclient(); private async void button1_click(object sender, eventargs e) { servicepointmanager.defaultconnectionlimit = 1000000; var sw = stopwatch.startnew(); var responses = await task.whenall(makerequest(), makerequest(), makerequest()); sw.stop(); var msg = string.join("\r\n", responses) + "\r\n\r\ntime elapsed: " + sw.elapsedmilliseconds; messagebox.show(msg); } private async task<string> makerequest() { using (var message = await _httpclient.getasync("http://testserver/testpage.aspx")) { return await message.content.readasstringasync(); } }
two reasons:
- client windows skus limit number of concurrent iis requests 10.
- apparently, client uses synchronous io or otherwise blocking. takes while thread pool warmed up. threads injected on time. either use async io , non-blocking or make sure threads need there. regarding "forgetting": idle pool threads retired on time. tests therefore timing sensitive. unreliable, don't put production.
Comments
Post a Comment