使用Dapper,Azure SQL和.NET Core 3.1的Azure Web应用程序上的性能问题

问题描述 投票:0回答:1

我们已经使用.NET Core 3.1,Dapper和Azure SQL构建了一个Web API,该API托管在Azure Web应用程序上。使用loader.io,我们进行了一些性能测试,这些测试对于S1实例令人失望(约10 RPS)。每个API调用都会使用Dapper触发几个SQL查询,以从Azure SQL数据库(100 DTU)检索数据。

Application Insights分析器进行了跟踪,这表明数据库中花费了大量时间:AI performance tip

[走热路:Hot path

数据库本身似乎没有压力(DTU或其他度量标准):enter image description here

Web应用程序在CPU上有一些高峰,线程看起来还不错:enter image description here

使用标准.NET CoreIServiceCollectionDI和Scrutor注入代码:

// Injection of all SqlServer repositories.
services.Scan(scan => scan
  .FromAssemblyOf<SomeRepository>()
  .AddClasses(classes => classes.Where(type => type.Name.EndsWith("Repository")))
  .AsImplementedInterfaces()
  .WithTransientLifetime());

Dapper在SQL Server存储库中用于检索信息:

public async Task<IReadOnlyCollection<DocumentRevisionOverview>> GetDocumentRevisionsAsync()
{
  Logger.LogInformation("Starting {Method} on {Class}.", nameof(GetDocumentRevisionsAsync), ClassName);

  var sql = "select statement";

  // databaseParameters.ConnectionString (type is string) is injected via the constructor.
  using (var connection = new SqlConnection(databaseParameters.ConnectionString))
  {
    var documentRevisions = await connection.QueryAsync<DocumentRevisionOverview>(sql);

    Logger.LogInformation("Finished {Method} on {Class}.", nameof(GetDocumentRevisionsAsync), ClassName);

    return documentRevisions.ToList();
  }
}

我们的数据库表中没有varchars,因此varchar to nvarchar conversion 不适用。

  • 所有资源都位于相同的资源组和位置。
  • 这些表仅包含数百条记录。
  • 从Web API返回的JSON为88 KB。
  • Dapper应该重新使用基于不变的连接字符串的ADO.NET连接池。
  • 连接字符串格式为"Server={server};Initial Catalog={database};Persist Security Info=False;User ID={user};Password={password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  • 单个电话(例如,来自邮递员的电话)很快,大约150毫秒:

我该怎么做才能进一步调查等待时间的原因?

Nice PS:部署到Linux Web应用程序(使用P1V2,S1大小是2倍)时,我看到了5倍的改进,我感到很惊讶。

c# azure-sql-database azure-web-sites dapper asp.net-core-3.1
1个回答
0
投票

[不知道您的情况,很难确定,但是我已经验证了Azure SQL比SQL Server慢很多,这是因为即使在相同的数据中,Azure SQL的位置也离执行服务器不够近。中心。

在我的最后一个位置,我们移至Azure SQL,由于我们的应用程序是繁重的计算应用程序,因此我们移回了位于虚拟机上的SQL Server,并使用SQL Data Sync将数据发送到Azure SQL进行备份。

这无助于解决您的问题,但从我发现的情况来看,Azure SQL最适合用于后端作业。

© www.soinside.com 2019 - 2024. All rights reserved.