|
| 1 | +using System.Diagnostics; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using BenchmarkDotNet.Environments; |
| 6 | +using BenchmarkDotNet.Jobs; |
| 7 | +using BenchmarkDotNet.Toolchains.NativeAot; |
| 8 | +using Lua; |
| 9 | +using Lua.Runtime; |
| 10 | +using Lua.Standard; |
| 11 | +using Perfolizer.Horology; |
| 12 | + |
| 13 | +static class RuntimeJobs |
| 14 | +{ |
| 15 | + public static Job NativeAot(Job job) => |
| 16 | + job.WithRuntime(NativeAotRuntime.Net10_0) |
| 17 | + .WithArguments([ |
| 18 | + new MsBuildArgument("/p:RestoreSources=https://api.nuget.org/v3/index.json"), |
| 19 | + ]) |
| 20 | + .WithToolchain( |
| 21 | + NativeAotToolchain |
| 22 | + .CreateBuilder() |
| 23 | + .UseNuGet("10.0.14", "https://api.nuget.org/v3/index.json") |
| 24 | + .IlcInstructionSet("native") |
| 25 | + .TargetFrameworkMoniker("net10.0") |
| 26 | + .RuntimeFrameworkVersion("10.0.14") |
| 27 | + .UseNuGetClearTag(true) |
| 28 | + .DisplayName("NativeAOT 10.0.14") |
| 29 | + .ToToolchain() |
| 30 | + ) |
| 31 | + .WithId("NET10-NativeAOT"); |
| 32 | +} |
| 33 | + |
| 34 | +public sealed class RuntimeExecutionConfig : ManualConfig |
| 35 | +{ |
| 36 | + public RuntimeExecutionConfig() |
| 37 | + { |
| 38 | + AddDiagnoser(MemoryDiagnoser.Default); |
| 39 | + var job = Job |
| 40 | + .ShortRun.WithLaunchCount(3) |
| 41 | + .WithWarmupCount(5) |
| 42 | + .WithIterationCount(10) |
| 43 | + .WithIterationTime(TimeInterval.FromMilliseconds(250)); |
| 44 | + AddJob(job.WithRuntime(CoreRuntime.Core10_0).WithId("NET10-JIT").AsBaseline()); |
| 45 | + AddJob(RuntimeJobs.NativeAot(job)); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +public sealed class RuntimeCompilationConfig : ManualConfig |
| 50 | +{ |
| 51 | + public RuntimeCompilationConfig() |
| 52 | + { |
| 53 | + AddDiagnoser(MemoryDiagnoser.Default); |
| 54 | + var job = Job |
| 55 | + .ShortRun.WithLaunchCount(3) |
| 56 | + .WithWarmupCount(20) |
| 57 | + .WithIterationCount(20) |
| 58 | + .WithInvocationCount(1) |
| 59 | + .WithUnrollFactor(1); |
| 60 | + AddJob(job.WithRuntime(CoreRuntime.Core10_0).WithId("NET10-JIT").AsBaseline()); |
| 61 | + AddJob(RuntimeJobs.NativeAot(job)); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +[Config(typeof(RuntimeExecutionConfig))] |
| 66 | +[BenchmarkCategory("Execution")] |
| 67 | +public class RuntimeExecutionComparison |
| 68 | +{ |
| 69 | + [ParamsSource(nameof(Cases))] |
| 70 | + public string Case { get; set; } = "numeric-loop"; |
| 71 | + public static IEnumerable<string> Cases => ComparisonWorkloads.Cases.Select(c => c.Name); |
| 72 | + LuaCSharpComparison runner = default!; |
| 73 | + |
| 74 | + [GlobalSetup] |
| 75 | + public void Setup() |
| 76 | + { |
| 77 | + var workload = ComparisonWorkloads.Find(Case); |
| 78 | + RuntimeVerification.Verify(workload); |
| 79 | + runner = new(workload.Source); |
| 80 | + } |
| 81 | + |
| 82 | + [Benchmark] |
| 83 | + public double Execute() => runner.Run(); |
| 84 | + |
| 85 | + [GlobalCleanup] |
| 86 | + public void Cleanup() => runner.Dispose(); |
| 87 | +} |
| 88 | + |
| 89 | +[Config(typeof(RuntimeCompilationConfig))] |
| 90 | +[BenchmarkCategory("Compilation")] |
| 91 | +public class RuntimeCompilationComparison |
| 92 | +{ |
| 93 | + [ParamsSource(nameof(Cases))] |
| 94 | + public string Case { get; set; } = "numeric-loop"; |
| 95 | + public static IEnumerable<string> Cases => |
| 96 | + ComparisonWorkloads.CompilationCases.Select(c => c.Name); |
| 97 | + string source = default!; |
| 98 | + LuaState state = default!; |
| 99 | + const int BatchSize = 64; |
| 100 | + |
| 101 | + [GlobalSetup] |
| 102 | + public void Setup() |
| 103 | + { |
| 104 | + var workload = ComparisonWorkloads.FindCompilation(Case); |
| 105 | + RuntimeVerification.Verify(workload); |
| 106 | + source = workload.Source; |
| 107 | + var timer = Stopwatch.StartNew(); |
| 108 | + do |
| 109 | + { |
| 110 | + ResetState(); |
| 111 | + try |
| 112 | + { |
| 113 | + Compile(); |
| 114 | + } |
| 115 | + finally |
| 116 | + { |
| 117 | + Cleanup(); |
| 118 | + } |
| 119 | + } while (timer.ElapsedMilliseconds < 2000); |
| 120 | + } |
| 121 | + |
| 122 | + [IterationSetup] |
| 123 | + public void ResetState() |
| 124 | + { |
| 125 | + state = LuaState.Create(); |
| 126 | + state.OpenStandardLibraries(); |
| 127 | + } |
| 128 | + |
| 129 | + [Benchmark(OperationsPerInvoke = BatchSize)] |
| 130 | + public LuaClosure Compile() |
| 131 | + { |
| 132 | + LuaClosure result = default!; |
| 133 | + for (var i = 0; i < BatchSize; i++) |
| 134 | + result = state.Load(source, "comparison"); |
| 135 | + return result; |
| 136 | + } |
| 137 | + |
| 138 | + [IterationCleanup] |
| 139 | + public void Cleanup() => state.Dispose(); |
| 140 | +} |
0 commit comments