-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAsyncTester.cs
80 lines (63 loc) · 2.64 KB
/
AsyncTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Open.Threading;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace AsyncFileWriterTester
{
public class AsyncTester
{
public readonly string FileName;
public AsyncTester(string fileName = "AsyncFileWriterTest.txt")
{
FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
}
public async Task<(int TotalBytesQueued, TimeSpan AggregateTimeWaiting, TimeSpan Elapsed)> Run(Func<string, Func<Func<string, Task>, Task>, Task> context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
Contract.EndContractBlock();
var dir = Environment.CurrentDirectory;
var filePath = Path.Combine(dir, FileName);
File.Delete(filePath); // Start from scratch. (comment out for further appends.)
var telemetry = new ConcurrentBag<(int bytes, TimeSpan time)>();
var sw = Stopwatch.StartNew();
await context(filePath, async writeHandler =>
{
async Task write(int i)
{
var message = $"{i}) {DateTime.Now} 00000000000000000000000000000000111111111111111111111111111222222222222222222222222222\n";
var t = Stopwatch.StartNew();
await writeHandler(message);
telemetry.Add((message.Length, t.Elapsed));
}
await ParallelAsync.ForAsync(0, 10000, write);
await ParallelAsync.ForAsync(10000, 20000, write);
//writer.Fault(new Exception("Stop!"));
await Task.Delay(1);
await ParallelAsync.ForAsync(20000, 100000, write);
await Task.Delay(1000); // Demonstrate that when nothing buffered the active stream closes.
await ParallelAsync.ForAsync(100000, 1000000, write);
});
sw.Stop();
var actualBytes = new FileInfo(filePath).Length;
var (bytes, time) = telemetry.Aggregate((a, b) => (a.bytes + b.bytes, a.time + b.time));
Debug.Assert(actualBytes == bytes, "Actual byte count does not match the queued bytes.");
return (bytes, time, sw.Elapsed);
}
public static async Task RunAndReportToConsole(Func<string, Func<Func<string, Task>, Task>, Task> context, string fileName = "AsyncFileWriterTest.txt")
=> (await new AsyncTester(fileName).Run(context)).EmitToConsole();
public static Task TestAsyncFileWriter(int boundedCapacity, bool asyncFileWrite)
{
Console.WriteLine("{0:#,##0} bounded capacity.", boundedCapacity);
return RunAndReportToConsole(async (filePath, handler) =>
{
var writer = new AsyncFileWriter(filePath, boundedCapacity, asyncFileStream: true, asyncFileWrite: asyncFileWrite);
await handler(s => writer.AddAsync(s));
await writer.DisposeAsync();
});
}
}
}