Browse Source

Remove more setup overhead in RDG benchmarks (#50302)

Safia Abdalla 2 years ago
parent
commit
a0fb1dd68c

+ 23 - 6
src/Http/Http/perf/Microbenchmarks/RequestDelegateGeneratorBenchmarks.cs

@@ -1,7 +1,11 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
+using System.Text;
 using BenchmarkDotNet.Attributes;
 using Microsoft.AspNetCore.Http.Generators.Tests;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.Text;
 
 namespace Microsoft.AspNetCore.Http.Microbenchmarks;
 
@@ -12,21 +16,34 @@ public class RequestDelegateGeneratorBenchmarks : RequestDelegateCreationTestBas
     [Params(10, 100, 1000, 10000)]
     public int EndpointCount { get; set; }
 
-    private string _source;
+    private GeneratorDriver _driver;
+    private Compilation _compilation;
 
     [GlobalSetup]
-    public void Setup()
+    public async Task Setup()
     {
-        _source = "";
+        var project = CreateProject();
+        var innerSource = "";
         for (var i = 0; i < EndpointCount; i++)
         {
-            _source += $"""app.MapGet("/route{i}", (int? id) => "Hello World!");""";
+            innerSource += $"""app.MapGet("/route{i}", (int? id) => "Hello World!");""";
         }
+        var source = GetMapActionString(innerSource);
+        project = project.AddDocument("TestMapActions.cs", SourceText.From(source, Encoding.UTF8)).Project;
+        _compilation = await project.GetCompilationAsync();
+
+        var generator = new RequestDelegateGenerator.RequestDelegateGenerator().AsSourceGenerator();
+        _driver = CSharpGeneratorDriver.Create(generators: new[]
+            {
+                generator
+            },
+            driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true),
+            parseOptions: ParseOptions);
     }
 
     [Benchmark]
-    public async Task CreateRequestDelegate()
+    public void CreateRequestDelegate()
     {
-        await RunGeneratorAsync(_source);
+        _driver.RunGeneratorsAndUpdateCompilation(_compilation, out var _, out var _);
     }
 }