1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
+ using System . Text ;
3
4
using BenchmarkDotNet . Attributes ;
4
5
using Microsoft . AspNetCore . Http . Generators . Tests ;
6
+ using Microsoft . CodeAnalysis ;
7
+ using Microsoft . CodeAnalysis . CSharp ;
8
+ using Microsoft . CodeAnalysis . Text ;
5
9
6
10
namespace Microsoft . AspNetCore . Http . Microbenchmarks ;
7
11
@@ -12,21 +16,34 @@ public class RequestDelegateGeneratorBenchmarks : RequestDelegateCreationTestBas
12
16
[ Params ( 10 , 100 , 1000 , 10000 ) ]
13
17
public int EndpointCount { get ; set ; }
14
18
15
- private string _source ;
19
+ private GeneratorDriver _driver ;
20
+ private Compilation _compilation ;
16
21
17
22
[ GlobalSetup ]
18
- public void Setup ( )
23
+ public async Task Setup ( )
19
24
{
20
- _source = "" ;
25
+ var project = CreateProject ( ) ;
26
+ var innerSource = "" ;
21
27
for ( var i = 0 ; i < EndpointCount ; i ++ )
22
28
{
23
- _source += $ """ app.MapGet("/route{ i } ", (int? id) => "Hello World!");""" ;
29
+ innerSource += $ """ app.MapGet("/route{ i } ", (int? id) => "Hello World!");""" ;
24
30
}
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 ) ;
25
42
}
26
43
27
44
[ Benchmark ]
28
- public async Task CreateRequestDelegate ( )
45
+ public void CreateRequestDelegate ( )
29
46
{
30
- await RunGeneratorAsync ( _source ) ;
47
+ _driver . RunGeneratorsAndUpdateCompilation ( _compilation , out var _ , out var _ ) ;
31
48
}
32
49
}
0 commit comments