Reputation: 3773
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?)
Upvotes: 9
Views: 6988
Reputation: 1323
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:
Upvotes: 3