小编Nyg*_*rds的帖子

测量 ASP.NET Core 中异步代码与同步代码的性能

我正在尝试使用 SQL Server Express 和 EF Core 3.1.3 来衡量 ASP.NET Core 3.1 中异步与同步的性能,并且有两个完全相同的函数,除了一个是异步的,一个是同步的:

[HttpGet("search/description/{searchString}")]
public async Task<ActionResult<IEnumerable<Products>>> SearchForProductsDescription(String searchString) {
    return await _context.Products.Where(p => p.Description == searchString).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)

以及同步版本:

return _context.Products.Where(p => p.Description == searchString).ToList();
Run Code Online (Sandbox Code Playgroud)

我使用 jmeter 作为基准工具,同步函数比异步函数更快(正如预期的那样),但是当我增加 jmeter 中的线程数以使平均响应时间为 500ms< 时,同步代码仍然更快。我尝试过在数据库中使用 1000 行和 20000 行,但它仍然更快。我试图找到一种场景,其中异步函数比同步函数更快,但我遇到了麻烦,是否有什么我出错或误解的地方?

c# benchmarking asynchronous entity-framework asp.net-core

5
推荐指数
1
解决办法
7107
查看次数