-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAsyncSeqBenchmarks.fs
More file actions
260 lines (226 loc) · 9.19 KB
/
AsyncSeqBenchmarks.fs
File metadata and controls
260 lines (226 loc) · 9.19 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
namespace AsyncSeqBenchmarks
open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Running
open BenchmarkDotNet.Jobs
open BenchmarkDotNet.Engines
open BenchmarkDotNet.Toolchains.InProcess.Emit
open FSharp.Control
/// Core AsyncSeq performance benchmarks focused on foundational operations
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqCoreBenchmarks() =
[<Params(1000, 10000)>]
member val ElementCount = 0 with get, set
/// Benchmark unfoldAsync - core sequence generation
[<Benchmark(Baseline = true)>]
member this.UnfoldAsync() =
let generator state = async {
if state < this.ElementCount then
return Some (state, state + 1)
else
return None
}
AsyncSeq.unfoldAsync generator 0
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark replicate - simple constant generation
[<Benchmark>]
member this.Replicate() =
AsyncSeq.replicate this.ElementCount 42
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark mapAsync - common transformation
[<Benchmark>]
member this.MapAsync() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.mapAsync (fun x -> async.Return (x * 2))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark chooseAsync with high selectivity
[<Benchmark>]
member this.ChooseAsync() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.chooseAsync (fun x -> async.Return (Some (x * 2)))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmarks for append operations (previously had memory leaks)
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqAppendBenchmarks() =
[<Params(10, 50, 100)>]
member val ChainCount = 0 with get, set
/// Benchmark chained appends - tests for memory leaks and O(n²) behavior
[<Benchmark>]
member this.ChainedAppends() =
let mutable result = AsyncSeq.singleton 1
for i in 2 .. this.ChainCount do
result <- AsyncSeq.append result (AsyncSeq.singleton i)
result
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark multiple sequence appends
[<Benchmark>]
member this.MultipleAppends() =
let sequences = [1 .. this.ChainCount] |> List.map (fun i -> AsyncSeq.singleton i)
sequences
|> List.reduce AsyncSeq.append
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmarks for computation builder recursive patterns (previously O(n²))
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqBuilderBenchmarks() =
[<Params(50, 100, 200)>]
member val RecursionDepth = 0 with get, set
/// Benchmark recursive asyncSeq computation - tests for O(n²) regression
[<Benchmark>]
member this.RecursiveAsyncSeq() =
let rec generate cnt = asyncSeq {
if cnt = 0 then () else
let! v = async.Return 1
yield v
yield! generate (cnt-1)
}
generate this.RecursionDepth
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark unfoldAsync equivalent for comparison
[<Benchmark>]
member this.UnfoldAsyncEquivalent() =
AsyncSeq.unfoldAsync (fun cnt -> async {
if cnt = 0 then return None
else
let! v = async.Return 1
return Some (v, cnt - 1)
}) this.RecursionDepth
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmarks for filter, choose, and fold operations (optimised direct-enumerator implementations)
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqFilterChooseFoldBenchmarks() =
[<Params(1000, 10000)>]
member val ElementCount = 0 with get, set
/// Benchmark filterAsync — all elements pass the predicate
[<Benchmark(Baseline = true)>]
member this.FilterAsyncAllPass() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.filterAsync (fun _ -> async.Return true)
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark filterAsync — no elements pass the predicate (entire sequence scanned)
[<Benchmark>]
member this.FilterAsyncNonePass() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.filterAsync (fun _ -> async.Return false)
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark chooseAsync — all elements selected
[<Benchmark>]
member this.ChooseAsyncAllSelected() =
AsyncSeq.replicate this.ElementCount 42
|> AsyncSeq.chooseAsync (fun x -> async.Return (Some x))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark foldAsync — sum all elements
[<Benchmark>]
member this.FoldAsync() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.foldAsync (fun acc x -> async.Return (acc + x)) 0
|> Async.RunSynchronously
|> ignore
/// Benchmarks for multi-step pipeline composition
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqPipelineBenchmarks() =
[<Params(1000, 10000)>]
member val ElementCount = 0 with get, set
/// Benchmark map → filter → fold pipeline (exercises the three optimised combinators together)
[<Benchmark(Baseline = true)>]
member this.MapFilterFold() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.mapAsync (fun x -> async.Return (x * 2))
|> AsyncSeq.filterAsync (fun x -> async.Return (x > 0))
|> AsyncSeq.foldAsync (fun acc x -> async.Return (acc + x)) 0
|> Async.RunSynchronously
|> ignore
/// Benchmark collecting to an array
[<Benchmark>]
member this.ToArray() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.toArrayAsync
|> Async.RunSynchronously
|> ignore
/// Benchmarks for take and skip — common slicing operations
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqSliceBenchmarks() =
/// Benchmark take: stops after N elements
[<Benchmark(Baseline = true)>]
member this.Take() =
AsyncSeq.replicateInfinite 1
|> AsyncSeq.take this.ElementCount
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark skip then iterate remaining elements
[<Benchmark>]
member this.Skip() =
let skipCount = this.ElementCount / 2
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.skip skipCount
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Benchmark skip then take (common pagination pattern)
[<Benchmark>]
member this.SkipThenTake() =
let page = this.ElementCount / 10
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.skip page
|> AsyncSeq.take page
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
[<Params(1000, 10000)>]
member val ElementCount = 0 with get, set
/// Benchmarks for map and mapi variants — ensures the direct-enumerator optimisation
/// for mapiAsync is visible and comparable against mapAsync.
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net80)>]
type AsyncSeqMapiBenchmarks() =
/// Baseline: mapAsync (already uses direct enumerator)
[<Benchmark(Baseline = true)>]
member this.MapAsync() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.mapAsync (fun x -> async.Return (x * 2))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// mapiAsync — now uses direct enumerator; should be close to mapAsync cost
[<Benchmark>]
member this.MapiAsync() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.mapiAsync (fun i x -> async.Return (i, x * 2))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// mapi — synchronous projection variant; dispatches through mapiAsync
[<Benchmark>]
member this.Mapi() =
AsyncSeq.replicate this.ElementCount 1
|> AsyncSeq.mapi (fun i x -> (i, x * 2))
|> AsyncSeq.iterAsync (fun _ -> async.Return())
|> Async.RunSynchronously
/// Entry point for running benchmarks.
/// Delegates directly to BenchmarkSwitcher so all BenchmarkDotNet CLI options
/// (--filter, --job short, --exporters, etc.) work out of the box.
/// Examples:
/// dotnet run -c Release # run all
/// dotnet run -c Release -- --filter '*Filter*' # specific class
/// dotnet run -c Release -- --filter '*' --job short # quick smoke-run
module AsyncSeqBenchmarkRunner =
[<EntryPoint>]
let Main args =
BenchmarkSwitcher
.FromAssembly(typeof<AsyncSeqCoreBenchmarks>.Assembly)
.Run(args)
|> ignore
0