Webapi (.NET Core) has no performance gain when using async

Asked

Viewed 76 times

1

I’m implementing an asynchronous api, but I haven’t seen much performance gain. I used Restful Stress for Chrome to do my tests, I simply didn’t see performance gain when using async. I thought this would increase the number of simultaneous requests.

Configuration used inserir a descrição da imagem aqui Result using async inserir a descrição da imagem aqui Result without using async inserir a descrição da imagem aqui Code using async

public async Task<ObjectResult> Get()
{
    try
    {
        //usando ToListAsync() a performance foi pior, por isso usei Task
        //var result = await _dbSet.AsNoTracking().ToListAsync();
        var result = await Task.FromResult(_dbSet.AsNoTracking().ToList());

        return Ok(result);
    }
    catch (Exception ex)
    {
        return this.BadRequest("ERROR: " + ex.Message);
    }
}

Code without using async

public ObjectResult Get()
{
    try
    {
        var result = _dbSet.AsNoTracking().ToList();

        return Ok(result);
    }
    catch (Exception ex)
    {
        return this.BadRequest("ERROR: " + ex.Message);
    }
}
  • But you shouldn’t have won performance, because the final task is the same.. and you’re waiting for the result.. by your example you are consulting the bank.. and even Sync or async your api will have the same job to perform these tasks. Async would be worth it in a case when you don’t expect a result.. for example.. doing an Insert, or calling another service where you don’t expect the full result or execution.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.