【问题标题】:Stopwatch against C# Async: Am I fooled by the stopwatch or there's something there I don't know?秒表与 C# 异步:我是被秒表愚弄了还是那里有我不知道的东西?
【发布时间】:2015-08-18 15:38:08
【问题描述】:

基本上,我想对 Google BigQuery 的异步 API 调用进行秒表。我打算以两种模式设计测试运行:(1)单线程模式——调用 API,等待它完成任务,然后触发下一个调用。 (2)多线程模式——通过Task.Run启动多个调用,然后全部等待。根据秒表,测试结果非常混乱。在第一种模式下,每个 API 调用在大约 300 毫秒内完成其任务,但在第二种模式下,除了前几次调用外,每个 API 调用都需要 10 秒以上。这是一个巨大的差异! (Windows 8.1 Pro,8GB RAM,2 核 CPU)

这可能是 Google BigQuery 方面的问题,但除此之外,我想确保我正确使用秒表,或者我的意思是我正在测量我想要测量的内容。我是吗?

代码如下:

 static void Main(string[] args)
    {
       // SingleThreadMode();
        MultiThreadMode();
    }

 private static void SingleThreadMode()
    {
        var batch = ReadOneBatchInArrays();
        for (int i = 0; i < 100; i++)
        {
            try
            {
                Task.Run(async () => { await InsertAsync("T20150624", batch, InsertId); }).Wait(-1);
            }
            catch (AggregateException e)
            {
                foreach (var exception in e.InnerExceptions)
                {
                    Console.WriteLine(exception);
                }
            }
        }
        Console.WriteLine("End");
        Console.ReadLine();
    }

private static void MultiThreadMode()
    {
        var batch = ReadOneBatchInArrays();
        IList<Task> tasks = new List<Task>();
        for (int i = 0; i < 100; i++)
        {
            try
            {
                tasks.Add(Task.Run(async () => { await InsertAsync("T20150624", batch, InsertId); }));
            }
            catch (AggregateException e)
            {
                foreach (var exception in e.InnerExceptions)
                {
                    Console.WriteLine(exception);
                }
            }
        }
        Task.WaitAll(tasks.ToArray(),-1);
        Console.WriteLine("End");
        Console.ReadLine();
    }

InsertAsync 中的秒表:

Stopwatch stopwatch = Stopwatch.StartNew();
TableDataInsertAllResponse response = await BqService.Tabledata.InsertAll(request, ProjectId, DatasetId, tableId).ExecuteAsync(); 
stopwatch.Stop();
Logger.Trace("BigQuery.InsertAll#Back|row count|milliseconds~{0}~{1}", rowList.Count, stopwatch.ElapsedMilliseconds);

解决 非常感谢下面的回答。我最终在我的 app.config 中添加了几行,它解决了这个问题。

<system.net>
<connectionManagement>
    <add address="*" maxconnection="2000" />
</connectionManagement>
</system.net>

【问题讨论】:

    标签: c# google-bigquery google-api-dotnet-client


    【解决方案1】:

    您很可能遇到 .Net 中的并发连接限制,如以下问题所示:Max number of concurrent HttpWebRequests

    对单个主机的最大并发请求数是有限的。所以发生的情况是前几个请求立即通过,但是一旦达到限制,后面的请求就会开始阻塞并等待第一个请求完成。

    【讨论】:

      猜你喜欢
      • 2011-09-10
      • 2012-02-08
      • 2018-01-09
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多