Skip to content

Commit 5b00cd5

Browse files
committed
add InstrumentationHelperBenchmarks
1 parent b8359e8 commit 5b00cd5

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
2121
<PackageVersion Include="BenchmarkDotNet.TestAdapter" Version="0.14.0" />
2222
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.14.0" />
23+
<!--<PackageVersion Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="17.13.35606.1" />-->
2324
<PackageVersion Include="DotNetConfig" Version="1.2.0" />
2425
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
2526
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildVersion)" />
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Toni Solarin-Sodara
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.IO;
6+
using BenchmarkDotNet.Attributes;
7+
using Coverlet.Core.Abstractions;
8+
using Coverlet.Core.Helpers;
9+
using Moq;
10+
11+
namespace coverlet.core.benchmark.tests
12+
{
13+
public class InstrumentationHelperBenchmarks
14+
{
15+
private Mock<IFileSystem> _mockFileSystem;
16+
private Mock<ILogger> _mockLogger;
17+
private Mock<IRetryHelper> _mockRetryHelper;
18+
private Mock<ISourceRootTranslator> _mockSourceRootTranslator;
19+
20+
private string _testModulePath;
21+
private string[] _testDirectories;
22+
23+
[GlobalSetup]
24+
public void Setup()
25+
{
26+
_mockFileSystem = new Mock<IFileSystem>();
27+
_mockLogger = new Mock<ILogger>();
28+
_mockRetryHelper = new Mock<IRetryHelper>();
29+
_mockSourceRootTranslator = new Mock<ISourceRootTranslator>();
30+
31+
// Setup test data
32+
_testModulePath = Path.Combine(Directory.GetCurrentDirectory(), "coverlet.testsubject.dll");
33+
_testDirectories = new[] { Directory.GetCurrentDirectory() };
34+
35+
// Mock file system behavior
36+
_mockFileSystem.Setup(fs => fs.Exists(It.IsAny<string>())).Returns(true);
37+
_mockFileSystem.Setup(fs => fs.OpenRead(It.IsAny<string>())).Returns(() => new MemoryStream());
38+
}
39+
40+
[Benchmark]
41+
public void Benchmark_GetCoverableModules()
42+
{
43+
string module = Directory.GetFiles(Directory.GetCurrentDirectory(), "coverlet.testsubject.dll")[0];
44+
string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");
45+
46+
InstrumentationHelper instrumentationHelper = new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), new FileSystem(), new Mock<ILogger>().Object,
47+
new SourceRootTranslator(module, new Mock<ILogger>().Object, new FileSystem(), new AssemblyAdapter()));
48+
instrumentationHelper.GetCoverableModules(_testModulePath, _testDirectories, includeTestAssembly: true);
49+
}
50+
51+
[Benchmark]
52+
public void Benchmark_HasPdb()
53+
{
54+
string module = Directory.GetFiles(Directory.GetCurrentDirectory(), "coverlet.testsubject.dll")[0];
55+
string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");
56+
57+
InstrumentationHelper instrumentationHelper = new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), new FileSystem(), new Mock<ILogger>().Object,
58+
new SourceRootTranslator(module, new Mock<ILogger>().Object, new FileSystem(), new AssemblyAdapter()));
59+
60+
instrumentationHelper.HasPdb(_testModulePath, out _);
61+
}
62+
63+
[Benchmark]
64+
public void Benchmark_BackupOriginalModule()
65+
{
66+
string module = Directory.GetFiles(Directory.GetCurrentDirectory(), "coverlet.testsubject.dll")[0];
67+
string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");
68+
DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
69+
File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true);
70+
File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);
71+
72+
InstrumentationHelper instrumentationHelper = new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), new FileSystem(), new Mock<ILogger>().Object,
73+
new SourceRootTranslator(module, new Mock<ILogger>().Object, new FileSystem(), new AssemblyAdapter()));
74+
75+
instrumentationHelper.BackupOriginalModule(Path.Combine(directory.FullName, Path.GetFileName(module)), "_coverlet.testsubject");
76+
}
77+
}
78+
}

