refactor: Gitee接口测试使用并发减少响应时间

This commit is contained in:
Argo Zhang 2019-08-14 11:22:24 +08:00
parent f7c9a638ed
commit c7b218f0f5
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
1 changed files with 6 additions and 16 deletions

View File

@ -38,24 +38,14 @@ namespace Bootstrap.Admin.HealthChecks
var urls = new string[] { "Issues", "Pulls", "Releases", "Builds" };
var data = new Dictionary<string, object>();
urls.ToList().ForEach(url =>
Task.WaitAll(urls.Select(url => Task.Run(async () =>
{
var sw = Stopwatch.StartNew();
try
{
var task = _client.GetStringAsync($"/api/Gitee/{url}");
task.Wait(cancellationToken);
}
catch (Exception)
{
}
finally
{
sw.Stop();
data.Add(url, sw.Elapsed);
}
});
Exception error = null;
var result = await _client.GetAsJsonAsync<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
sw.Stop();
data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}");
})).ToArray());
return Task.FromResult(HealthCheckResult.Healthy("Ok", data));
}
}