|
| 1 | +module ApplicativeTests |
| 2 | + |
| 3 | + |
| 4 | +open BenchmarkDotNet.Attributes |
| 5 | +open FsToolkit.ErrorHandling |
| 6 | +open System.Threading |
| 7 | +open System |
| 8 | + |
| 9 | +let successSlowSync (time : TimeSpan) = |
| 10 | + Thread.Sleep time |
| 11 | + Ok time.Ticks |
| 12 | + |
| 13 | +let errorSlowSync (time : TimeSpan) = |
| 14 | + Thread.Sleep time |
| 15 | + Error "failed" |
| 16 | + |
| 17 | +[<MemoryDiagnoser>] |
| 18 | +type Result_BindvsAndCEBenchmarks () = |
| 19 | + |
| 20 | + member this.ValuesForDelay = [| |
| 21 | + TimeSpan.FromMilliseconds (0.) |
| 22 | + TimeSpan.FromMilliseconds (10.) |
| 23 | + TimeSpan.FromMilliseconds (100.) |
| 24 | + |] |
| 25 | + // let this.delay = TimeSpan.FromMilliseconds 100. |
| 26 | + [<ParamsSource("ValuesForDelay")>] |
| 27 | + member val public delay = |
| 28 | + TimeSpan.MinValue |
| 29 | + with get,set |
| 30 | + |
| 31 | + |
| 32 | + [<Benchmark(Baseline = true)>] |
| 33 | + member this.All_Success_Bind() = |
| 34 | + result { |
| 35 | + let! r1 = successSlowSync this.delay |
| 36 | + let! r2 = successSlowSync this.delay |
| 37 | + let! r3 = successSlowSync this.delay |
| 38 | + return r1 + r2 + r3 |
| 39 | + } |
| 40 | + |> ignore |
| 41 | + |
| 42 | + [<Benchmark>] |
| 43 | + member this.All_Success_And() = |
| 44 | + result { |
| 45 | + let! r1 = successSlowSync this.delay |
| 46 | + and! r2 = successSlowSync this.delay |
| 47 | + and! r3 = successSlowSync this.delay |
| 48 | + return r1 + r2 + r3 |
| 49 | + } |
| 50 | + |> ignore |
| 51 | + |
| 52 | + [<Benchmark>] |
| 53 | + member this.Fail_First_Bind() = |
| 54 | + result { |
| 55 | + let! r1 = errorSlowSync this.delay |
| 56 | + let! r2 = successSlowSync this.delay |
| 57 | + let! r3 = successSlowSync this.delay |
| 58 | + return r1 + r2 + r3 |
| 59 | + } |
| 60 | + |> ignore |
| 61 | + |
| 62 | + [<Benchmark>] |
| 63 | + member this.Fail_First_And() = |
| 64 | + result { |
| 65 | + let! r1 = errorSlowSync this.delay |
| 66 | + and! r2 = successSlowSync this.delay |
| 67 | + and! r3 = successSlowSync this.delay |
| 68 | + return r1 + r2 + r3 |
| 69 | + } |
| 70 | + |> ignore |
| 71 | + |
| 72 | + [<Benchmark>] |
| 73 | + member this.Fail_Mid_Bind() = |
| 74 | + result { |
| 75 | + let! r1 = successSlowSync this.delay |
| 76 | + let! r2 = errorSlowSync this.delay |
| 77 | + let! r3 = successSlowSync this.delay |
| 78 | + return r1 + r2 + r3 |
| 79 | + } |
| 80 | + |> ignore |
| 81 | + |
| 82 | + [<Benchmark>] |
| 83 | + member this.Fail_Mid_And() = |
| 84 | + result { |
| 85 | + let! r1 = successSlowSync this.delay |
| 86 | + and! r2 = errorSlowSync this.delay |
| 87 | + and! r3 = successSlowSync this.delay |
| 88 | + return r1 + r2 + r3 |
| 89 | + } |
| 90 | + |> ignore |
| 91 | + |
| 92 | + [<Benchmark>] |
| 93 | + member this.Fail_Last_Bind() = |
| 94 | + result { |
| 95 | + let! r1 = successSlowSync this.delay |
| 96 | + let! r2 = successSlowSync this.delay |
| 97 | + let! r3 = errorSlowSync this.delay |
| 98 | + return r1 + r2 + r3 |
| 99 | + } |
| 100 | + |> ignore |
| 101 | + |
| 102 | + [<Benchmark>] |
| 103 | + member this.Fail_Last_And() = |
| 104 | + result { |
| 105 | + let! r1 = successSlowSync this.delay |
| 106 | + and! r2 = successSlowSync this.delay |
| 107 | + and! r3 = errorSlowSync this.delay |
| 108 | + return r1 + r2 + r3 |
| 109 | + } |
| 110 | + |> ignore |
0 commit comments