Start
Elapsed 00:00:09.9404484 to process 150 requests to real_async_with_await
Elapsed 00:00:07.4861382 to process 150 requests to real_async_with_ContinueWith
Elapsed 00:01:19.0962694 to process 150 requests to lying_async_with_Wait
Elapsed 00:00:37.6250714 to process 150 requests to honest_and_simple_synchronous
Elapsed 00:00:08.0892999 to process 150 requests to real_async_with_await
Elapsed 00:00:07.3898359 to process 150 requests to real_async_with_ContinueWith
Elapsed 00:00:06.7302149 to process 150 requests to lying_async_with_Wait
Elapsed 00:00:06.7629274 to process 150 requests to honest_and_simple_synchronous
Elapsed 00:00:07.8457549 to process 150 requests to real_async_with_await
Elapsed 00:00:07.7699032 to process 150 requests to real_async_with_ContinueWith
Elapsed 00:00:14.2671408 to process 150 requests to lying_async_with_Wait
Elapsed 00:00:10.5924920 to process 150 requests to honest_and_simple_synchronous
Elapsed 00:00:08.8338700 to process 150 requests to real_async_with_await
Elapsed 00:00:07.7864875 to process 150 requests to real_async_with_ContinueWith
Elapsed 00:00:13.2418521 to process 150 requests to lying_async_with_Wait
Elapsed 00:00:16.3558802 to process 150 requests to honest_and_simple_synchronous
Press ENTER to continue . . .
Created
June 23, 2017 19:29
-
-
Save andresmoschini/a03d916ddf2ff12d21265ec7bb549c18 to your computer and use it in GitHub Desktop.
Test Async/Await benefits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace Client | |
{ | |
class Program | |
{ | |
private static readonly HttpClient _httpClient = new HttpClient(); | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Start"); | |
var actions = new string[] | |
{ | |
"real_async_with_await", | |
"real_async_with_ContinueWith", | |
"lying_async_with_Wait", | |
"honest_and_simple_synchronous" | |
}; | |
foreach (var action in actions) | |
{ | |
Test(150, action); | |
} | |
foreach (var action in actions) | |
{ | |
Test(150, action); | |
} | |
foreach (var action in actions) | |
{ | |
Test(150, action); | |
} | |
foreach (var action in actions) | |
{ | |
Test(150, action); | |
} | |
Console.WriteLine("Press ENTER to continue . . ."); | |
Console.ReadLine(); | |
} | |
private static void Test(int count, string action) | |
{ | |
var stopwatch = Stopwatch.StartNew(); | |
var tasks = Enumerable.Repeat(true, count).Select(x => DoRequest(action)).ToArray(); | |
Task.WaitAll(tasks); | |
Console.WriteLine($"Elapsed {stopwatch.Elapsed} to process {count} requests to {action}"); | |
} | |
private static async Task<string> DoRequest(string action) | |
{ | |
var response = await _httpClient.GetAsync($"http://localhost:50765/{action}"); | |
var content = await response.Content.ReadAsStringAsync(); | |
return content; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.AspNetCore.Builder; | |
using System.IO; | |
namespace Service | |
{ | |
public class TestController : Controller | |
{ | |
private static Task DoSomethingAsync() | |
{ | |
return Task.Delay(1000); | |
} | |
[HttpGet("real_async_with_await")] | |
public async Task<string> real_async_with_await() | |
{ | |
await DoSomethingAsync(); | |
return "OK!"; | |
} | |
[HttpGet("real_async_with_ContinueWith")] | |
public Task<string> real_async_with_ContinueWith() | |
{ | |
return DoSomethingAsync().ContinueWith(t => "OK!"); | |
} | |
[HttpGet("lying_async_with_Wait")] | |
public Task<string> lying_async_with_Wait() | |
{ | |
var t = DoSomethingAsync(); | |
t.Wait(); | |
return Task.FromResult("OK!"); | |
} | |
[HttpGet("honest_and_simple_synchronous")] | |
public string honest_and_simple_synchronous() | |
{ | |
var t = DoSomethingAsync(); | |
t.Wait(); | |
return "OK!"; | |
} | |
} | |
public class Startup | |
{ | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath); | |
Configuration = builder.Build(); | |
} | |
public IConfigurationRoot Configuration { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseMvc(); | |
} | |
public static void Main(string[] args) | |
{ | |
var host = new WebHostBuilder() | |
.UseKestrel() | |
.UseContentRoot(Directory.GetCurrentDirectory()) | |
.UseIISIntegration() | |
.UseStartup<Startup>() | |
.Build(); | |
host.Run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment