diff --git a/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.XPlat/UsageReport/WriteUsageBurndownData.cs b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.XPlat/UsageReport/WriteUsageBurndownData.cs index ab40fa590..2a6d00863 100644 --- a/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.XPlat/UsageReport/WriteUsageBurndownData.cs +++ b/src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.SourceBuild.Tasks.XPlat/UsageReport/WriteUsageBurndownData.cs @@ -15,6 +15,7 @@ using System.IO; using System.IO.Compression; using System.Linq; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using System.Xml.Linq; using Task = Microsoft.Build.Utilities.Task; @@ -43,7 +44,14 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport [Required] public string OutputFilePath { get; set; } - public override bool Execute() + /// + /// Sends HTTP requests and receives HTTP responses. + /// + private readonly HttpClient client = new(); + + public override bool Execute() => ExecuteAsync().GetAwaiter().GetResult(); + + private async Task ExecuteAsync() { string baselineRelativeFileName = PrebuiltBaselineFile.Replace(RootDirectory, ""); string gitLogCommand = $"log --first-parent --pretty=format:%H,%f,%ci -- {PrebuiltBaselineFile}"; @@ -51,22 +59,26 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport DateTime startTime = DateTime.Now; Log.LogMessage(MessageImportance.High, "Generating summary usage burndown data..."); - ParallelQuery data = ExecuteGitCommand(RootDirectory, gitLogCommand).AsParallel().Select(commitLine => - { - var splitLine = commitLine.Split(','); - var commit = new Commit() + + IEnumerable> getCommitTasks = ExecuteGitCommand(RootDirectory, gitLogCommand) + .Select(async commitLine => { - Sha = splitLine[0], - Title = splitLine[1], - CommitDate = DateTime.Parse(splitLine[2]) - }; - string fileContents = GetFileContents(baselineRelativeFileName, commit.Sha); - Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray(); - commit.PackageVersionCount = usages.Count(); - commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count(); - return commit; - }) - .Select(c => c.ToString()); + var splitLine = commitLine.Split(','); + var commit = new Commit() + { + Sha = splitLine[0], + Title = splitLine[1], + CommitDate = DateTime.Parse(splitLine[2]) + }; + string fileContents = await GetFileContentsAsync(baselineRelativeFileName, commit.Sha); + Usage[] usages = UsageData.Parse(XElement.Parse(fileContents)).Usages.NullAsEmpty().ToArray(); + commit.PackageVersionCount = usages.Count(); + commit.PackageCount = usages.GroupBy(i => i.PackageIdentity.Id).Select(grp => grp.First()).Count(); + return commit; + }); + + Commit[] commits = await System.Threading.Tasks.Task.WhenAll(getCommitTasks); + IEnumerable data = commits.Select(c => c.ToString()); Directory.CreateDirectory(Path.GetDirectoryName(OutputFilePath)); @@ -85,12 +97,8 @@ namespace Microsoft.DotNet.SourceBuild.Tasks.UsageReport /// The relative path (from the git root) to the file. /// The commit sha for the version of the file to get. /// The contents of the specified file. - private string GetFileContents(string relativeFilePath, string commitSha) - { - WebClient client = new WebClient(); - var xmlString = client.DownloadString($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}"); - return xmlString; - } + private Task GetFileContentsAsync(string relativeFilePath, string commitSha) => + client.GetStringAsync($"https://raw.githubusercontent.com/dotnet/source-build/{commitSha}/{relativeFilePath.Replace('\\', '/')}"); /// /// Executes a git command and returns the result.