Skip to content

Add benchmark tests for v3 (and v2) performance #690

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
merged 17 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/code/PowerShellGet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Benchmarks" />
</ItemGroup>

<Target Name="CoreTypes" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'NuGet.Protocol.Core.Types'">
Expand Down
70 changes: 70 additions & 0 deletions test/perf/benchmarks/BenchmarksV2LocalRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using Microsoft.PowerShell;
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Benchmarks
{
public class BenchmarksV2LocalRepo
{
System.Management.Automation.PowerShell pwsh;

[GlobalSetup]
public void GlobalSetup()
{
// Setting up the PowerShell runspace
var defaultSS = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted;
pwsh = System.Management.Automation.PowerShell.Create(defaultSS);

// Using PSGet v3 in order to save the Az modules and its dependencies
pwsh.AddScript("Import-Module PowerShellGet -RequiredVersion 3.0.14 -Force");
pwsh.AddScript("New-Item TestRepo -ItemType Directory");
pwsh.AddScript("Save-PSResource -Name Az -Repository PSGallery -AsNupkg -TrustRepository -Path .\\TestRepo");

// Now import the PSGet module version we want to test and register a local repo
pwsh.AddScript("Import-Module PowerShellGet -RequiredVersion 2.2.5 -Force");
pwsh.AddScript("Register-PSRepository -Name LocalRepo -SourceLocation .\\TestRepo");

pwsh.Invoke();
}

[GlobalCleanup]
public void GlobalCleanup()
{
pwsh.Dispose();
}

[Benchmark]
public void FindAzModuleV2()

{
pwsh.Commands.Clear();
pwsh.AddScript("Find-Module -Name Az -Repository LocalRepo");
pwsh.Invoke();
}

[Benchmark]
public void FindAzModuleAndDependenciesV2()

{
pwsh.Commands.Clear();
pwsh.AddScript("Find-Module -Name Az -IncludeDependencies -Repository LocalRepo");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleAndDependenciesV2()

{
pwsh.Commands.Clear();
pwsh.AddScript("Install-Module -Name Az -Repository LocalRepo -Force");
pwsh.Invoke();
}
}
}
60 changes: 60 additions & 0 deletions test/perf/benchmarks/BenchmarksV2RemoteRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using Microsoft.PowerShell;
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Benchmarks
{
public class BenchmarksV2RemoteRepo
{
System.Management.Automation.PowerShell pwsh;

[GlobalSetup]
public void GlobalSetup()
{
// Setting up the PowerShell runspace
var defaultSS = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted;
pwsh = System.Management.Automation.PowerShell.Create(defaultSS);

// Import the PSGet version we want to test
pwsh.AddScript("Import-Module PowerShellGet -RequiredVersion 2.2.5 -Force");
pwsh.Invoke();
}

[GloblaCleanup]
public void IterationCleanup()
{
pwsh.Dispose();
}

[Benchmark]
public void FindAzModuleV2()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-Module -Name Az -Repository PSGallery");
pwsh.Invoke();
}

[Benchmark]
public void FindAzModuleAndDependenciesV2()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-Module -Name Az -IncludeDependencies -Repository PSGallery");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleAndDependenciesV2()
{
pwsh.Commands.Clear();
pwsh.AddScript("Install-Module -Name Az -Repository PSGallery -Force");
pwsh.Invoke();
}
}
}
72 changes: 72 additions & 0 deletions test/perf/benchmarks/BenchmarksV3LocalRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using Microsoft.PowerShell;
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Benchmarks
{
public class BenchmarksV3LocalRepo
{
System.Management.Automation.PowerShell pwsh;

[GlobalSetup]
public void GlobalSetup()
{
// Setting up the PowerShell runspace
var defaultSS = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted;
pwsh = System.Management.Automation.PowerShell.Create(defaultSS);

// Import the PSGet module version we want to test, register a local repo, and save the Az modules and its dependencies
pwsh.AddScript("Import-Module PowerShellGet -RequiredVersion 3.0.14 -Force");
pwsh.AddScript("New-Item TestRepo -ItemType Directory");
pwsh.AddScript("Register-PSResourceRepository -Name LocalRepo -Uri .\\TestRepo");
pwsh.AddScript("Save-PSResource -Name Az -Repository PSGallery -AsNupkg -TrustRepository -Path .\\TestRepo");

pwsh.Invoke();
}

[GlobalCleanup]
public void GlobalCleanup()
{
pwsh.Dispose();
}

[Benchmark]
public void FindAzModuleV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-PSResource -Name Az -Repository LocalRepo");
pwsh.Invoke();
}

[Benchmark]
public void FindAzModuleAndDependenciesV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-PSResource -Name Az -IncludeDependencies -Repository LocalRepo");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Install-PSResource -Name Az -Repository LocalRepo -TrustRepository -SkipDependencyCheck -Reinstall");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleAndDependenciesV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Install-PSResource -Name Az -Repository LocalRepo -TrustRepository -Reinstall");
pwsh.Invoke();
}
}
}
68 changes: 68 additions & 0 deletions test/perf/benchmarks/BenchmarksV3RemoteRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using Microsoft.PowerShell;
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Benchmarks
{
public class BenchmarksV3RemoteRepo
{
System.Management.Automation.PowerShell pwsh;

[GlobalSetup]
public void GlobalSetup()
{
// Setting up the PowerShell runspace
var defaultSS = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted;
pwsh = System.Management.Automation.PowerShell.Create(defaultSS);

// Import the PSGet version we want to test
pwsh.AddScript("Import-Module PowerShellGet -RequiredVersion 3.0.14 -Force");
pwsh.Invoke();
}

[GlobalCleanup]
public void GlobalCleanup()
{
pwsh.Dispose();
}

[Benchmark]
public void FindAzModuleV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-PSResource -Name Az -Repository PSGallery");
pwsh.Invoke();
}

[Benchmark]
public void FindAzModuleAndDependenciesV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Find-PSResource -Name Az -IncludeDependencies -Repository PSGallery");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Install-PSResource -Name Az -Repository PSGallery -TrustRepository -SkipDependencyCheck -Reinstall");
pwsh.Invoke();
}

