c# - Boosting performance on async web calls -


backgound: must call web service call 1500 times takes 1.3 seconds complete. (no control on 3rd party api.) total time = 1500 * 1.3 = 1950 seconds / 60 seconds = 32 minutes roughly.

i came though solution did not pan out great. changed calls async web calls thinking dramatically results did not.

example code:

pre-optimizations:

foreach (var elmkeydataelementnamed in findresponse.keys) {      var getrequest = new elementmastergetrequest     {         key = new elmfullkey         {             cmpcode = codaservicesettings.companycode,             code = elmkeydataelementnamed.code,             level = filterlevel         }     };      elementmastergetresponse getresponse;     _elementmasterserviceclient.get(new masteroptions(), getrequest, out getresponse);     elementlist.add(new codaelement { element = getresponse.element, searchcode = filtercode }); } 

with optimizations:

var tasks = findresponse.keys.select(elmkeydataelementnamed => new elementmastergetrequest     {         key = new elmfullkey             {                 cmpcode = codaservicesettings.companycode,                 code = elmkeydataelementnamed.code,                 level = filterlevel             }     }).select(getrequest => _elementmasterserviceclient.getasync(new masteroptions(), getrequest)).tolist();  task.waitall(tasks.toarray());  elementlist.addrange(tasks.select(p => new codaelement     {         element = p.result.getresponse.element,         searchcode = filtercode     })); 

smaller sampling example: test did smaller sampling of 40 records took 60 seconds no optimizations optimizations took 50 seconds. have though have been closer 30 or better.

i used wireshark watch transactions come through , realized async way not sending fast assumed have.

async requests captured async requests captured

normal no optimization normal no optimization can see asnyc pushes few fast drops off... note between requests 10 , 11 took 3 seconds.

is overhead creating threads tasks slow takes seconds? note: tasks referring 4.5 tap task library.

why wouldn't request come faster that. told apache web server hitting hold 200 max threads don't see issue there..

am not thinking clearly? when calling web services there little advantages async requests? have code mistake? ideas great.

after many days of searching found post solved problem: trying run multiple http requests in parallel, being limited windows (registry)

the reason request not hitting server quicker due client side code , nothing server. default c# allows 2 concurrent requests. see here: http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

i added line of code , request came through in milliseconds.

system.net.servicepointmanager.defaultconnectionlimit = 50; 

enter image description here


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -