Search code examples
c#asp.net-mvcasp.net-core.net-coreasp.net-mvc-5

How to count await method execution time


I am working on a dot net core project. In which I have to count how much had taken by my await method while executing.

[HttpPost]
public async Task<bool> InsertIntoDatabase([Frombody] StudentModel objStu) {
//For Each method and other stuff
  await DoInsert(Fiels Details);
}

I am calling this method in Ajax. So after successful execution of code, I want to return the number of times in minutes taken by the method.

This insertion process contains 500+ records. So, I am interested to calculate the time


Solution

  • One approach is to use Stopwatch

    [HttpPost]
    public async Task<bool> InsertIntoDatabase([Frombody] StudentModel student) 
    {
        var clock = new Stopwatch();
        clock.Start();
    
        await DoInsert(student);
    
        clock.Stop();
        var minutes = clock.Elapsed.TotalMinutes();
    }
    

    Stopwatch Class