1
+ using BenchmarkDotNet . Attributes ;
2
+ using Microsoft . AspNetCore . Http ;
3
+ using Prometheus ;
4
+ using System . Threading . Tasks ;
5
+ using Grpc . AspNetCore . Server ;
6
+ using Grpc . Core ;
7
+
8
+ namespace Benchmark . NetCore
9
+ {
10
+ [ MemoryDiagnoser ]
11
+ public class GrpcExporterBenchmarks
12
+ {
13
+ private CollectorRegistry _registry ;
14
+ private MetricFactory _factory ;
15
+ private GrpcRequestCountMiddleware _countMiddleware ;
16
+ private GrpcRequestDurationMiddleware _durationMiddleware ;
17
+ private DefaultHttpContext _ctx ;
18
+
19
+ [ Params ( 1000 , 10000 ) ]
20
+ public int RequestCount { get ; set ; }
21
+
22
+ [ GlobalSetup ]
23
+ public void Setup ( )
24
+ {
25
+ _ctx = new DefaultHttpContext ( ) ;
26
+ _ctx . SetEndpoint ( new Endpoint (
27
+ ctx => Task . CompletedTask ,
28
+ new EndpointMetadataCollection ( new GrpcMethodMetadata ( typeof ( int ) ,
29
+ new Method < object , object > ( MethodType . Unary ,
30
+ "test" ,
31
+ "test" ,
32
+ new Marshaller < object > ( o => new byte [ 0 ] , c => null ) ,
33
+ new Marshaller < object > ( o => new byte [ 0 ] , c => null ) ) ) ) ,
34
+ "test" ) ) ;
35
+ _registry = Metrics . NewCustomRegistry ( ) ;
36
+ _factory = Metrics . WithCustomRegistry ( _registry ) ;
37
+
38
+ _countMiddleware = new GrpcRequestCountMiddleware ( next => Task . CompletedTask , new GrpcRequestCountOptions
39
+ {
40
+ Counter = _factory . CreateCounter ( "count" , "help" )
41
+ } ) ;
42
+ _durationMiddleware = new GrpcRequestDurationMiddleware ( next => Task . CompletedTask , new GrpcRequestDurationOptions
43
+ {
44
+ Histogram = _factory . CreateHistogram ( "duration" , "help" )
45
+ } ) ;
46
+ }
47
+
48
+ [ Benchmark ]
49
+ public async Task GrpcRequestCount ( )
50
+ {
51
+ for ( var i = 0 ; i < RequestCount ; i ++ )
52
+ await _countMiddleware . Invoke ( _ctx ) ;
53
+ }
54
+
55
+ [ Benchmark ]
56
+ public async Task GrpcRequestDuration ( )
57
+ {
58
+ for ( var i = 0 ; i < RequestCount ; i ++ )
59
+ await _durationMiddleware . Invoke ( _ctx ) ;
60
+ }
61
+ }
62
+ }
0 commit comments