test/coverlet.core.benchmark.tests/Program.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// Copyright (c) Toni Solarin-Sodara
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
//using System.Diagnostics.Tracing;
45
using BenchmarkDotNet.Configs;
56
using BenchmarkDotNet.Diagnosers;
7+
//using BenchmarkDotNet.Diagnostics.Windows;
68
using BenchmarkDotNet.Exporters;
79
using BenchmarkDotNet.Exporters.Csv;
810
using BenchmarkDotNet.Exporters.Json;
911
using BenchmarkDotNet.Exporters.Xml;
1012
using BenchmarkDotNet.Jobs;
1113
using BenchmarkDotNet.Running;
1214
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;
15+
//using Microsoft.Diagnostics.NETCore.Client;
16+
//using Microsoft.Diagnostics.Tracing.Parsers;
1317

1418
namespace coverlet.core.benchmark.tests
1519
{
@@ -18,25 +22,21 @@ public class Program
1822

1923
public static void Main(string[] args)
2024
{
21-
2225
var config = DefaultConfig.Instance
2326
.WithOptions(ConfigOptions.JoinSummary)
2427
.AddJob(Job
2528
.ShortRun
2629
.WithLaunchCount(1)
2730
.WithToolchain(InProcessNoEmitToolchain.Instance))
28-
.AddExporter(CsvExporter.Default)
29-
.AddExporter(CsvMeasurementsExporter.Default)
30-
.AddExporter(RPlotExporter.Default)
31-
.AddExporter(HtmlExporter.Default)
32-
.AddExporter(MarkdownExporter.GitHub)
33-
.AddExporter(JsonExporter.Default)
34-
.AddExporter(XmlExporter.Default)
35-
.AddDiagnoser(MemoryDiagnoser.Default)
31+
.AddExporter(CsvExporter.Default, CsvMeasurementsExporter.Default, RPlotExporter.Default, HtmlExporter.Default, JsonExporter.Default, MarkdownExporter.GitHub, XmlExporter.Default)
32+
.AddDiagnoser(MemoryDiagnoser.Default, ThreadingDiagnoser.Default, ExceptionDiagnoser.Default)
33+
//.AddDiagnoser(new InliningDiagnoser(), new EtwProfiler()) // only windows platform, requires elevated privileges
34+
//.AddDiagnoser(new EventPipeProfiler(EventPipeProfile.CpuSampling)) // stops collecting results ???
3635
;
3736
var summary = BenchmarkRunner.Run(new[]{
3837
BenchmarkConverter.TypeToBenchmarks( typeof(CoverageBenchmarks), config),
39-
BenchmarkConverter.TypeToBenchmarks( typeof(InstrumenterBenchmarks), config)
38+
BenchmarkConverter.TypeToBenchmarks( typeof(InstrumenterBenchmarks), config),
39+
BenchmarkConverter.TypeToBenchmarks( typeof(InstrumentationHelperBenchmarks), config),
4040
});
4141

4242
// Use this to select benchmarks from the console and execute with additional options e.g. 'coverlet.core.benchmark.tests.exe --profiler EP'

test/coverlet.core.benchmark.tests/coverlet.core.benchmark.tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<OutputType>Exe</OutputType>
5-
</PropertyGroup>
6-
<PropertyGroup>
75
<PlatformTarget>AnyCPU</PlatformTarget>
86
<DebugType>pdbonly</DebugType>
97
<DebugSymbols>true</DebugSymbols>
@@ -18,6 +16,7 @@
1816

1917
<ItemGroup>
2018
<PackageReference Include="BenchmarkDotNet" />
19+
<PackageReference Include="BenchmarkDotNet.TestAdapter" />
2120
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" />
2221
<!--<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" />-->
2322
<PackageReference Include="Microsoft.NET.Test.Sdk" />
@@ -27,6 +26,7 @@
2726
<ProjectReference Include="$(RepoRoot)src\coverlet.core\coverlet.core.csproj" />
2827
<ProjectReference Include="$(RepoRoot)test\coverlet.tests.utils\coverlet.tests.utils.csproj" />
2928
<ProjectReference Include="$(RepoRoot)test\coverlet.tests.projectsample.excludedbyattribute\coverlet.tests.projectsample.excludedbyattribute.csproj" />
29+
<ProjectReference Include="$(RepoRoot)test\coverlet.testsubject\coverlet.testsubject.csproj" />
3030
</ItemGroup>
3131

3232
<ItemGroup>

0 commit comments

Comments
 (0)