【发布时间】: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