在C#中测量多个任务的持续时间显示不正确的时间跨度

在C#中测量多个任务的持续时间显示不正确的时间跨度,c#,task,stopwatch,C#,Task,Stopwatch,我试图大致衡量完成一系列异步任务所需的时间。我的方法是 var stopwatch = Stopwatch.StartNew(); await Task.WhenAll(tasks); stopwatch.Stop(); 我坐着等待手术结束超过5分钟,但秒表会显示1.5秒已经过去 我还尝试: var stopwatch = Stopwatch.StartNew(); await Task.WhenAll(tasks).ContinueWith(task => stopwatch.Stop

我试图大致衡量完成一系列异步任务所需的时间。我的方法是

var stopwatch = Stopwatch.StartNew();
await Task.WhenAll(tasks);
stopwatch.Stop();
我坐着等待手术结束超过5分钟,但秒表会显示1.5秒已经过去

我还尝试:

var stopwatch = Stopwatch.StartNew();
await Task.WhenAll(tasks).ContinueWith(task => stopwatch.Stop());
…但这仍然报告仅发生了1.5秒,但仍需要超过5分钟才能运行

tasks
是类型为
IEnumerable
的变量

我正在努力寻找一种诊断方法,希望能得到一些帮助。谢谢

更新 下面是一个更完整的示例:

public async Task<BulkOperationResponse<Source>> BulkInsertSourceDocumentsAsync(IEnumerable<Source> documents)
{
    _logger.LogInformation("Beginning bulk insert...");

    var tasks = new List<Task<OperationResponse<Source>>>(documents.Count());
    tasks.AddRange(documents.Select(document =>
        _orderStatusesContainer.CreateItemAsync(document, new PartitionKey(document.CUSTACCOUNT))
            .CaptureOperationResponse(document)));

    var response = await ExecuteTasksAsync(tasks);

    return response;
}
公共异步任务bulkInsertSourceDocumentsSync(IEnumerable文档)
{
_logger.LogInformation(“开始批量插入…”);
var tasks=新列表(documents.Count());
tasks.AddRange(documents.Select(document=>
_OrderStatuseContainer.CreateItemAsync(文档,新分区键(document.CUSTACCOUNT))
.CaptureOperationResponse(文件));
var response=await ExecuteTasksAsync(任务);
返回响应;
}
public异步任务ExecuteTasksAsync(
IReadOnlyCollection任务)
{
_logger.LogInformation($“正在执行{tasks.Count}个任务”);
//TODO:这似乎报告的时间不对
//在GitHub上打开了一个问题:https://github.com/MicrosoftDocs/azure-docs/issues/53193
var stopwatch=stopwatch.StartNew();
等待任务。何时(任务);
秒表;
_logger.LogInformation($“在{stopwatch.appeased}中完成了{tasks.Count}个任务”);
返回新的BulkOperationResponse
{
TotalTimeTake=stopwatch.Appeased,//TODO:确认时间是否正确。
TotalRequestUnitsConsumed=tasks.Sum(task=>task.Result.RequestUnitsConsumed),
SuccessfulDocuments=tasks.Count(task=>task.Result.IsSuccessful),
失败=任务。其中(任务=>!任务。结果。IsSuccessful)
.Select(task=>(task.Result.Item,task.Result.CosmosException)).ToList()
};
}

在我的测试中,我导入了400000个文档,它们都在5分钟后成功完成。

到底什么需要5分钟?使用调试器跳过等待?或者还有更复杂的事情吗?请记住,
当所有的
正在执行一组已经由其他事情启动的任务时。根据您获取这些
任务的方式,可能无法测量大量工作量每个任务都是CosmosDB操作,如果我在
stopwatch.Stop()
上或之后设置断点,则
stopwatch.appeased
属性会报告1.5秒的
TimeSpan
@Damien\u不信者,我更新了我的问题,更具体地说,这肯定是怎么回事。大部分工作完成后才会调用Stopwatch.StartNew()
。具体需要5分钟的时间?使用调试器跳过等待?或者还有更复杂的事情吗?请记住,
当所有的
正在执行一组已经由其他事情启动的任务时。根据您获取这些
任务的方式,可能无法测量大量工作量每个任务都是CosmosDB操作,如果我在
stopwatch.Stop()
上或之后设置断点,则
stopwatch.appeased
属性会报告1.5秒的
TimeSpan
@Damien\u不信者,我更新了我的问题,更具体地说,这肯定是怎么回事。大部分工作完成后才会调用Stopwatch.StartNew()
public async Task<BulkOperationResponse<T>> ExecuteTasksAsync<T>(
    IReadOnlyCollection<Task<OperationResponse<T>>> tasks)
{
    _logger.LogInformation($"Executing {tasks.Count} tasks");

    // TODO: This seems to be reporting the wrong time
    // Opened an issue on GitHub: https://github.com/MicrosoftDocs/azure-docs/issues/53193
    var stopwatch = Stopwatch.StartNew();
    await Task.WhenAll(tasks);
    stopwatch.Stop();

    _logger.LogInformation($"Finished {tasks.Count} tasks in {stopwatch.Elapsed}");

    return new BulkOperationResponse<T>
    {
        TotalTimeTaken = stopwatch.Elapsed, // TODO: Confirm time is correct.
        TotalRequestUnitsConsumed = tasks.Sum(task => task.Result.RequestUnitsConsumed),
        SuccessfulDocuments = tasks.Count(task => task.Result.IsSuccessful),
        Failures = tasks.Where(task => !task.Result.IsSuccessful)
            .Select(task => (task.Result.Item, task.Result.CosmosException)).ToList()
    };
}