c# - Multi threaded web requests performance issue -
i want use multi threaded web requests not getting performance expect.
i running computer 4 cores, 1 gbps of upload , 1 gbps of download.
for test downloading google main page.
the size of data google page ~50 kb.
i sending 1000 requests simultaneously , except complete requests within ~2 seconds takes more 20 seconds complete
my code looks this:
bool success = threadpool.setminthreads(workerthreads: 2000, completionportthreads: 1000); success = threadpool.setmaxthreads(2000, 2000); datetime dt = datetime.utcnow; parallel.for(0, 1000, (num) => { string url = "https://www.google.co.il/?gfe_rd=cr&dcr=0&ei=ozy3wcmomy7b8affj4f4&gws_rd=ssl"; using (webclient web = new webclient()) { byte[] bytes = web.downloaddata(url); } } ); double sec = (datetime.utcnow - dt).totalseconds; console.writeline(sec);
i think better choice in case using httpclient
explicit asynchronous operations , let windows manage thread pool itself.
stopwatch sw = new stopwatch(); sw.start(); (int = 0; < 1000; i++) { var httpclient = new httpclient(); results[i] = httpclient.getbytearrayasync(@"https://www.google.co.il/?gfe_rd=cr&dcr=0&ei=ozy3wcmomy7b8affj4f4&gws_rd=ssl"); } var status = task.whenall(results); //whenany if can process results independently var pages = status.result; sw.stop(); double sec = sw.elapsed.totalseconds; console.writeline(sec);
side note: please consider using stopwatch
time measurement.
Comments
Post a Comment