Skip to content

Commit d8f60d3

Browse files
committed
Adds applicative vs bind benchmarks
1 parent d43314f commit d8f60d3

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed

benchmarks/ApplicativeTests.fs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

benchmarks/Program.fs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ open BenchmarkDotNet.Configs
55
open BenchmarkDotNet.Jobs
66
open BenchmarkDotNet.Environments
77
open FsToolkit.ErrorHandling.Benchmarks
8+
open ApplicativeTests
89

910
[<EntryPoint>]
1011
let main argv =
11-
let cfg =
12+
let cfg =
1213
DefaultConfig.Instance
13-
// .AddJob(Job.Default.WithRuntime(CoreRuntime.Core50))
14+
// .AddJob(Job.Default.WithRuntime(CoreRuntime.Core50))
1415
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core60))
1516
// BenchmarkRunner.Run<EitherMapBenchmarks>() |> ignore
16-
BenchmarkRunner.Run<TaskResult_BindCEBenchmarks>(cfg) |> ignore
17+
// BenchmarkRunner.Run<TaskResult_BindCEBenchmarks>(cfg) |> ignore
1718
// BenchmarkRunner.Run<BindSameBenchmarks>() |> ignore
18-
//
19-
20-
0 // return an integer exit code
19+
20+
BenchmarkRunner.Run<Result_BindvsAndCEBenchmarks>(cfg) |> ignore
21+
//
22+
23+
0 // return an integer exit code

benchmarks/benchmarks.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
<Optimize>true</Optimize>
1313
<Configuration>Release</Configuration>
1414
<IsPackable>false</IsPackable>
15-
1615
<NoWarn>FS1204;FS3501;FS3511</NoWarn>
1716
</PropertyGroup>
1817
<PropertyGroup Condition="'$(Configuration)'=='Release'">
1918
<Tailcalls>true</Tailcalls>
2019
</PropertyGroup>
2120
<ItemGroup>
21+
<Compile Include="ApplicativeTests.fs" />
2222
<Compile Include="Benchmarks.fs" />
2323
<Compile Include="AsyncResultCE.fs" />
2424
<Compile Include="TaskResultCE.fs" />

0 commit comments

Comments
 (0)