Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active March 17, 2018 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/f8c0eed3830385d478ad058f5785ed56 to your computer and use it in GitHub Desktop.
Save guitarrapc/f8c0eed3830385d478ad058f5785ed56 to your computer and use it in GitHub Desktop.
Serial? PSJob? C# Async?
async Task Main()
{
var client = new HttpClient();
await Task.WhenAll(Enumerable.Range(0, 100).Select(x => client.GetStringAsync("https://google.com")));
}
Measure-Command {$job = 1..100 | %{Invoke-RestMethod -Method Get -Uri https://google.com &}; Receive-Job $job}
Measure-Command {1..100 | %{Invoke-RestMethod -Method Get -Uri https://google.com}}
$csharp = "using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public class AsyncRestMethod
{
public static HttpClient client = new HttpClient();
public static async Task<string[]> GetStringAsync(string uri, int count) {
return await Task.WhenAll(Enumerable.Range(0, count).Select(x => client.GetStringAsync(uri)));
}
}
"
Add-Type -TypeDefinition $csharp -Language CSharp
Measure-Command {[AsyncRestMethod]::GetStringAsync("https://google.com", 100).Result;}
# PowerShellSerial.ps1
TotalSeconds : 25.7549936
# PowerShellJob.ps1
TotalSeconds : 15.8745210
# PowerShellWithCSharpAsyncTask.ps1
TotalSeconds : 0.6443892
# CSharpAsyncAwait.cs
00:00:00.97544477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment