为什么这个 C# 数据处理应用程序的吞吐量远低于服务器的原始能力?

Why is the throughput of this C# data processing app so much lower than the raw capabilities of the server?

我已经整理了一个小测试工具来诊断为什么我的 C# 数据处理应用程序的吞吐量(其核心功能使用非阻塞 IO 从远程数据库服务器中以 100 个批次选择记录并对它们执行简单处理) 比它可能的要低得多。我观察到,在 运行ning 期间,该应用程序在 CPU(<3%)、网络或磁盘 IO 或 RAM 方面没有遇到瓶颈,并且不会对数据库服务器(数据数据库上的设置几乎总是完全在 RAM 中)。如果我 运行 并行应用程序的多个实例,我可以获得最多约 45 个实例,延迟仅降低约 10%,但在数据库服务器上的 CPU 利用率变为之前,吞吐量增加了 45 倍瓶颈(此时,客户端上仍然没有资源瓶颈)。

我的问题是为什么 TPL 不增加运行中的任务数量或以其他方式增加吞吐量,而客户端服务器能够提供更高的吞吐量?

简化代码摘录:

    public static async Task ProcessRecordsAsync()
    {
        int max = 10000;
        var s = new Stopwatch();
        s.Start();
        Parallel.For(0, max, async x => 
        {
            await ProcessFunc();
        });
        s.Stop();
        Console.WriteLine("{2} Selects completed in {0} ms ({1} per ms).", s.ElapsedMilliseconds, ((float)s.ElapsedMilliseconds) / max, max);
    }

    public static async Task ProcessFunc()
    {
        string sql = "select top 100 MyTestColumn from MyTestTable order by MyTestColumn desc;";
        string connStr = "<blah>...";

        using (SqlConnection conn = new SqlConnection(connStr))
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                DbDataReader rdr = await cmd.ExecuteReaderAsync();

                while (rdr.Read())
                {
                    // do simple processing here
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

您的示例可能受到应用程序中 pooled SQL Connections 的最大数量限制,默认情况下为 100。这可能解释了为什么当 运行 个应用程序的多个实例时您会获得更多的吞吐量。您可以尝试监控 SQL 服务器中的连接数,看看是否是这种情况。

Parallel For 不会试图扼杀处理器的生命并最大化为您工作的并发线程数。它使用核心数量作为起点,并可能根据工作负载的性质逐渐增加。参见 this question

碰巧,您实际上 有阻塞 IO...在打开连接和读取行时。您可以试试这个:

//....
using (var conn = new SqlConnection(connStr))
{
  await conn.OpenAsync();
  SqlCommand cmd = new SqlCommand(sql, conn);
  try
  {
    using ( var rdr = await cmd.ExecuteReaderAsync())
    { 
      while (await rdr.ReadAsync())
      {
        // do simple processing here
      }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }
}
//...