c# - How to get a Fast .Net Http Request -


i need http request can use in .net takes under 100 ms. i'm able achieve in browser don't see why such problem in code.

i've tried winhttp webrequest.create , both of them on 500ms isn't acceptable use case.

here examples of simple test i'm trying pass. (winhttpfetcher simple wrapper wrote trivial example of request i'm not sure it's worth pasting.)

i'm getting acceptable results libcurlnet if there simultaneous usages of class access violation. since it's not managed code , has copied bin directory, it's not ideal deploy open source project.

any ideas of implementation try?

    [test]     public void winhttp_should_get_html_quickly()     {         var fetcher = new winhttpfetcher();         var starttime = datetime.now;                   var result = fetcher.fetch(new uri("http://localhost"));         var endtime = datetime.now;         assert.less((endtime - starttime).totalmilliseconds, 100);     }     [test]     public void webrequest_should_get_html_quickly()     {         var starttime = datetime.now;         var req = (httpwebrequest) webrequest.create("http://localhost");         var response = req.getresponse();         var endtime = datetime.now;         assert.less((endtime - starttime).totalmilliseconds, 100);     } 

when benchmarking, best discard @ least first 2 timings skew results:

  • timing 1: dominated jit overhead i.e. process of turning byte code native code.
  • timing 2: possible optimization pass jit'd code.

timings after reflect repeat performance better.

the following example of test harness automatically disregard jit , optimization passes, , run test set number of iterations before taking average assert performance. can see jit pass takes substantial amount of time.

jit:410.79ms

optimize:0.98ms.

average on 10 iterations:0.38ms

code:

[test] public void webrequest_should_get_html_quickly() {     private const int testiterations = 10;     private const int maxmilliseconds = 100;      action test = () =>     {        webrequest.create("http://localhost/iisstart.htm").getresponse();     };      asserttimedtest(testiterations, maxmilliseconds, test); }  private static void asserttimedtest(int iterations, int maxms, action test) {     double jit = execute(test); //disregard jit pass     console.writeline("jit:{0:f2}ms.", jit);      double optimize = execute(test); //disregard optimize pass     console.writeline("optimize:{0:f2}ms.", optimize);      double totalelapsed = 0;     (int = 0; < iterations; i++) totalelapsed += execute(test);      double averagems = (totalelapsed / iterations);     console.writeline("average:{0:f2}ms.", averagems);     assert.less(averagems, maxms, "average elapsed test time."); }  private static double execute(action action) {     stopwatch stopwatch = stopwatch.startnew();     action();     return stopwatch.elapsed.totalmilliseconds; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -