Skip to content

Commit

Permalink
update benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertk committed Aug 4, 2024
1 parent e8175eb commit b90572a
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 41 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="BenchmarkDotNet.TestAdapter" Version="0.13.12" />
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.12"/>
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.10.4" />
Expand Down
68 changes: 68 additions & 0 deletions test/coverlet.core.benchmark.tests/CoverageBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using Coverlet.Core;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Helpers;
using Coverlet.Core.Symbols;
using Moq;

namespace coverlet.core.benchmark.tests
{
[MemoryDiagnoser]
public class CoverageBenchmarks
{
private Coverage _coverage;
private readonly Mock<ILogger> _mockLogger = new();
private DirectoryInfo _directory;

[GlobalSetup(Target = nameof(GetCoverageBenchmark))]
public void GetCoverageBenchmarkSetup()
{
string module = GetType().Assembly.Location;
string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");

_directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

File.Copy(module, Path.Combine(_directory.FullName, Path.GetFileName(module)), true);
File.Copy(pdb, Path.Combine(_directory.FullName, Path.GetFileName(pdb)), true);

// TODO: Find a way to mimick hits
var instrumentationHelper =
new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), new FileSystem(), new Mock<ILogger>().Object,
new SourceRootTranslator(module, new Mock<ILogger>().Object, new FileSystem(), new AssemblyAdapter()));

var parameters = new CoverageParameters
{
IncludeFilters = new string[] { "[coverlet.tests.projectsample.excludedbyattribute*]*" },
IncludeDirectories = Array.Empty<string>(),
ExcludeFilters = Array.Empty<string>(),
ExcludedSourceFiles = Array.Empty<string>(),
ExcludeAttributes = Array.Empty<string>(),
IncludeTestAssembly = false,
SingleHit = false,
MergeWith = string.Empty,
UseSourceLink = false
};

_coverage = new Coverage(Path.Combine(_directory.FullName, Path.GetFileName(module)), parameters, _mockLogger.Object, instrumentationHelper, new FileSystem(), new SourceRootTranslator(_mockLogger.Object, new FileSystem()), new CecilSymbolHelper());
_coverage.PrepareModules();

}

[GlobalCleanup]
public void IterationCleanup()
{
_directory.Delete(true);
}

[Benchmark]
public void GetCoverageBenchmark()
{
CoverageResult result = _coverage.GetCoverageResult();
}
}
}
94 changes: 94 additions & 0 deletions test/coverlet.core.benchmark.tests/InstrumenterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Coverlet.Core.Abstractions;
using Coverlet.Core.Helpers;
using Coverlet.Core.Symbols;

using BenchmarkDotNet.Attributes;
using Coverlet.Core.Instrumentation;
using Coverlet.Core;
using System.IO;
using Moq;

namespace coverlet.core.benchmark.tests
{
[MemoryDiagnoser]
public class InstrumenterBenchmarks
{
Mock<ILogger> _mockLogger;
Mock<FileSystem> _partialMockFileSystem;
readonly string[] _files = new[]
{
"System.Private.CoreLib.dll",
"System.Private.CoreLib.pdb"
};
Instrumenter _instrumenter;
DirectoryInfo _directory;
SourceRootTranslator _sourceRootTranslator;
CoverageParameters _parameters;
InstrumentationHelper _instrumentationHelper;

//[GlobalSetup(Target = nameof(InstrumenterBenchmarks))]
//public void InstrumenterBenchmarkSetup()
//{

//}

[GlobalCleanup]
public void IterationCleanup()
{
_directory.Delete(true);
}

[Benchmark]
public void InstrumenterBenchmark()
{
_mockLogger = new Mock<ILogger>();
_directory = Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), nameof(InstrumenterBenchmark)));

foreach (string file in _files)
{
File.Copy(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", file), Path.Combine(_directory.FullName, file), overwrite: true);
}

_partialMockFileSystem = new Mock<FileSystem>();
_partialMockFileSystem.CallBase = true;
_partialMockFileSystem.Setup(fs => fs.OpenRead(It.IsAny<string>())).Returns((string path) =>
{
if (Path.GetFileName(path.Replace(@"\", @"/")) == _files[1])
{
return File.OpenRead(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), _files[1]));
}
else
{
return File.OpenRead(path);
}
});
_partialMockFileSystem.Setup(fs => fs.Exists(It.IsAny<string>())).Returns((string path) =>
{
if (Path.GetFileName(path.Replace(@"\", @"/")) == _files[1])
{
return File.Exists(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), _files[1]));
}
else
{
if (path.Contains(@":\git\runtime"))
{
return true;
}
else
{
return File.Exists(path);
}
}
});
_sourceRootTranslator = new SourceRootTranslator(_mockLogger.Object, new FileSystem());
_parameters = new CoverageParameters();
_instrumentationHelper =
new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), _partialMockFileSystem.Object, _mockLogger.Object, _sourceRootTranslator);
_instrumenter = new Instrumenter(Path.Combine(_directory.FullName, _files[0]), "_coverlet_instrumented", _parameters, _mockLogger.Object, _instrumentationHelper, _partialMockFileSystem.Object, _sourceRootTranslator, new CecilSymbolHelper());

// implement your benchmark here
InstrumenterResult result = _instrumenter.Instrument();
}

}
}
25 changes: 16 additions & 9 deletions test/coverlet.core.benchmark.tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@

namespace coverlet.core.benchmark.tests
{
public class Program
public class Program
{

public static void Main(string[] args)
{
public static void Main(string[] args)
{
var config = DefaultConfig.Instance


var config = DefaultConfig.Instance
.WithOptions(ConfigOptions.JoinSummary)
.AddJob(Job
.MediumRun
.ShortRun
.WithLaunchCount(1)
.WithToolchain(InProcessNoEmitToolchain.Instance));
var summary = BenchmarkRunner.Run<CoreBenchmarks>(config, args);
var summary = BenchmarkRunner.Run(new[]{
BenchmarkConverter.TypeToBenchmarks( typeof(CoverageBenchmarks), config),
BenchmarkConverter.TypeToBenchmarks( typeof(InstrumenterBenchmarks), config)
});

// Use this to select benchmarks from the console:
// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
// Use this to select benchmarks from the console:
//var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CS0162</NoWarn>
<!-- Disable entry point generation as this project has it's own entry point -->
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" />
<PackageReference Include="BenchmarkDotNet.TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\coverlet.core\coverlet.core.csproj" />
<ProjectReference Include="$(RepoRoot)test\coverlet.tests.utils\coverlet.tests.utils.csproj" />
<ProjectReference Include="$(RepoRoot)test\coverlet.tests.xunit.extensions\coverlet.tests.xunit.extensions.csproj" />

<ProjectReference Include="$(RepoRoot)test\coverlet.tests.projectsample.excludedbyattribute\coverlet.tests.projectsample.excludedbyattribute.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="TestAssets\System.Private.CoreLib.dll">
Expand Down
2 changes: 1 addition & 1 deletion test/coverlet.core.tests/Coverage/InstrumenterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void Retry(Action action, Func<TimeSpan> backoffStrategy, int maxAttemptC
}
}

// We log to files for debugging pourpose, we can check if instrumentation is ok
// We log to files for debugging purpose, we can check if instrumentation is ok
class Logger : ILogger
{
readonly string _logFile;
Expand Down
30 changes: 16 additions & 14 deletions test/coverlet.msbuild.benchmark.tests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using BenchmarkDotNet.Configs;
//using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;

namespace coverlet.msbuild.benchmark.tests
{
public class Program
public class Program
{
public static void Main(string[] args)
{
public static void Main(string[] args)
{
var config = DefaultConfig.Instance
.AddJob(Job.MediumRun
.WithLaunchCount(1)
.WithToolchain(InProcessNoEmitToolchain.Instance
)
);
var summary = BenchmarkRunner.Run<MSBuildTaskBenchmarks>(config, args);
var config = DefaultConfig.Instance
.WithOptions(ConfigOptions.JoinSummary)
.AddJob(Job
.ShortRun
.WithLaunchCount(1)
.WithToolchain(InProcessNoEmitToolchain.Instance));
var summary = BenchmarkRunner.Run(new[]{
BenchmarkConverter.TypeToBenchmarks( typeof(CoverageResultTaskBenchmarks), config),
//BenchmarkConverter.TypeToBenchmarks( typeof(InstrumentationTaskBenchmarks), config)
});

// Use this to select benchmarks from the console:
//var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
// Use this to select benchmarks from the console:
//var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public MSBuildFixture()
}

[MemoryDiagnoser]
public class MSBuildTaskBenchmarks : IAssemblyFixture<MSBuildFixture>
public class CoverageResultTaskBenchmarks : IAssemblyFixture<MSBuildFixture>
{
private readonly Mock<IBuildEngine> _buildEngine;
CoverageResultTask? _coverageResultTask;
private readonly List<BuildErrorEventArgs> _errors;
private readonly Mock<IAssemblyAdapter> _mockAssemblyAdapter;

public MSBuildTaskBenchmarks()
public CoverageResultTaskBenchmarks()
{
_buildEngine = new Mock<IBuildEngine>();
_errors = new List<BuildErrorEventArgs>();
_mockAssemblyAdapter = new Mock<IAssemblyAdapter>();
_mockAssemblyAdapter.Setup(x => x.GetAssemblyName(It.IsAny<string>())).Returns("abc");
}

[GlobalSetup(Target = nameof(CoverageResultTaskBenchmarkSingle))]
[GlobalSetup(Target = nameof(CoverageResultTaskBenchmark))]
public void CoverageResultTaskSingleSetup()
{
var mockFileSystem = new Mock<IFileSystem>();
Expand Down Expand Up @@ -93,7 +93,7 @@ public void CoverageResultTaskSingleSetup()
//}

[Benchmark]
public void CoverageResultTaskBenchmarkSingle()
public void CoverageResultTaskBenchmark()
{
bool success = _coverageResultTask!.Execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public void PreprocessorBenchmarkInitialTargetsOuterAndInnerSetup()
[Benchmark]
public void InstrumentationTaskBenchmarkSingle()
{
//instrumentationTask.SaveLogicalProject(new StringWriter());
_instrumentationTask!.Execute();
}

[Benchmark]
public void InstrumentationTaskBenchmarkInitialTargetsOuterAndInner()
{
//instrumentationTask.SaveLogicalProject(new StringWriter());
_instrumentationTask!.Execute();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyOriginatorKeyFile>coverlet.msbuild.benchmark.tests.snk</AssemblyOriginatorKeyFile>
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
<!-- Disable entry point generation as this project has it's own entry point -->
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="BenchmarkDotNet.TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq"/>
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.assemblyfixture" />
<PackageReference Include="Microsoft.Build.Framework" />
<PackageReference Include="Microsoft.Build.Utilities.Core" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<!-- <ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Build.Framework" VersionOverride="17.3.2" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" VersionOverride="17.3.2" ExcludeAssets="runtime" />
</ItemGroup>
</ItemGroup>-->

<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\coverlet.msbuild.tasks\coverlet.msbuild.tasks.csproj" />
Expand Down

0 comments on commit b90572a

Please sign in to comment.