3
3
using POGOProtos . Networking . Requests ;
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using System . Linq ;
7
8
using System . Net . Http ;
8
9
using System . Text ;
@@ -13,9 +14,19 @@ namespace MandraSoft.PokemonGo.Api.Extensions
13
14
public static class HttpClientExtensions
14
15
{
15
16
private static bool Relogging = false ;
17
+ static int _Count = 0 ;
18
+ static long _TotalElapsedNetwork = 0 ;
19
+ static long _TotalElapsedHandling = 0 ;
16
20
public static async Task < List < IMessage > > GetResponses ( this HttpClient client , PokemonGoClient pogoClient , bool withAuthTicket , string url , params Request [ ] requests )
17
21
{
22
+ #if _INSTRUMENTING
23
+ Stopwatch sw = new Stopwatch ( ) ;
24
+ sw . Start ( ) ;
25
+ #endif
18
26
var response = await GetEnvelope ( client , pogoClient , withAuthTicket , url , requests ) ;
27
+ #if _INSTRUMENTING
28
+ _TotalElapsedNetwork += sw . ElapsedMilliseconds ;
29
+ #endif
19
30
int retryCount = 0 ;
20
31
while ( response . Returns . Count != requests . Length )
21
32
{
@@ -40,6 +51,10 @@ public static async Task<List<IMessage>> GetResponses(this HttpClient client, Po
40
51
}
41
52
if ( retryCount > 5 ) throw new Exception ( "Client in a weird state" ) ;
42
53
}
54
+ #if _INSTRUMENTING
55
+ sw . Reset ( ) ;
56
+ sw . Start ( ) ;
57
+ #endif
43
58
var result = new List < IMessage > ( ) ;
44
59
for ( var i = 0 ; i < requests . Length ; i ++ )
45
60
{
@@ -53,26 +68,66 @@ public static async Task<List<IMessage>> GetResponses(this HttpClient client, Po
53
68
else result . Add ( new POGOProtos . Networking . Responses . EchoResponse ( ) ) ;
54
69
}
55
70
await pogoClient . HandleGenericResponses ( result ) ;
71
+ #if _INSTRUMENTING
72
+ _TotalElapsedHandling += sw . ElapsedMilliseconds ;
73
+ _Count ++ ;
74
+ if ( _Count % 100 == 0 )
75
+ {
76
+ Console . WriteLine ( "Average network time : " + ( double ) _TotalElapsedNetwork / ( double ) _Count + "ms" ) ;
77
+ Console . WriteLine ( " AuthTicket : " + ( double ) _TotalAuthTicket / ( double ) _Count + "ms" ) ;
78
+ Console . WriteLine ( " Request Serialization : " + ( double ) _TotalSerializationRequest / ( double ) _Count + "ms" ) ;
79
+ Console . WriteLine ( " Post Time : " + ( double ) _TotalPostTime / ( double ) _Count + "ms" ) ;
80
+ Console . WriteLine ( " ReadResponse : " + ( double ) _TotalReadResponse / ( double ) _Count + "ms" ) ;
81
+ Console . WriteLine ( " Deserialization : " + ( double ) _TotalDeserialization / ( double ) _Count + "ms" ) ;
82
+ Console . WriteLine ( "Average handling time : " + ( double ) _TotalElapsedHandling / ( double ) _Count + "ms" ) ;
83
+ }
84
+ #endif
56
85
return result ;
57
86
}
58
-
87
+ static private long _TotalAuthTicket = 0 ;
88
+ static private long _TotalSerializationRequest = 0 ;
89
+ static private long _TotalPostTime = 0 ;
90
+ static private long _TotalReadResponse = 0 ;
91
+ static private long _TotalDeserialization = 0 ;
59
92
public static async Task < POGOProtos . Networking . Envelopes . ResponseEnvelope > GetEnvelope ( this HttpClient client , PokemonGoClient pogoClient , bool withAuthTicket , string url , params Request [ ] requests )
60
93
{
94
+ #if _INSTRUMENTING
95
+ Stopwatch sw = new Stopwatch ( ) ;
96
+ sw . Start ( ) ;
97
+ #endif
61
98
//Check Session closed.
62
99
if ( withAuthTicket && pogoClient . _authTicket . ExpireTimestampMs < ( ulong ) DateTime . UtcNow . ToUnixTime ( ) )
63
100
{
64
101
await pogoClient . LoginPtc ( ) ;
65
102
await pogoClient . SetServer ( ) ;
66
103
url = pogoClient . _apiUrl ;
67
104
}
105
+ #if _INSTRUMENTING
106
+ _TotalAuthTicket += sw . ElapsedMilliseconds ;
107
+ sw . Restart ( ) ;
108
+ #endif
68
109
var response = new POGOProtos . Networking . Envelopes . ResponseEnvelope ( ) ;
69
110
var requestEnvloppe = await pogoClient . GetRequest ( withAuthTicket , requests ) ;
70
111
var data = requestEnvloppe . ToByteString ( ) ;
112
+ #if _INSTRUMENTING
113
+ _TotalSerializationRequest += sw . ElapsedMilliseconds ;
114
+ sw . Restart ( ) ;
115
+ #endif
71
116
var result = await client . PostAsync ( url , new ByteArrayContent ( data . ToByteArray ( ) ) ) ;
72
-
117
+ #if _INSTRUMENTING
118
+ _TotalPostTime += sw . ElapsedMilliseconds ;
119
+ sw . Restart ( ) ;
120
+ #endif
73
121
var responseData = await result . Content . ReadAsByteArrayAsync ( ) ;
122
+ #if _INSTRUMENTING
123
+ _TotalReadResponse += sw . ElapsedMilliseconds ;
124
+ sw . Restart ( ) ;
125
+ #endif
74
126
var codedStream = new CodedInputStream ( responseData ) ;
75
127
response . MergeFrom ( codedStream ) ;
128
+ #if _INSTRUMENTING
129
+ _TotalDeserialization += sw . ElapsedMilliseconds ;
130
+ #endif
76
131
return response ;
77
132
}
78
133
}
0 commit comments