Home > database >  How to count await method execution time
How to count await method execution time

Time:03-04

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

CodePudding user response:

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

CodePudding user response:

Why not return a custom object in your task with the time and the result?

private async Task<MyClass> GetResult()
{}

public class MyClass
{
    public bool  Success { get; set; }
    public long TimeInSeconds { get; set; }
}
  • Related