-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added basic FSharp benchmarks #1546
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b96cd0
Added basic FSharp benchmarks
TIHan 059770d
Changed comment text Roslyn to FSharp
TIHan 8f69956
Getting netcore3.1 references instead of net5
TIHan 44312c8
Merge branch 'master' into fsharp-bench
TIHan 1fd51da
Update azure-pipelines.yml
TIHan b02b73e
Updating to net5
TIHan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. --> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>$(PERFLAB_TARGET_FRAMEWORKS)</TargetFrameworks> | ||
<TargetFrameworks Condition="'$(TargetFrameworks)' == ''">netcoreapp3.1;netcoreapp5.0</TargetFrameworks> | ||
<IsShipping>false</IsShipping> | ||
<!-- this app is using internal Roslyn API and needs to be signed --> | ||
<SignAssembly>true</SignAssembly> | ||
<StrongNameKeyId>MicrosoftShared</StrongNameKeyId> | ||
<!-- ignore warning about BenchmarkDotNet.Extensions being not signed --> | ||
<NoWarn>$(NoWarn);8002</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Helpers.fs" /> | ||
<Compile Include="FSharpCompilerServiceBenchmarks.fs" /> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FSharp.Compiler.Service" Version="37.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\harness\BenchmarkDotNet.Extensions\BenchmarkDotNet.Extensions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
82 changes: 82 additions & 0 deletions
82
src/benchmarks/real-world/FSharp/FSharpCompilerServiceBenchmarks.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace FSharpBenchmarks | ||
|
||
open System | ||
open System.Collections.Immutable | ||
open System.Diagnostics | ||
open System.IO | ||
open System.Threading.Tasks | ||
open BenchmarkDotNet.Attributes | ||
open FSharp.Compiler.SourceCodeServices | ||
open FSharp.Compiler.AbstractIL | ||
|
||
[<RequireQualifiedAccess>] | ||
module Sources = | ||
|
||
let helloWorld = | ||
""" | ||
[<EntryPoint>] | ||
let main _argv = | ||
printfn "Hello World!" | ||
0 | ||
""" | ||
|
||
/// FSharp compiler service benchmarks | ||
[<BenchmarkCategory("FSharp");MemoryDiagnoser>] | ||
type FSharpCompilerServiceBenchmarks () = | ||
|
||
let sourceDir = Path.Combine(Environment.CurrentDirectory, "fsharpSource"); | ||
let tempFsFile = Path.Combine(sourceDir, "temp.fs") | ||
let tempOutputFile = Path.Combine(sourceDir, "temp.dll") | ||
|
||
let defaultReferenceArgs = | ||
Helpers.getFrameworkReferences () | ||
|> Array.map (fun r -> "-r:" + r) | ||
|
||
let compileArgs = | ||
Array.append | ||
[| | ||
"--preferreduilang:en-US" | ||
"--optimize+" | ||
"--langversion:preview" | ||
"-o:" + tempOutputFile | ||
"--nowin32manifest" | ||
"--noframework" | ||
"--simpleresolution" | ||
"--targetprofile:netcore" | ||
"--warn:5" | ||
tempFsFile | ||
|] | ||
defaultReferenceArgs | ||
|
||
let mutable checker = Unchecked.defaultof<FSharpChecker> | ||
|
||
[<GlobalSetup>] | ||
member _.Setup() = | ||
Directory.CreateDirectory(sourceDir) |> ignore | ||
|
||
// Benchmark.NET creates a new process to run the benchmark, so the easiest way | ||
// to communicate information is pass by environment variable | ||
Environment.SetEnvironmentVariable(Helpers.testProjectEnvVarName, sourceDir) | ||
|
||
match box checker with | ||
| null -> | ||
checker <- FSharpChecker.Create(projectCacheSize = 200) | ||
| _ -> | ||
() | ||
|
||
[<IterationSetup(Target = "CompileHelloWorld")>] | ||
member _.CompileHelloWorldSetup() = | ||
File.WriteAllText(tempFsFile, Sources.helloWorld) | ||
|
||
[<Benchmark>] | ||
member _.CompileHelloWorld() = | ||
let errors, _ = checker.Compile(compileArgs) |> Async.RunSynchronously | ||
if errors.Length > 0 then | ||
failwithf "%A" errors | ||
|
||
[<IterationCleanup(Target = "CompileHelloWorld")>] | ||
member _.CompileHelloWorldCleanup() = | ||
checker.InvalidateAll() | ||
checker.ClearLanguageServiceRootCachesAndCollectAndFinalizeAllTransients() | ||
try File.Delete(tempOutputFile) with | _ -> () | ||
try File.Delete(tempFsFile) with | _ -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module FSharpBenchmarks.Helpers | ||
|
||
open System | ||
open System.IO | ||
open System.Diagnostics | ||
|
||
let testProjectEnvVarName = "FSHARP_TEST_PROJECT_DIR" | ||
|
||
let getFrameworkReferences () = | ||
|
||
let programFs = """ | ||
open System | ||
|
||
[<EntryPoint>] | ||
let main argv = 0""" | ||
|
||
let projectFile = """ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5</TargetFramework> | ||
<UseFSharpPreview>true</UseFSharpPreview> | ||
</PropertyGroup> | ||
|
||
<ItemGroup><Compile Include="Program.fs" /></ItemGroup> | ||
|
||
<Target Name="WriteFrameworkReferences" AfterTargets="AfterBuild"> | ||
<WriteLinesToFile File="FrameworkReferences.txt" Lines="@(ReferencePath)" Overwrite="true" WriteOnlyWhenDifferent="true" /> | ||
</Target> | ||
|
||
</Project>""" | ||
|
||
let mutable output = "" | ||
let mutable errors = "" | ||
let mutable cleanUp = true | ||
let projectDirectory = Path.Combine(Path.GetTempPath(), "FSharpBenchmarks", Path.GetRandomFileName()) | ||
try | ||
try | ||
Directory.CreateDirectory(projectDirectory) |> ignore | ||
let projectFileName = Path.Combine(projectDirectory, "ProjectFile.fsproj") | ||
let programFsFileName = Path.Combine(projectDirectory, "Program.fs") | ||
let frameworkReferencesFileName = Path.Combine(projectDirectory, "FrameworkReferences.txt") | ||
File.WriteAllText(projectFileName, projectFile) | ||
File.WriteAllText(programFsFileName, programFs) | ||
|
||
let pInfo = ProcessStartInfo () | ||
pInfo.FileName <- "dotnet" | ||
pInfo.Arguments <- "build" | ||
pInfo.WorkingDirectory <- projectDirectory | ||
pInfo.RedirectStandardOutput <- true | ||
pInfo.RedirectStandardError <- true | ||
pInfo.UseShellExecute <- false | ||
|
||
let p = Process.Start(pInfo) | ||
let timeout = 30000 | ||
let succeeded = p.WaitForExit(timeout) | ||
|
||
output <- p.StandardOutput.ReadToEnd () | ||
errors <- p.StandardError.ReadToEnd () | ||
|
||
if not (String.IsNullOrWhiteSpace errors) then failwithf "%A" errors | ||
if p.ExitCode <> 0 then failwithf "Program exited with exit code %d" p.ExitCode | ||
if not succeeded then failwithf "Program timed out after %d ms" timeout | ||
|
||
File.ReadLines(frameworkReferencesFileName) |> Seq.toArray | ||
with | e -> | ||
cleanUp <- false | ||
printfn "Project directory: %s" projectDirectory | ||
printfn "STDOUT: %s" output | ||
printfn "STDERR: %s" errors | ||
raise (new Exception (sprintf "An error occurred getting framework references: %A" e)) | ||
finally | ||
if cleanUp then | ||
try Directory.Delete(projectDirectory) with | _ -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
open System | ||
open System.Collections.Immutable | ||
open System.Reflection | ||
open System.IO | ||
open System.Threading.Tasks | ||
open BenchmarkDotNet.Running | ||
open BenchmarkDotNet.Jobs | ||
open BenchmarkDotNet.Configs | ||
open BenchmarkDotNet.Extensions | ||
|
||
open FSharpBenchmarks | ||
|
||
let init argv = | ||
async { | ||
let asm = Assembly.GetExecutingAssembly() | ||
return | ||
BenchmarkSwitcher.FromAssembly(asm) | ||
.Run(argv, RecommendedConfig.Create( | ||
artifactsPath = DirectoryInfo(Path.Combine(Path.GetDirectoryName(asm.Location), "BenchmarkDotNet.Artifacts")), | ||
mandatoryCategories = ImmutableHashSet.Create("FSharp"), | ||
job = Job.Default.WithMaxRelativeError(0.01))) | ||
.ToExitCode() | ||
} | ||
|
||
[<EntryPoint>] | ||
let main argv = | ||
init argv | ||
|> Async.RunSynchronously |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this file is actually used - @DrewScoggins can you confirm?