Measuring C # / Async Data Access Using the StopWatch Class

I have the following code, and it seems the elapsed milliseconds are inaccurate:

public async Task<ActionResult> Index() { try { var connString = RoleEnvironment.IsEmulated ? ConfigurationManager.ConnectionStrings["Poc"].ConnectionString : ConfigurationManager.ConnectionStrings["PocVm"].ConnectionString; var repository = new AccountRepository(connString); var stopWatch = new Stopwatch(); stopWatch.Start(); var accounts = await repository.GetAll(); stopWatch.Stop(); ViewBag.Accounts = accounts; ViewBag.VmStatus = stopWatch.ElapsedMilliseconds; } catch (Exception e) { blah blah blah... } return View(); } 

Is this right, or am I missing something painfully obvious?

+7
source share
1 answer

it looks completely normal to me.

A potential error would be that the Repository.GetAll method is not asynchronous, I hope it has the following signature:

  public async Task<IEnumerable<Account>> GetAll(); 
0
source

All Articles