Skip to content

Commit

Permalink
Add Computation Expression Benchmarks (#16541)
Browse files Browse the repository at this point in the history
* add benchmarks for various usages of CEs

* refactor

* move CE source files to dedicated ce folder
  • Loading branch information
dawedawe authored Jan 17, 2024
1 parent f6e0313 commit 7e91ae6
Show file tree
Hide file tree
Showing 8 changed files with 12,491 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module FSharp.Benchmarks.ComputationExpressionBenchmarks

open System.IO
open BenchmarkDotNet.Attributes
open FSharp.Compiler.CodeAnalysis
open FSharp.Test.ProjectGeneration

[<Literal>]
let FSharpCategory = "fsharp"

[<MemoryDiagnoser>]
[<BenchmarkCategory(FSharpCategory)>]
type ComputationExpressionBenchmarks() =

let mutable sourceFileName = ""

[<Params("CE100xnest1.fs",
"CE100xnest5.fs",
// "CE100xnest10.fs", // enable if you have the spare time
"CE200xnest5.fs",
"CEwCO500xnest1.fs",
"CEwCO100xnest5.fs")>]
member public this.Source
with get () = File.ReadAllText(__SOURCE_DIRECTORY__ ++ "ce" ++ sourceFileName)
and set f = sourceFileName <- f

member val Benchmark = Unchecked.defaultof<_> with get, set

member this.setup(project) =
let checker = FSharpChecker.Create()
this.Benchmark <- ProjectWorkflowBuilder(project, checker = checker).CreateBenchmarkBuilder()
saveProject project false checker |> Async.RunSynchronously

[<GlobalSetup(Targets = [| "CheckCE"; "CompileCE" |])>]
member this.SetupWithSource() =
this.setup
{ SyntheticProject.Create() with
SourceFiles =
[

{ sourceFile "File" [] with
ExtraSource = this.Source
}
]
OtherOptions = []
}

[<Benchmark>]
member this.CheckCE() =
this.Benchmark { checkFile "File" expectOk }

[<Benchmark>]
member this.CompileCE() = this.Benchmark { compileWithFSC }
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="FileCascadeBenchmarks.fs" />
<Compile Include="GraphTypeCheckingBenchmarks.fs" />
<Compile Include="BackgroundCompilerBenchmarks.fs" />
<Compile Include="ComputationExpressionBenchmarks.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module SomeModule =

type StrChainBuilder() =
member this.Zero() = ""
member this.Delay(f) = f ()
member this.Yield(x: string) = x
member this.Combine(a, b) = a + b

let strchain = StrChainBuilder()

let test =
// 100 x nesting of 1
strchain {
"test0"
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
strchain { "test1" }
}
Loading

0 comments on commit 7e91ae6

Please sign in to comment.