1+ using BenchmarkDotNet . Attributes ;
2+ using CaseManagement . CMMN . CaseInstance . Commands ;
3+ using Newtonsoft . Json ;
4+ using Newtonsoft . Json . Linq ;
5+ using System ;
6+ using System . Net . Http ;
7+ using System . Text ;
8+ using System . Threading ;
9+ using System . Threading . Tasks ;
10+
11+ namespace CaseManagement . CMMN . Benchmark
12+ {
13+ [ InProcess ]
14+ [ MemoryDiagnoser ]
15+ [ RPlotExporter ]
16+ public class CaseInstanceControllerBenchmark
17+ {
18+ private HttpClient _client ;
19+
20+ [ GlobalSetup ]
21+ public void GlobalSetup ( )
22+ {
23+ var factory = new CustomWebApplicationFactory < Startup > ( ) ;
24+ _client = factory . CreateClient ( ) ;
25+ }
26+
27+ [ Benchmark ]
28+ public async Task CreateCaseInstance ( )
29+ {
30+ var cmd = new CreateCaseInstanceCommand
31+ {
32+ CaseDefinitionId = "caseWithTimerEventListener" ,
33+ CasesId = "Case_0d1ujq8"
34+ } ;
35+ var httpRequestMessage = new HttpRequestMessage
36+ {
37+ Method = HttpMethod . Post ,
38+ RequestUri = new Uri ( "http://localhost/case-instances" ) ,
39+ Content = new StringContent ( JsonConvert . SerializeObject ( cmd ) , Encoding . UTF8 , "application/json" )
40+ } ;
41+ var httpResult = await _client . SendAsync ( httpRequestMessage ) ;
42+ var json = await httpResult . Content . ReadAsStringAsync ( ) ;
43+ var id = JsonConvert . DeserializeObject < JObject > ( json ) [ "id" ] . ToString ( ) ;
44+ await PollCaseInstanceCreated ( id ) ;
45+ }
46+
47+ [ Benchmark ]
48+ public async Task LaunchEventListener2Seconds ( )
49+ {
50+ var cmd = new CreateCaseInstanceCommand
51+ {
52+ CaseDefinitionId = "caseWithTimerEventListener" ,
53+ CasesId = "Case_0d1ujq8"
54+ } ;
55+ var httpRequestMessage = new HttpRequestMessage
56+ {
57+ Method = HttpMethod . Post ,
58+ RequestUri = new Uri ( "http://localhost/case-instances" ) ,
59+ Content = new StringContent ( JsonConvert . SerializeObject ( cmd ) , Encoding . UTF8 , "application/json" )
60+ } ;
61+ var httpResult = await _client . SendAsync ( httpRequestMessage ) ;
62+ var json = await httpResult . Content . ReadAsStringAsync ( ) ;
63+ var id = JsonConvert . DeserializeObject < JObject > ( json ) [ "id" ] . ToString ( ) ;
64+ await _client . GetAsync ( $ "http://localhost/case-instances/{ id } /launch") ;
65+ await PollCaseInstanceCompleted ( id ) ;
66+ }
67+
68+ public async Task PollCaseInstanceCreated ( string id )
69+ {
70+ var httpResult = await _client . GetAsync ( $ "http://localhost/case-instances/{ id } ") ;
71+ if ( ! httpResult . IsSuccessStatusCode )
72+ {
73+ await PollCaseInstanceCreated ( id ) ;
74+ return ;
75+ }
76+ }
77+
78+ public async Task PollCaseInstanceCompleted ( string id )
79+ {
80+ var httpResult = await _client . GetAsync ( $ "http://localhost/case-instances/{ id } ") ;
81+ if ( ! httpResult . IsSuccessStatusCode )
82+ {
83+ Thread . Sleep ( 2 ) ;
84+ await PollCaseInstanceCompleted ( id ) ;
85+ return ;
86+ }
87+
88+ var json = await httpResult . Content . ReadAsStringAsync ( ) ;
89+ var jObj = JsonConvert . DeserializeObject < JObject > ( json ) ;
90+ if ( ! jObj . ContainsKey ( "status" ) )
91+ {
92+ Thread . Sleep ( 2 ) ;
93+ await PollCaseInstanceCompleted ( id ) ;
94+ return ;
95+ }
96+
97+ var status = jObj [ "status" ] . ToString ( ) ;
98+ if ( status != "completed" )
99+ {
100+ Thread . Sleep ( 2 ) ;
101+ await PollCaseInstanceCompleted ( id ) ;
102+ return ;
103+ }
104+ }
105+ }
106+ }
0 commit comments