2026-02-05 14:14:54 +00:00
|
|
|
using System;
|
2026-02-05 13:30:57 +00:00
|
|
|
using BenchmarkDotNet.Configs;
|
|
|
|
|
using BenchmarkDotNet.Jobs;
|
|
|
|
|
using BenchmarkDotNet.Running;
|
|
|
|
|
using BenchmarkDotNet.Toolchains.InProcess.Emit;
|
2025-10-23 09:39:57 +01:00
|
|
|
|
2026-02-05 13:30:57 +00:00
|
|
|
namespace SharpCompress.Performance;
|
2025-10-23 09:39:57 +01:00
|
|
|
|
2026-02-05 13:30:57 +00:00
|
|
|
public class Program
|
2025-10-23 09:39:57 +01:00
|
|
|
{
|
2026-02-05 13:30:57 +00:00
|
|
|
public static void Main(string[] args)
|
2025-10-23 09:39:57 +01:00
|
|
|
{
|
2026-02-05 14:14:54 +00:00
|
|
|
// Check if profiling mode is requested
|
|
|
|
|
if (args.Length > 0 && args[0].Equals("--profile", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
RunWithProfiler(args);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default: Run BenchmarkDotNet
|
2026-02-05 13:43:44 +00:00
|
|
|
var config = DefaultConfig.Instance.AddJob(
|
|
|
|
|
Job.Default.WithToolchain(InProcessEmitToolchain.Instance)
|
|
|
|
|
.WithWarmupCount(3) // Minimal warmup iterations for CI
|
|
|
|
|
.WithIterationCount(10) // Minimal measurement iterations for CI
|
|
|
|
|
.WithInvocationCount(10)
|
|
|
|
|
.WithUnrollFactor(1)
|
|
|
|
|
);
|
2025-10-23 11:43:21 +01:00
|
|
|
|
2026-02-05 13:30:57 +00:00
|
|
|
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
|
2025-10-23 09:39:57 +01:00
|
|
|
}
|
2026-02-05 14:14:54 +00:00
|
|
|
|
|
|
|
|
private static void RunWithProfiler(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var profileType = "cpu"; // Default to CPU profiling
|
|
|
|
|
var outputPath = "./profiler-snapshots";
|
|
|
|
|
|
|
|
|
|
// Parse arguments
|
|
|
|
|
for (int i = 1; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (args[i].Equals("--type", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
|
|
|
|
|
{
|
|
|
|
|
profileType = args[++i].ToLowerInvariant();
|
|
|
|
|
}
|
|
|
|
|
else if (
|
|
|
|
|
args[i].Equals("--output", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
&& i + 1 < args.Length
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
outputPath = args[++i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Running with JetBrains Profiler ({profileType} mode)");
|
|
|
|
|
Console.WriteLine($"Output path: {outputPath}");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"Usage: dotnet run --project SharpCompress.Performance.csproj -c Release -- --profile [--type cpu|memory] [--output <path>]"
|
|
|
|
|
);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
// Run a sample benchmark with profiling
|
|
|
|
|
RunSampleBenchmarkWithProfiler(profileType, outputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RunSampleBenchmarkWithProfiler(string profileType, string outputPath)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Running sample benchmark with profiler...");
|
|
|
|
|
Console.WriteLine("Note: JetBrains profiler requires the profiler tools to be installed.");
|
|
|
|
|
Console.WriteLine("Install from: https://www.jetbrains.com/profiler/");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IDisposable? profiler = null;
|
|
|
|
|
|
|
|
|
|
if (profileType == "cpu")
|
|
|
|
|
{
|
|
|
|
|
profiler = Test.JetbrainsProfiler.Cpu(outputPath);
|
|
|
|
|
}
|
|
|
|
|
else if (profileType == "memory")
|
|
|
|
|
{
|
|
|
|
|
profiler = Test.JetbrainsProfiler.Memory(outputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (profiler)
|
|
|
|
|
{
|
|
|
|
|
// Run a simple benchmark iteration
|
|
|
|
|
var zipBenchmark = new Benchmarks.ZipBenchmarks();
|
|
|
|
|
zipBenchmark.Setup();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Running benchmark iterations...");
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
zipBenchmark.ZipExtractArchiveApi();
|
|
|
|
|
if (i % 3 == 0)
|
|
|
|
|
{
|
|
|
|
|
Console.Write(".");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Benchmark iterations completed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Profiler snapshot saved to: {outputPath}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error running profiler: {ex.Message}");
|
|
|
|
|
Console.WriteLine("Make sure JetBrains profiler tools are installed and accessible.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-23 09:39:57 +01:00
|
|
|
}
|