|
| 1 | +module AsyncSequences |
| 2 | + |
| 3 | +open BenchmarkDotNet.Attributes |
| 4 | +open System.Threading |
| 5 | +open System.Threading.Tasks |
| 6 | + |
| 7 | +type AsyncSeqTaskState<'t> = |
| 8 | + | Idle |
| 9 | + | Ok of 't |
| 10 | + | Error of exn |
| 11 | + | Cancelled |
| 12 | + |
| 13 | +let sequence0 (t: seq<Async<_>>) : Async<seq<_>> = |
| 14 | + async { |
| 15 | + let! ct = Async.CancellationToken |
| 16 | + |
| 17 | + return |
| 18 | + seq { |
| 19 | + use enum = t.GetEnumerator() |
| 20 | + |
| 21 | + while enum.MoveNext() do |
| 22 | + yield Async.RunSynchronously(enum.Current, cancellationToken = ct) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | +let sequence_StartImmediateAsTask (t: seq<Async<_>>) : Async<seq<_>> = |
| 27 | + async { |
| 28 | + let startImmediateAsTask ct a = |
| 29 | + Async.StartImmediateAsTask(a, ct).Result |
| 30 | + |
| 31 | + let! ct = Async.CancellationToken |
| 32 | + return t |> Seq.map (startImmediateAsTask ct) |
| 33 | + } |
| 34 | + |
| 35 | +let sequence_ManualResetEventSlim (t: seq<Async<_>>) : Async<seq<_>> = |
| 36 | + async { |
| 37 | + let startImmediateAsTask ct (a: Async<'t>) : 't = |
| 38 | + let mutable state = |
| 39 | + AsyncSeqTaskState<'t>.Idle |
| 40 | + |
| 41 | + let mutex = new ManualResetEventSlim(false) |
| 42 | + |
| 43 | + let setState newState = |
| 44 | + try |
| 45 | + state <- newState |
| 46 | + finally |
| 47 | + mutex.Set() |
| 48 | + |
| 49 | + Async.StartWithContinuations( |
| 50 | + a, |
| 51 | + (fun k -> AsyncSeqTaskState<'t>.Ok k |> setState), |
| 52 | + (fun e -> AsyncSeqTaskState<'t>.Error e |> setState), |
| 53 | + (fun _ -> setState AsyncSeqTaskState<'t>.Cancelled), |
| 54 | + ct |
| 55 | + ) |
| 56 | + |
| 57 | + mutex.Wait() |
| 58 | + |
| 59 | + match state with |
| 60 | + | Idle |
| 61 | + | Cancelled -> TaskCanceledException() |> raise |
| 62 | + | Ok res -> res |
| 63 | + | Error e -> raise e |
| 64 | + |
| 65 | + let! ct = Async.CancellationToken |
| 66 | + return t |> Seq.map (startImmediateAsTask ct) |
| 67 | + } |
| 68 | + |
| 69 | +let SyncSum = async { |
| 70 | + return 1 + 1 |
| 71 | +} |
| 72 | + |
| 73 | +let AsyncWith1SecSleep = async { |
| 74 | + do! Async.Sleep 1 |
| 75 | + return 1 + 1 |
| 76 | +} |
| 77 | + |
| 78 | +let SyncAsyncSleepOverAsync = async { |
| 79 | + Async.RunSynchronously (Async.Sleep 5) |
| 80 | + return 1 + 1 |
| 81 | +} |
| 82 | + |
| 83 | +type Benchmarks() = |
| 84 | + [<Params(10, 100, 1000)>] |
| 85 | + member val public times = 0 with get, set |
| 86 | + |
| 87 | + [<Params(1)>] |
| 88 | + member val public threads = 0 with get, set |
| 89 | + |
| 90 | + [<GlobalSetup>] |
| 91 | + member self.GlobalSetup() = |
| 92 | + if self.threads > 0 then |
| 93 | + ThreadPool.SetMinThreads (self.threads, self.threads) |> ignore |
| 94 | + ThreadPool.SetMaxThreads (self.threads, self.threads) |> ignore |
| 95 | + |
| 96 | + [<Benchmark(Baseline = true)>] |
| 97 | + member this.Base() = |
| 98 | + if this.threads = 1 then |
| 99 | + failwith "This function will fail with just one available thread." |
| 100 | + seq { |
| 101 | + for _ = 1 to this.times do |
| 102 | + yield SyncSum |
| 103 | + yield AsyncWith1SecSleep |
| 104 | + yield SyncAsyncSleepOverAsync } |
| 105 | + |> sequence0 |
| 106 | + |> Async.RunSynchronously |
| 107 | + |> Seq.toArray |
| 108 | + |
| 109 | + [<Benchmark>] |
| 110 | + member this.StartImmediateAsTask() = |
| 111 | + seq { |
| 112 | + for _ = 1 to this.times do |
| 113 | + yield SyncSum |
| 114 | + yield AsyncWith1SecSleep |
| 115 | + yield SyncAsyncSleepOverAsync } |
| 116 | + |> sequence_StartImmediateAsTask |
| 117 | + |> Async.RunSynchronously |
| 118 | + |> Seq.toArray |
| 119 | + |
| 120 | + [<Benchmark>] |
| 121 | + member this.ManualResetEventSlim() = |
| 122 | + seq { |
| 123 | + for _ = 1 to this.times do |
| 124 | + yield SyncSum |
| 125 | + yield AsyncWith1SecSleep |
| 126 | + yield SyncAsyncSleepOverAsync } |
| 127 | + |> sequence_ManualResetEventSlim |
| 128 | + |> Async.RunSynchronously |
| 129 | + |> Seq.toArray |
0 commit comments