Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (89 loc) · 3.74 KB
/
Copy pathProgram.cs
File metadata and controls
95 lines (89 loc) · 3.74 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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using Perfolizer.Horology;
namespace Regression;
[MemoryDiagnoser]
[JsonExporterAttribute.Full]
public class PerformanceBenchmarks
{
[Params("Standard", "Light")] public string Backend { get; set; } = "Standard";
[Params("Scalar", "Arithmetic", "CallConditional", "Closure")] public string Shape { get; set; } = "Scalar";
private Func<Delegate> compile = null!;
private Func<object> construct = null!;
private Func<int, int>? scalar;
private Func<int, Func<int>>? closure;
private Delegate compiled = null!;
[GlobalSetup]
public void Setup()
{
if (Backend == "Standard")
{
compile = Standard.Workloads.Prepare(Shape);
construct = () => Standard.Workloads.Construct(Shape);
Standard.Workloads.Verify(Shape, compile);
}
else
{
compile = Light.Workloads.Prepare(Shape);
construct = () => Light.Workloads.Construct(Shape);
Light.Workloads.Verify(Shape, compile);
}
compiled = compile();
if (Shape == "Closure") { closure = (Func<int, Func<int>>)compiled; _ = closure(3)(); }
else { scalar = (Func<int, int>)compiled; _ = scalar(3); }
}
[Benchmark] public object Construct() => construct();
[Benchmark] public Delegate Compile() => compile();
[Benchmark]
public int CompileAndFirstInvoke()
{
var fresh = compile();
return Shape == "Closure" ? ((Func<int, Func<int>>)fresh)(3)() : ((Func<int, int>)fresh)(3);
}
// Amortize harness overhead; return a consumed checksum to keep every call observable.
[Benchmark(OperationsPerInvoke = 256)]
public int Invoke()
{
var sum = 0;
if (Shape == "Closure")
{
var f = closure!;
for (var i = 0; i < 256; ++i) sum += f((i & 15) - 8)();
}
else
{
var f = scalar!;
for (var i = 0; i < 256; ++i) sum += f((i & 15) - 8);
}
return sum;
}
}
public static class Program
{
public static int Main(string[] args)
{
if (args.Contains("--verify"))
{
foreach (var backend in new[] { "Standard", "Light" })
foreach (var shape in new[] { "Scalar", "Arithmetic", "CallConditional", "Closure" })
{
var benchmark = new PerformanceBenchmarks { Backend = backend, Shape = shape };
benchmark.Setup();
var first = shape switch { "Scalar" or "Closure" => 4, "Arithmetic" => 96, _ => 7 };
var batched = shape switch { "Scalar" or "Closure" => 128, "Arithmetic" => -4096, _ => 1216 };
if (benchmark.CompileAndFirstInvoke() != first || benchmark.Invoke() != batched || benchmark.Construct() == null)
throw new InvalidOperationException("Incorrect benchmark driver: " + backend + "/" + shape);
}
Console.WriteLine("All 8 workload/backend behavior checks passed.");
return 0;
}
var smoke = args.Contains("--smoke");
var job = smoke ? Job.Dry : Job.Default.WithLaunchCount(1).WithWarmupCount(2).WithIterationCount(5).WithIterationTime(TimeInterval.FromMilliseconds(100));
var config = ManualConfig.Create(DefaultConfig.Instance).AddJob(job.WithId(smoke ? "Smoke" : "Regression"));
var summaries = BenchmarkSwitcher.FromTypes(new[] { typeof(PerformanceBenchmarks) }).Run(args.Where(a => a != "--smoke").ToArray(), config).ToArray();
return summaries.Length == 0 || summaries.Any(s => s.HasCriticalValidationErrors || s.Reports.Any(r => !r.Success)) ? 1 : 0;
}
}