I am currently investigating for a project if there is a signification performance difference the following Methods:
[HttpGet]
public async Task<IHttpActionResult> Test1()
{
await Task.Delay(TimeSpan.FromSeconds(10));
return Ok();
}
and
[HttpGet]
public IHttpActionResult Test2()
{
Thread.Sleep(TimeSpan.FromSeconds(10));
return Ok();
}
In my understanding the first method Test1 is executed asynchronous, which means the result of the task.delay is awaited and the current thread can be reused by an other request.
The second method Test2 is executed synchronous and the thread is blocked for 10 seconds. Nevertheless the OK result is executed asynchronous since my return type is IHttpActionResult.
My questions are the following:
- Is there a benefit of returning a IHttpActionResult vs a Task< IHttpActionResult >?
- I tested both methods on my local machine using wcat Performance tool using 2 clients with 40 virtual clients and both methods resulted in the similar results. I expected that with the first method I would be able to execute more requests than with the second one. So either my setup of this test is wrong or I misunderstood the effects of async / synchronous execution?
Looking forward to your answers!