-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from iron-software/use-benchmarkdotnet
Use BenchmarkDotNet
- Loading branch information
Showing
67 changed files
with
2,178 additions
and
1,617 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
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 |
---|---|---|
@@ -1,50 +1,189 @@ | ||
using IronBenchmarks.IronBarCode; | ||
using IronBenchmarks.IronPdf; | ||
using IronBenchmarks.IronXL; | ||
using IronBenchmarks.Reporting; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Validators; | ||
using IronBenchmarks.App.Configuration; | ||
using IronBenchmarks.BarCodeLibs.Benchmarks; | ||
using IronBenchmarks.ExcelLibs.Benchmarks; | ||
using IronBenchmarks.PdfLibs.Benchmarks; | ||
using IronBenchmarks.Reporting; | ||
using IronBenchmarks.Reporting.Configuration; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using IronBenchmarks.Reporting.Configuration; | ||
|
||
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; | ||
var host = SetupApplication(); | ||
|
||
var environment = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT"); | ||
var builder = new ConfigurationBuilder() | ||
.AddJsonFile($"appsettings.json", true, true) | ||
.AddJsonFile($"appsettings.{environment}.json", true, true) | ||
.AddUserSecrets(Assembly.GetExecutingAssembly(), true) | ||
.AddEnvironmentVariables(); | ||
ApplyIronXLLicenseKey(host); | ||
|
||
var configurationRoot = builder.Build(); | ||
var reportConfig = GetReportConfig(args, host); | ||
var reportGenerator = new ReportGenerator(reportConfig); | ||
|
||
var host = Host.CreateDefaultBuilder() | ||
.ConfigureServices((context, services) => | ||
RunExcelBenchmarks(args, reportConfig, reportGenerator); | ||
RunPdfBenchmarks(args, reportConfig, reportGenerator); | ||
RunBarCodeBenchmarks(args, reportConfig, reportGenerator); | ||
|
||
Console.ReadKey(); | ||
|
||
//############################################################################# | ||
|
||
static void RunBarCodeBenchmarks( | ||
string[] args, | ||
IReportingConfig reportConfig, | ||
ReportGenerator reportGenerator) | ||
{ | ||
if (args.Contains("-bc")) | ||
{ | ||
services.AddSingleton<IAppConfig, AppConfig>( | ||
_ => configurationRoot.GetSection(nameof(AppConfig)).Get<AppConfig>()); | ||
services.AddSingleton<IReportingConfig, ReportingConfig>( | ||
_ => configurationRoot.GetSection(nameof(ReportingConfig)).Get<ReportingConfig>()); | ||
}) | ||
.UseSerilog() | ||
.Build(); | ||
|
||
var appConfig = ActivatorUtilities.GetServiceOrCreateInstance<IAppConfig>(host.Services); | ||
IronXL.License.LicenseKey = appConfig.LicenseKeyIronXl; | ||
IronBarCode.License.LicenseKey = appConfig.LicenseKeyIronBarCode; | ||
IronPdf.License.LicenseKey = appConfig.LicenseKeyIronPdf; | ||
|
||
var reportConfig = ActivatorUtilities.GetServiceOrCreateInstance<IReportingConfig>(host.Services); | ||
var reportGenerator = new ReportGenerator(reportConfig); | ||
reportConfig.ReportsFolder += "\\BarCode"; | ||
|
||
var barcodeSummaries = new List<Summary> | ||
{ | ||
BenchmarkRunner.Run<CreateBarcodeBenchmark>() | ||
}; | ||
|
||
reportGenerator.GenerateReport(barcodeSummaries, "BarCodeLibs"); | ||
} | ||
} | ||
|
||
static void RunPdfBenchmarks( | ||
string[] args, | ||
IReportingConfig reportConfig, | ||
ReportGenerator reportGenerator) | ||
{ | ||
if (args.Contains("-pdf")) | ||
{ | ||
reportConfig.ReportsFolder += "\\PDF"; | ||
|
||
var config = new ManualConfig() | ||
.WithOptions(ConfigOptions.DisableOptimizationsValidator) | ||
.AddValidator(JitOptimizationsValidator.DontFailOnError) | ||
.AddLogger(ConsoleLogger.Default) | ||
.AddColumnProvider(DefaultColumnProviders.Instance); | ||
|
||
var pdfSummaries = new List<Summary> | ||
{ | ||
BenchmarkRunner.Run<RenderHtmlToPdfBenchmark>(config), | ||
BenchmarkRunner.Run<LoadingLargeFileBenchmark>(config), | ||
BenchmarkRunner.Run<SavingLargeFileBenchmark>(config), | ||
}; | ||
|
||
reportGenerator.GenerateReport(pdfSummaries, "PDFLibs"); | ||
} | ||
} | ||
|
||
static void RunExcelBenchmarks( | ||
string[] args, | ||
IReportingConfig reportConfig, | ||
ReportGenerator reportGenerator) | ||
{ | ||
if (args.Contains("-xl")) | ||
{ | ||
reportConfig.ReportsFolder += "\\Excel"; | ||
|
||
var libsWithVersions = GetLibNamesWithVersions(typeof(RandomCellsBenchmark)); | ||
|
||
var excelSummaries = new List<Summary> | ||
{ | ||
BenchmarkRunner.Run<RandomCellsBenchmark>(), | ||
BenchmarkRunner.Run<DateCellBenchmark>(), | ||
BenchmarkRunner.Run<StyleChangeBenchmark>(), | ||
BenchmarkRunner.Run<FormulaCellBenchmark>(), | ||
BenchmarkRunner.Run<LoadLargeFileBenchmark>(), | ||
BenchmarkRunner.Run<SaveLargeFileBenchmark>(), | ||
BenchmarkRunner.Run<SortRangeBenchmark>(), | ||
BenchmarkRunner.Run<AccessingRangePropertiesBenchmark>(), | ||
}; | ||
|
||
reportGenerator.GenerateReport(excelSummaries, "ExcelLibs", libsWithVersions); | ||
} | ||
} | ||
|
||
static Dictionary<string, string> GetLibNamesWithVersions(Type type) | ||
{ | ||
var libNames = new Dictionary<string, string>(); | ||
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance) | ||
.Where(m => m.IsDefined(typeof(BenchmarkAttribute), false)); | ||
var fields = type.BaseType?.GetFields() ?? Array.Empty<FieldInfo>(); | ||
|
||
foreach (var property in fields) | ||
{ | ||
var propertyType = property.FieldType; | ||
|
||
foreach (var method in methods) | ||
{ | ||
if (property.Name.ToLower().Contains(method.Name.ToLower())) | ||
{ | ||
var methodName = method.Name; | ||
var assembly = propertyType.Assembly; | ||
var assemblyName = assembly.GetName(); | ||
var assemblyVersion = assemblyName.Version; | ||
|
||
if (libNames.ContainsKey(methodName)) | ||
{ | ||
libNames[methodName] = assemblyVersion?.ToString() ?? ""; | ||
} | ||
else | ||
{ | ||
libNames.Add(methodName, assemblyVersion?.ToString() ?? ""); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
return libNames; | ||
} | ||
|
||
static void ApplyIronXLLicenseKey(IHost host) | ||
{ | ||
var appConfig = ActivatorUtilities | ||
.GetServiceOrCreateInstance<IAppConfig>(host.Services); | ||
|
||
IronXL.License.LicenseKey = appConfig.LicenseKeyIronXl; | ||
} | ||
|
||
static IReportingConfig GetReportConfig(string[] args, IHost host) | ||
{ | ||
var reportConfig = ActivatorUtilities | ||
.GetServiceOrCreateInstance<IReportingConfig>(host.Services); | ||
|
||
if (args.Contains("-a") || args.Contains("-append")) | ||
{ | ||
reportConfig.AppendToLastReport = true; | ||
} | ||
|
||
return reportConfig; | ||
} | ||
|
||
static IHost SetupApplication() | ||
{ | ||
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; | ||
|
||
//var timeTableData = new IronPdfPlayList().RunPlayList(appConfig.ResultsFolderName); | ||
//reportGenerator.GenerateReport(timeTableData, "IronPdf"); | ||
var environment = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT"); | ||
var builder = new ConfigurationBuilder() | ||
.AddJsonFile($"appsettings.json", true, true) | ||
.AddJsonFile($"appsettings.{environment}.json", true, true) | ||
.AddUserSecrets(Assembly.GetExecutingAssembly(), true) | ||
.AddEnvironmentVariables(); | ||
|
||
var timeTableData = new IronXlPlayList(new Dictionary<string, string>() { { "IronXL", appConfig.LicenseKeyIronXl } }).RunPlayList(appConfig.ResultsFolderName); | ||
reportGenerator.GenerateReport(timeTableData, "IronXL"); | ||
var configurationRoot = builder.Build(); | ||
|
||
//var timeTableData = new IronBarCodePlayList().RunPlayList(appConfig.ResultsFolderName); | ||
//reportGenerator.GenerateReport(timeTableData, "IronBarCode"); | ||
var host = Host.CreateDefaultBuilder() | ||
.ConfigureServices((context, services) => | ||
{ | ||
services.AddSingleton<IAppConfig, AppConfig>( | ||
_ => configurationRoot.GetSection(nameof(AppConfig)) | ||
.Get<AppConfig>() ?? new AppConfig()); | ||
services.AddSingleton<IReportingConfig, ReportingConfig>( | ||
_ => configurationRoot.GetSection(nameof(ReportingConfig)) | ||
.Get<ReportingConfig>() ?? new ReportingConfig()); | ||
}) | ||
.Build(); | ||
return host; | ||
} |
Binary file not shown.
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,39 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace IronBenchmarks.BarCodeLibs.Benchmarks | ||
{ | ||
public abstract class BenchmarkBase | ||
{ | ||
public BenchmarkBase() | ||
{ | ||
SetupLicenses(); | ||
EnsureResultsFolderExists(); | ||
} | ||
|
||
public static void SetupLicenses() | ||
{ | ||
var builder = new ConfigurationBuilder() | ||
.AddUserSecrets(Assembly.GetExecutingAssembly(), true); | ||
var configuration = builder.Build(); | ||
var appConfig = configuration.GetSection("AppConfig"); | ||
var licenseKeyIronBarCode = appConfig["LicenseKeyIronBarCode"]; | ||
|
||
IronBarCode.License.LicenseKey = licenseKeyIronBarCode; | ||
} | ||
|
||
public static void EnsureResultsFolderExists() | ||
{ | ||
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
var reportsFolder = Path.Combine(path ?? "", "Results"); | ||
|
||
if (!Directory.Exists(reportsFolder)) | ||
{ | ||
Directory.CreateDirectory(reportsFolder); | ||
} | ||
} | ||
|
||
public abstract void Iron_BarCode(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
IronBenchmarks.BarCodeLibs/Benchmarks/CreateBarcodeBenchmark.cs
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,21 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
|
||
namespace IronBenchmarks.BarCodeLibs.Benchmarks | ||
{ | ||
[ShortRunJob] | ||
[MemoryDiagnoser] | ||
public class CreateBarcodeBenchmark : BenchmarkBase | ||
{ | ||
private readonly string text = Guid.NewGuid().ToString(); | ||
|
||
[Benchmark] | ||
public override void Iron_BarCode() | ||
{ | ||
IronBarCode.QRCodeWriter.CreateQrCode( | ||
text, | ||
500, | ||
IronBarCode.QRCodeWriter.QrErrorCorrectionLevel.Medium); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
IronBenchmarks.BarCodeLibs/IronBenchmarks.BarCodeLibs.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<UserSecretsId>84554a44-3362-4f16-874e-896b2b5196c1</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.3" /> | ||
<PackageReference Include="BarCode" Version="2022.11.10702" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.