24
loading...
This website collects cookies to deliver better user experience
public class Example { public string Hello { get; set; } }
static void Main()
{
// Let's go to sleep synchronously ...
Thread.Sleep(1000);
// Let's load some JSON files synchronously ...
var example = JsonSerializer.Deserialize<Example>(File.ReadAllText("example.json"));
// Let's make a web request synchronously ...
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.to");
var client = new HttpClient();
var response = client.Send(request);
}
public class Example { public string Hello { get; set; } }
static async Task Main()
{
// let's sleep asynchronously ...
await Task.Delay(1000);
// Let's load some JSON files asynchronously ...
await using FileStream stream = File.OpenRead("example.json");
var example = await JsonSerializer.DeserializeAsync<Example>(stream);
// Let's make a web request asynchronously ...
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.to");
var client = new HttpClient();
var response = await client.SendAsync(request);
}
public class Example { public string Hello { get; set; } }
[RPlotExporter]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net50, baseline: true)]
public class Benchmarker
{
[Params(16, 64, 256)] public int N;
[Benchmark]
public void A_SyncLoop() => A_SyncLoop(N);
[Benchmark]
public void B_SyncThreaded() => B_SyncThreaded(N);
[Benchmark]
public async Task C_AsyncLoop() => await C_AsyncLoop(N);
[Benchmark]
public async Task D_AsyncPooled() => await D_AsyncPooled(N);
public void A_SyncLoop(int numberOfFiles)
{
for (var ii = 0; ii < numberOfFiles; ++ii)
{
var example = JsonSerializer.Deserialize<Example>(File.ReadAllText("example.json"));
}
}
public void B_SyncThreaded(int numberOfFiles)
{
var threads = new Thread[numberOfFiles];
for (var ii = 0; ii < numberOfFiles; ++ii)
{
threads[ii] = new Thread(() =>
{
var example = JsonSerializer.Deserialize<Example>(File.ReadAllText("example.json"));
});
// Don't block waiting for the result of this specific
// thread ...
threads[ii].Start();
}
// ... block waiting for the results of all threads
// running concurrently.
for (var ii = 0; ii < numberOfFiles; ++ii)
threads[ii].Join();
}
public async Task C_AsyncLoop(int numberOfFiles)
{
for (var ii = 0; ii < numberOfFiles; ++ii)
{
await using FileStream stream = File.OpenRead("example.json");
var example = await JsonSerializer.DeserializeAsync<Example>(stream);
}
}
// Note this code looks strange because want to
// separate the task creation from the task run. We cannot
// simply return a task from an async method because it
// will be started automatically. I have tried to make this
// code look as similar as possible to the threaded
// implementation.
public async Task D_AsyncPooled(int numberOfFiles)
{
var tasks = new Task[numberOfFiles];
for (var ii = 0; ii < numberOfFiles; ++ii)
{
var func = new Func<Task>(async () =>
{
await using FileStream stream = File.OpenRead("example.json");
var example = await JsonSerializer.DeserializeAsync<Example>(stream);
});
tasks[ii] = func.Invoke();
}
// ... await the results of all tasks running as concurrently as the
// thread pool will allow.
await Task.WhenAll(tasks);
}
}
public static void Main()
{
var summary = BenchmarkRunner.Run<Benchmarker>();
}
public class Example
{
public static async Task GoToSleep()
{
// Note I should really be using a logger that uses the
// async file API but I feel lazy so ...
Console.WriteLine($"Async Thread ID: {Thread.CurrentThread.ManagedThreadId}");
await Task.Delay(1000);
}
}
class Program
{
static async Task Main()
{
Console.WriteLine($"Main Thread ID: {Thread.CurrentThread.ManagedThreadId}");
await Example.GoToSleep();
}
}
Main Thread ID: 1
Async Thread ID: 1