9

I'm writing some performance tests, and want to be able to time a method that is asynchronous. The code looks like this, where action is a Func<Task<HttpResponseMessage>>:

var sw = new Stopwatch();
HttpResponseMessage response = null;

sw.Start();
response = await action().ConfigureAwait(continueOnCapturedContext: false);
sw.Stop();

The code compiles and runs ok, but the measured milliseconds are about 100 times higher than the request timings we see in Fiddler - Fiddler reports 200-300ms, but the stop watch reports ~30,000ms. Is there some catch about timing asynchronous methods? Is the solution to do the timing in the action itself (which would be annoying?)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
GoatInTheMachine
  • 3,583
  • 3
  • 25
  • 35
  • Does it take 30 seconds to execute? – Lasse V. Karlsen Jul 23 '15 at 15:50
  • 4
    There measuring two extremely different things. That the times are different is unsurprising. It's like comparing how long it takes from the moment you wake up until the moment you get back into bed versus the time that you spent driving into work and back. They're not *supposed* to be the same thing, and how different they are will vary radically for different people in different situations. – Servy Jul 23 '15 at 15:52
  • I get that they're two different things, I assumed that the request building/response processing would be miniscule in comparison to the time it takes to make the request and recieve the response - even if it took double the time it would be ok for our purposes, but 100 times out is puzzling? – GoatInTheMachine Jul 23 '15 at 16:00
  • 1
    if you debug it and access the stopwatches Elapsed or ElapsedMilliseconds properties inside the debugger within the 30 seconds, what does it show? if you write something to console or log at various points in your code, is it 30 seconds? – citykid Jul 23 '15 at 16:01
  • @citykid we're sending the timings to statsd, console shows the same timings, plus the whole test only takes about 15 seconds! – GoatInTheMachine Jul 23 '15 at 16:11
  • What do you have inside `action()` except for an http call? – shay__ Jul 27 '15 at 13:08
  • If I recall, nothing besides making a request. A timer inside that code reported times similar to those reported by Fiddler. – GoatInTheMachine Feb 22 '16 at 11:10
  • Running into this same problem. Started stopwatch as soon as the request was received and stopped immediately after the await statement, before the return. The stopwatch reported 500 milliseconds and fiddler reported 1.5 seconds. My first thought was that there's something happening before/after the request was received and the response was returned. Turns out this is not the case. After some tuning of the app, the stopwatch reported similar times, but fiddler reported significant drops. – Michael Nov 07 '22 at 14:13

1 Answers1

3

This should work fine for measuring the true amount of time it takes for your asynchronous task to finish. You need to keep in mind that:

  1. the time you are measuring with Fiddler is measuring the request only, and none of the time it takes for your code to process the response.
  2. there is a significant difference in time here and you should easily be able to time your code yourself to see how long it takes to progress from a break point before your request to after your request. If this is closer to 30 seconds then your stop watch is probably accurate.
  3. there isn't anything obvious to me that would make your time inaccurate.
Shawn Lehner
  • 1,293
  • 7
  • 14