[Benchmark]
public void InstallAzModuleAndDependenciesV3()
{
pwsh.Commands.Clear();
pwsh.AddScript("Install-PSResource -Name Az -Repository PSGallery -TrustRepository -Reinstall");
pwsh.Invoke();
}
}
}
18 changes: 18 additions & 0 deletions test/perf/benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using BenchmarkDotNet.Running;

namespace Benchmarks
{
public sealed class Program
{
public static void Main(string[] args)
{
var summaryV2Remote = BenchmarkRunner.Run<BenchmarksV2RemoteRepo>();
var summaryV3Remote = BenchmarkRunner.Run<BenchmarksV3RemoteRepo>();
var summaryV2Local = BenchmarkRunner.Run<BenchmarksV2LocalRepo>();
var summaryV3Local = BenchmarkRunner.Run<BenchmarksV3LocalRepo>();
}
}
}
32 changes: 32 additions & 0 deletions test/perf/benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Micro Benchmarks

This folder contains micro benchmarks that test the performance of PowerShellGet.

### Quick Start

You can run the benchmarks directly using `dotnet run` in this directory:
1. To run the benchmarks in Interactive Mode, where you will be asked which benchmark(s) to run:
```
dotnet run -c Release -f net6.0
```

2. To list all available benchmarks ([read more](https://github.com/dotnet/performance/blob/main/docs/benchmarkdotnet.md#Listing-the-Benchmarks)):
```
dotnet run -c Release -f net6.0 --list [flat/tree]
```

3. To filter the benchmarks using a glob pattern applied to `namespace.typeName.methodName` ([read more](https://github.com/dotnet/performance/blob/main/docs/benchmarkdotnet.md#Filtering-the-Benchmarks)]):
```
dotnet run -c Release -f net6.0 --filter *script* --list flat
```

4. To profile the benchmarked code and produce an ETW Trace file ([read more](https://github.com/dotnet/performance/blob/main/docs/benchmarkdotnet.md#Profiling))
```
dotnet run -c Release -f net6.0 --filter *script* --profiler ETW
```

## References

- [Getting started with BenchmarkDotNet](https://benchmarkdotnet.org/articles/guides/getting-started.html)
- [Micro-benchmark Design Guidelines](https://github.com/dotnet/performance/blob/main/docs/microbenchmark-design-guidelines.md)
- [Adam SITNIK: Powerful benchmarking in .NET](https://www.youtube.com/watch?v=pdcrSG4tOLI&t=351s)
25 changes: 25 additions & 0 deletions test/perf/benchmarks/benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../../src/code/PowerShellGet.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1"/>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.5" />
</ItemGroup>
</Project>