|
| 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 | +} |
0 commit comments