11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
3+ using System . Text ;
34using BenchmarkDotNet . Attributes ;
45using Microsoft . AspNetCore . Http . Generators . Tests ;
6+ using Microsoft . CodeAnalysis ;
7+ using Microsoft . CodeAnalysis . CSharp ;
8+ using Microsoft . CodeAnalysis . Text ;
59
610namespace Microsoft . AspNetCore . Http . Microbenchmarks ;
711
@@ -12,21 +16,34 @@ public class RequestDelegateGeneratorBenchmarks : RequestDelegateCreationTestBas
1216 [ Params ( 10 , 100 , 1000 , 10000 ) ]
1317 public int EndpointCount { get ; set ; }
1418
15- private string _source ;
19+ private GeneratorDriver _driver ;
20+ private Compilation _compilation ;
1621
1722 [ GlobalSetup ]
18- public void Setup ( )
23+ public async Task Setup ( )
1924 {
20- _source = "" ;
25+ var project = CreateProject ( ) ;
26+ var innerSource = "" ;
2127 for ( var i = 0 ; i < EndpointCount ; i ++ )
2228 {
23- _source += $ """ app.MapGet("/route{ i } ", (int? id) => "Hello World!");""" ;
29+ innerSource += $ """ app.MapGet("/route{ i } ", (int? id) => "Hello World!");""" ;
2430 }
31+ var source = GetMapActionString ( innerSource ) ;
32+ project = project . AddDocument ( "TestMapActions.cs" , SourceText . From ( source , Encoding . UTF8 ) ) . Project ;
33+ _compilation = await project . GetCompilationAsync ( ) ;
34+
35+ var generator = new RequestDelegateGenerator . RequestDelegateGenerator ( ) . AsSourceGenerator ( ) ;
36+ _driver = CSharpGeneratorDriver . Create ( generators : new [ ]
37+ {
38+ generator
39+ } ,
40+ driverOptions : new GeneratorDriverOptions ( IncrementalGeneratorOutputKind . None , trackIncrementalGeneratorSteps : true ) ,
41+ parseOptions : ParseOptions ) ;
2542 }
2643
2744 [ Benchmark ]
28- public async Task CreateRequestDelegate ( )
45+ public void CreateRequestDelegate ( )
2946 {
30- await RunGeneratorAsync ( _source ) ;
47+ _driver . RunGeneratorsAndUpdateCompilation ( _compilation , out var _ , out var _ ) ;
3148 }
3249}
0 commit comments