-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added option to report rule summary using -As parameter of Invoke-PSRule #12 - Added performance benchmarks
- Loading branch information
1 parent
dfd53c7
commit b18afbc
Showing
30 changed files
with
709 additions
and
384 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
/**/*.user | ||
/src/**/*-help.xml | ||
/src/**/*.help.txt | ||
/BenchmarkDotNet.Artifacts/ |
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
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
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,11 @@ | ||
# | ||
# A set of benchmark rules for testing PSRule performance | ||
# | ||
|
||
Rule 'BenchmarkOdd' -If { ($TargetObject.Name % 2) -gt 0 } { | ||
Hint 'Odd message' | ||
} | ||
|
||
Rule 'BenchmarkEven' -If { ($TargetObject.Name % 2) -ge 0 } { | ||
Hint 'Even message' | ||
} |
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,45 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<!-- <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netcoreapp2.1|AnyCPU'"> | ||
<Optimize>false</Optimize> | ||
<DefineConstants>TRACE</DefineConstants> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp2.1|AnyCPU'"> | ||
<DefineConstants /> | ||
</PropertyGroup> --> | ||
|
||
<PropertyGroup Condition="'$(Configuration)'=='Release'"> | ||
<DefineConstants>TRACE;BENCHMARK</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.11.3" /> | ||
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net472'"> | ||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.11.3" /> | ||
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'"> | ||
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PSRule\PSRule.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Benchmark.Rule.ps1"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
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,61 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using PSRule.Pipeline; | ||
using PSRule.Rules; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Management.Automation; | ||
using System.Linq; | ||
using BenchmarkDotNet.Engines; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace PSRule.Benchmark | ||
{ | ||
/// <summary> | ||
/// Define a set of benchmarks for performance testing PSRule internals. | ||
/// </summary> | ||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class PSRule | ||
{ | ||
private PSObject[] _TargetObject; | ||
private InvokeRulePipeline _Invoke; | ||
|
||
public sealed class TargetObject | ||
{ | ||
public TargetObject(string name, string message) | ||
{ | ||
Name = name; | ||
Message = message; | ||
} | ||
|
||
public string Name { get; private set; } | ||
|
||
public string Message { get; private set; } | ||
} | ||
|
||
[GlobalSetup] | ||
public void Prepare() | ||
{ | ||
var builder = PipelineBuilder.Invoke(); | ||
|
||
builder.Source(new string[] { Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Benchmark.Rule.ps1") }); | ||
_Invoke = builder.Build(); | ||
|
||
var r = new Random(); | ||
var randomBuffer = new byte[40]; | ||
var targetObjects = new List<PSObject>(); | ||
while (targetObjects.Count < 1000) | ||
{ | ||
r.NextBytes(randomBuffer); | ||
var o = new TargetObject(name: targetObjects.Count.ToString(), message: Convert.ToBase64String(randomBuffer)); | ||
targetObjects.Add(PSObject.AsPSObject(o)); | ||
} | ||
|
||
_TargetObject = targetObjects.ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public void Invoke() => _Invoke.Process(_TargetObject).Consume(new Consumer()); | ||
} | ||
} |
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,87 @@ | ||
using BenchmarkDotNet.Analysers; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Running; | ||
using Microsoft.Extensions.CommandLineUtils; | ||
using PSRule.Pipeline; | ||
using System; | ||
|
||
namespace PSRule.Benchmark | ||
{ | ||
internal sealed class Program | ||
{ | ||
private class BenchmarkConfig : ManualConfig | ||
{ | ||
public BenchmarkConfig(string artifactsPath) | ||
{ | ||
ArtifactsPath = artifactsPath; | ||
} | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
|
||
var app = new CommandLineApplication(); | ||
app.Name = "PSRule Benchmark"; | ||
app.Description = ""; | ||
|
||
#if !BENCHMARK | ||
// Do profiling | ||
DebugProfile(); | ||
#endif | ||
|
||
#if BENCHMARK | ||
RunProfile(app); | ||
app.Execute(args); | ||
#endif | ||
} | ||
|
||
private static void RunProfile(CommandLineApplication app) | ||
{ | ||
var config = ManualConfig.CreateEmpty() | ||
.With(ConsoleLogger.Default) | ||
.With(DefaultColumnProviders.Instance) | ||
.With(EnvironmentAnalyser.Default) | ||
.With(OutliersAnalyser.Default) | ||
.With(MinIterationTimeAnalyser.Default) | ||
.With(MultimodalDistributionAnalyzer.Default) | ||
.With(RuntimeErrorAnalyser.Default) | ||
.With(ZeroMeasurementAnalyser.Default); | ||
|
||
app.Command("benchmark", cmd => | ||
{ | ||
var output = cmd.Option("-o | --output", "The path to store report output.", CommandOptionType.SingleValue); | ||
|
||
cmd.OnExecute(() => | ||
{ | ||
if (output.HasValue()) | ||
{ | ||
config.WithArtifactsPath(output.Value()); | ||
} | ||
|
||
// Do benchmarks | ||
var summary = BenchmarkRunner.Run<PSRule>(config); | ||
|
||
return 0; | ||
}); | ||
|
||
cmd.HelpOption("-? | -h | --help"); | ||
}); | ||
|
||
app.HelpOption("-? | -h | --help"); | ||
} | ||
|
||
private static void DebugProfile() | ||
{ | ||
var profile = new PSRule(); | ||
profile.Prepare(); | ||
|
||
for (var i = 0; i < 1000; i++) | ||
{ | ||
profile.Invoke(); | ||
} | ||
} | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"profiles": { | ||
"PSRule.Benchmark": { | ||
"commandName": "Project", | ||
"commandLineArgs": "benchmark" | ||
} | ||
} | ||
} |
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
Oops, something went wrong.