|
5 | 5 | using System.Threading.Tasks; |
6 | 6 | using System.Threading.Tasks.Dataflow; |
7 | 7 |
|
8 | | -namespace Open.ChannelExtensions.ComparisonTests |
| 8 | +namespace Open.ChannelExtensions.ComparisonTests; |
| 9 | + |
| 10 | +static class Program |
9 | 11 | { |
10 | | - class Program |
| 12 | + static async Task Main() |
11 | 13 | { |
12 | | - static async Task Main() |
| 14 | + const int repeat = 50; |
| 15 | + const int concurrency = 4; |
| 16 | + const int testSize = 30000001; |
| 17 | + |
13 | 18 | { |
14 | | - const int repeat = 50; |
15 | | - const int concurrency = 4; |
16 | | - const int testSize = 30000001; |
17 | | - |
18 | | - { |
19 | | - Console.WriteLine("Standard DataFlow operation test..."); |
20 | | - var block = new ActionBlock<int>(async i => await Delay(i)); |
21 | | - var sw = Stopwatch.StartNew(); |
22 | | - foreach (int i in Enumerable.Range(0, repeat)) |
23 | | - block.Post(i); |
24 | | - block.Complete(); |
25 | | - await block.Completion; |
26 | | - sw.Stop(); |
27 | | - Console.WriteLine(sw.Elapsed); |
28 | | - Console.WriteLine(); |
29 | | - } |
30 | | - |
31 | | - await BasicTests.ReadAll(testSize); |
32 | | - await BasicTests.ReadAllAsync(testSize); |
33 | | - await BasicTests.BatchThenJoin(testSize, 5001); |
34 | | - await BasicTests.BatchJoin(testSize, 50); |
35 | | - |
36 | | - { |
37 | | - Console.WriteLine("Standard Channel filter test..."); |
38 | | - System.Collections.Generic.IEnumerable<ValueTask<int>> source = Enumerable |
39 | | - .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
40 | | - .Select((t, i) => t(i)); |
41 | | - |
42 | | - var sw = Stopwatch.StartNew(); |
43 | | - long total = await source |
44 | | - .ToChannelAsync(singleReader: true) |
45 | | - .Filter(i => i % 2 == 0) |
46 | | - .ReadAll(Dummy); |
47 | | - sw.Stop(); |
48 | | - |
49 | | - Debug.Assert(total == repeat / 2); |
50 | | - Console.WriteLine(sw.Elapsed); |
51 | | - Console.WriteLine(); |
52 | | - } |
53 | | - |
54 | | - { |
55 | | - Console.WriteLine("Concurrent DataFlow operation test..."); |
56 | | - var sw = Stopwatch.StartNew(); |
57 | | - var block = new ActionBlock<int>(async i => await Delay(i), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = concurrency }); |
58 | | - foreach (int i in Enumerable.Range(0, repeat)) |
59 | | - block.Post(i); |
60 | | - block.Complete(); |
61 | | - await block.Completion; |
62 | | - sw.Stop(); |
63 | | - Console.WriteLine(sw.Elapsed); |
64 | | - Console.WriteLine(); |
65 | | - } |
66 | | - |
67 | | - { |
68 | | - Console.WriteLine("Concurrent Channel operation test..."); |
69 | | - var sw = Stopwatch.StartNew(); |
70 | | - await Enumerable |
71 | | - .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
72 | | - .Select((t, i) => t(i)) |
73 | | - .ToChannelAsync(singleReader: false, maxConcurrency: concurrency) |
74 | | - .ReadAllConcurrently(4, Dummy); |
75 | | - sw.Stop(); |
76 | | - Console.WriteLine(sw.Elapsed); |
77 | | - Console.WriteLine(); |
78 | | - } |
79 | | - |
80 | | - { |
81 | | - Console.WriteLine("Pipe operation test..."); |
82 | | - var sw = Stopwatch.StartNew(); |
83 | | - long total = await Enumerable |
84 | | - .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
85 | | - .Select((t, i) => t(i)) |
86 | | - .ToChannelAsync() |
87 | | - .Pipe(i => i * 2) |
88 | | - .ReadAll(Dummy); |
89 | | - sw.Stop(); |
90 | | - Debug.Assert(total == repeat); |
91 | | - Console.WriteLine(sw.Elapsed); |
92 | | - Console.WriteLine(); |
93 | | - } |
94 | | - |
95 | | - { |
96 | | - Console.WriteLine("Transform operation test..."); |
97 | | - var sw = Stopwatch.StartNew(); |
98 | | - await Enumerable |
99 | | - .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
100 | | - .Select((t, i) => t(i)) |
101 | | - .ToChannelAsync() |
102 | | - .Transform(i => i * 2L) |
103 | | - .ReadAll(Dummy); |
104 | | - sw.Stop(); |
105 | | - Console.WriteLine(sw.Elapsed); |
106 | | - Console.WriteLine(); |
107 | | - } |
| 19 | + Console.WriteLine("Standard DataFlow operation test..."); |
| 20 | + var block = new ActionBlock<int>(async i => await Delay(i)); |
| 21 | + var sw = Stopwatch.StartNew(); |
| 22 | + foreach (int i in Enumerable.Range(0, repeat)) |
| 23 | + block.Post(i); |
| 24 | + block.Complete(); |
| 25 | + await block.Completion; |
| 26 | + sw.Stop(); |
| 27 | + Console.WriteLine(sw.Elapsed); |
| 28 | + Console.WriteLine(); |
| 29 | + } |
108 | 30 |
|
109 | | -#if NETCOREAPP3_0 |
110 | | - { |
111 | | - Console.WriteLine("Async Enumerable test..."); |
112 | | - var sw = Stopwatch.StartNew(); |
113 | | - await foreach (var e in Enumerable |
114 | | - .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
115 | | - .Select((t, i) => t(i)) |
116 | | - .ToChannelAsync() |
117 | | - .ReadAllAsync()) |
118 | | - Dummy(e); |
119 | | - sw.Stop(); |
120 | | - Console.WriteLine(sw.Elapsed); |
121 | | - Console.WriteLine(); |
122 | | - } |
123 | | -#endif |
| 31 | + await BasicTests.ReadAll(testSize); |
| 32 | + await BasicTests.ReadAllAsync(testSize); |
| 33 | + await BasicTests.BatchThenJoin(testSize, 5001); |
| 34 | + await BasicTests.BatchJoin(testSize, 50); |
124 | 35 |
|
| 36 | + { |
| 37 | + Console.WriteLine("Standard Channel filter test..."); |
| 38 | + System.Collections.Generic.IEnumerable<ValueTask<int>> source = Enumerable |
| 39 | + .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
| 40 | + .Select((t, i) => t(i)); |
| 41 | + |
| 42 | + var sw = Stopwatch.StartNew(); |
| 43 | + long total = await source |
| 44 | + .ToChannelAsync(singleReader: true) |
| 45 | + .Filter(i => i % 2 == 0) |
| 46 | + .ReadAll(Dummy); |
| 47 | + sw.Stop(); |
| 48 | + |
| 49 | + Debug.Assert(total == repeat / 2); |
| 50 | + Console.WriteLine(sw.Elapsed); |
| 51 | + Console.WriteLine(); |
125 | 52 | } |
126 | 53 |
|
127 | | - static void Dummy(int i) |
128 | 54 | { |
| 55 | + Console.WriteLine("Concurrent DataFlow operation test..."); |
| 56 | + var sw = Stopwatch.StartNew(); |
| 57 | + var block = new ActionBlock<int>(async i => await Delay(i), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = concurrency }); |
| 58 | + foreach (int i in Enumerable.Range(0, repeat)) |
| 59 | + block.Post(i); |
| 60 | + block.Complete(); |
| 61 | + await block.Completion; |
| 62 | + sw.Stop(); |
| 63 | + Console.WriteLine(sw.Elapsed); |
| 64 | + Console.WriteLine(); |
| 65 | + } |
129 | 66 |
|
| 67 | + { |
| 68 | + Console.WriteLine("Concurrent Channel operation test..."); |
| 69 | + var sw = Stopwatch.StartNew(); |
| 70 | + await Enumerable |
| 71 | + .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
| 72 | + .Select((t, i) => t(i)) |
| 73 | + .ToChannelAsync(singleReader: false, maxConcurrency: concurrency) |
| 74 | + .ReadAllConcurrently(4, Dummy); |
| 75 | + sw.Stop(); |
| 76 | + Console.WriteLine(sw.Elapsed); |
| 77 | + Console.WriteLine(); |
130 | 78 | } |
131 | 79 |
|
132 | | - static void Dummy(long i) |
133 | 80 | { |
| 81 | + Console.WriteLine("Pipe operation test..."); |
| 82 | + var sw = Stopwatch.StartNew(); |
| 83 | + long total = await Enumerable |
| 84 | + .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
| 85 | + .Select((t, i) => t(i)) |
| 86 | + .ToChannelAsync() |
| 87 | + .Pipe(i => i * 2) |
| 88 | + .ReadAll(Dummy); |
| 89 | + sw.Stop(); |
| 90 | + Debug.Assert(total == repeat); |
| 91 | + Console.WriteLine(sw.Elapsed); |
| 92 | + Console.WriteLine(); |
| 93 | + } |
134 | 94 |
|
| 95 | + { |
| 96 | + Console.WriteLine("Transform operation test..."); |
| 97 | + var sw = Stopwatch.StartNew(); |
| 98 | + await Enumerable |
| 99 | + .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
| 100 | + .Select((t, i) => t(i)) |
| 101 | + .ToChannelAsync() |
| 102 | + .Transform(i => i * 2L) |
| 103 | + .ReadAll(Dummy); |
| 104 | + sw.Stop(); |
| 105 | + Console.WriteLine(sw.Elapsed); |
| 106 | + Console.WriteLine(); |
135 | 107 | } |
136 | 108 |
|
137 | | - static async ValueTask<int> Delay(int i) |
| 109 | +#if NETCOREAPP3_0 |
138 | 110 | { |
139 | | - await Task.Delay(100); |
140 | | - return i; |
| 111 | + Console.WriteLine("Async Enumerable test..."); |
| 112 | + var sw = Stopwatch.StartNew(); |
| 113 | + await foreach (var e in Enumerable |
| 114 | + .Repeat((Func<int, ValueTask<int>>)Delay, repeat) |
| 115 | + .Select((t, i) => t(i)) |
| 116 | + .ToChannelAsync() |
| 117 | + .ReadAllAsync()) |
| 118 | + Dummy(e); |
| 119 | + sw.Stop(); |
| 120 | + Console.WriteLine(sw.Elapsed); |
| 121 | + Console.WriteLine(); |
141 | 122 | } |
| 123 | +#endif |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + static void Dummy(int i) |
| 128 | + { |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + static void Dummy(long i) |
| 133 | + { |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + static async ValueTask<int> Delay(int i) |
| 138 | + { |
| 139 | + await Task.Delay(100); |
| 140 | + return i; |
142 | 141 | } |
143 | 142 | } |
0 commit comments