-
Notifications
You must be signed in to change notification settings - Fork 512
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (98 loc) · 3.76 KB
/
Program.cs
File metadata and controls
112 lines (98 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
namespace SharpCompress.Performance;
public class Program
{
public static void Main(string[] args)
{
// Check if profiling mode is requested
if (args.Length > 0 && args[0].Equals("--profile", StringComparison.OrdinalIgnoreCase))
{
RunWithProfiler(args);
return;
}
// Default: Run BenchmarkDotNet
var config = DefaultConfig.Instance.AddJob(
Job.Default.WithToolchain(InProcessEmitToolchain.Instance)
.WithWarmupCount(5) // Minimal warmup iterations for CI
.WithIterationCount(30) // Minimal measurement iterations for CI
.WithInvocationCount(30)
.WithUnrollFactor(2)
);
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
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.");
}
}
}