Repository navigation
Add benchmark to test compiler performance #16083
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
Merged
Aditya Patwardhan (adityapatwardhan)
merged 8 commits into
PowerShell:master
from
daxian-dbw:benchmark
Sep 14, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d2a7e01
Add benchmark to test compiler performance
daxian-dbw 1f3fd10
Remove unneeded code
daxian-dbw 7e2f448
A minor fix
daxian-dbw 97962cc
Update comment
daxian-dbw 9cffce7
Remove unused using directive
daxian-dbw fe821dc
Update the BenchmarkDotNet package version
daxian-dbw 0bd0d3e
Compile individual methods instead of the whole file
daxian-dbw 598f06e
Address Aditya's comments
daxian-dbw 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 hidden or 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,78 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #if NET6_0 | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Management.Automation; | ||
| using System.Management.Automation.Language; | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using MicroBenchmarks; | ||
|
|
||
| namespace Engine | ||
| { | ||
| [BenchmarkCategory(Categories.Engine, Categories.Internal)] | ||
| public class Compiler | ||
| { | ||
| private static readonly Dictionary<string, ScriptBlockAst> s_scriptBlocksDict; | ||
| private static readonly List<string> s_functionNames; | ||
| private ScriptBlockAst _currentAst; | ||
|
|
||
| static Compiler() | ||
| { | ||
| string pattern = string.Format("{0}test{0}perf{0}benchmarks", Path.DirectorySeparatorChar); | ||
| string location = typeof(Compiler).Assembly.Location; | ||
| string testFilePath = null; | ||
|
|
||
| int start = location.IndexOf(pattern, StringComparison.Ordinal); | ||
| if (start > 0) | ||
| { | ||
| testFilePath = Path.Join(location.AsSpan(0, start + pattern.Length), "assets", "compiler.test.ps1"); | ||
| } | ||
|
|
||
| var topScriptBlockAst = Parser.ParseFile(testFilePath, tokens: out _, errors: out _); | ||
| var allFunctions = topScriptBlockAst.FindAll(ast => ast is FunctionDefinitionAst, searchNestedScriptBlocks: false); | ||
|
|
||
| s_scriptBlocksDict = new Dictionary<string, ScriptBlockAst>(capacity: 16); | ||
| s_functionNames = new List<string>(capacity: 16); | ||
|
|
||
| foreach (FunctionDefinitionAst function in allFunctions) | ||
| { | ||
| s_functionNames.Add(function.Name); | ||
| s_scriptBlocksDict.Add(function.Name, function.Body); | ||
| } | ||
| } | ||
|
|
||
| [ParamsSource(nameof(FunctionName))] | ||
| public string FunctionsToCompile { get; set; } | ||
|
|
||
| public IEnumerable<string> FunctionName() => s_functionNames; | ||
|
|
||
| [GlobalSetup(Target = nameof(CompileFunction))] | ||
| public void GlobalSetup() | ||
| { | ||
| _currentAst = s_scriptBlocksDict[FunctionsToCompile]; | ||
|
|
||
| // Run it once to get the C# code jitted. | ||
| // The first call to this takes relatively too long, which makes the BDN's heuristic incorrectly | ||
| // believe that there is no need to run many ops in each interation. However, the subsequent runs | ||
| // of this method is much faster than the first run, and this causes 'MinIterationTime' warnings | ||
| // to our benchmarks and make the benchmark results not reliable. | ||
| // Calling this method once in 'GlobalSetup' is a workaround. | ||
| // See https://github.com/dotnet/BenchmarkDotNet/issues/837#issuecomment-828600157 | ||
| CompileFunction(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public bool CompileFunction() | ||
| { | ||
| var compiledData = new CompiledScriptBlockData(_currentAst, isFilter: false); | ||
| return compiledData.Compile(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Why just NET6, it would be good if we can compile and run against NET to compare perf between 7.1 and 7.2 right?
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.
The test uses the internal API directly. 7.1 and 7.2 doesn't have the internal visible attribute defined for powershell-perf.dll