2021年4月25日星期日

Why asynchronous counterpart of a synchronous method have less throughput?

I have 2 API endpoints which basically performs the same. Loads an item from Database using Ef Core and return it as response. The difference is one is synchronous and other is asynchronous, specifically, the call to Ef Core is synchronous in first method and asynchronous in the second.

I was performing load testing on both endpoints using Bombardier. At first I use only 1 connection with 5000 requests and synchronous method performed well. I thought context switching in asynchronous method might be responsible for this as there is only 1 connection. Then I increased connections to 150 but still synchronous method perform way better and has much more throughput than the asynchronous one as you can see in the image below. First output is of synchronous version and second is of asynchronous version.

I thought more connection there will be the asynchronous method will shine as it won't wait for the existing connection and will take on the other connection. What is here that I'm not understanding? Could anyone explain it to me.

enter image description here

UPDATE

Here is my code and also I re-ran the test, including its result as well.

        [HttpGet("a")]          public IActionResult Get()          {              var invoices = context.Invoices                  .Include(it => it.InvoiceItems)                  .ToList();              return Ok(invoices);          }            [HttpGet("b")]          public async Task<IActionResult> GetAsync()          {              var invoices = await context.Invoices                  .Include(it => it.InvoiceItems)                  .ToListAsync();              return Ok(invoices);          }  

enter image description here

https://stackoverflow.com/questions/67255617/why-asynchronous-counterpart-of-a-synchronous-method-have-less-throughput April 26, 2021 at 12:18AM

没有评论:

发表评论