1
- //#define MOESIF_INSTRUMENT
1
+ // #define MOESIF_INSTRUMENT
2
2
3
3
using System ;
4
4
using System . IO ;
@@ -182,32 +182,27 @@ public string Base64Encode(string plainText)
182
182
public Tuple < object , string > Serialize ( string data , string contentType , bool logBody , bool debug )
183
183
{
184
184
#if MOESIF_INSTRUMENT
185
- Stopwatch stopwatch = new Stopwatch ( ) ;
186
- stopwatch . Start ( ) ;
185
+ var logStage = false ;
186
+ var perfMetrics = new PerformanceMetrics ( "Serialize" , logStage ) ;
187
+ perfMetrics . Start ( "checkForEmptyDataAndInitObj" ) ;
187
188
_logger . LogError ( $@ "
188
189
Serialize Body: { data } " ) ;
189
-
190
- long checkForEmptyDataAndInitObj = 0 ;
191
- long parseJsonData = 0 ;
192
- long parseBase64Data = 0 ;
193
- long exceptionBase64Data = 0 ;
194
190
#endif
195
191
196
192
if ( string . IsNullOrEmpty ( data ) )
197
193
{
198
194
#if MOESIF_INSTRUMENT
199
- stopwatch . Stop ( ) ;
200
195
_logger . LogError ( $@ "
201
- Exiting Serialize Body as empty data with time: { stopwatch . ElapsedMilliseconds } ms" ) ;
196
+ Exiting Serialize Body as empty data with time" ) ;
197
+ perfMetrics . PrintMetrics ( Console . WriteLine ) ;
202
198
#endif
203
199
return new Tuple < object , string > ( null , null ) ;
204
200
}
205
201
206
202
var reqBody = new object ( ) ;
207
203
string requestTransferEncoding = null ;
208
204
#if MOESIF_INSTRUMENT
209
- checkForEmptyDataAndInitObj = stopwatch . ElapsedMilliseconds ;
210
- stopwatch . Restart ( ) ;
205
+ perfMetrics . Stop ( ) ;
211
206
#endif
212
207
if ( logBody && contentType != null && ! ( contentType . ToLower ( ) . StartsWith ( "multipart/form-data" ) ) )
213
208
{
@@ -216,61 +211,43 @@ public Tuple<object, string> Serialize(string data, string contentType, bool lo
216
211
// Only try parse if is JSON or looks like JSON
217
212
if ( contentType != null && contentType . ToLower ( ) . Contains ( "json" ) || data . StartsWith ( "{" ) || data . StartsWith ( "[" ) )
218
213
{
214
+ #if MOESIF_INSTRUMENT
215
+ perfMetrics . Start ( "parseJsonData" ) ;
216
+ #endif
219
217
reqBody = ApiHelper . JsonDeserialize < object > ( data ) ;
220
218
requestTransferEncoding = "json" ;
221
219
#if MOESIF_INSTRUMENT
222
- parseJsonData = stopwatch . ElapsedMilliseconds ;
223
- stopwatch . Restart ( ) ;
220
+ perfMetrics . Stop ( ) ;
224
221
#endif
225
222
}
226
223
else
227
224
{
228
225
LogDebugMessage ( debug , "About to parse Request body as Base64 encoding" ) ;
226
+ #if MOESIF_INSTRUMENT
227
+ perfMetrics . Start ( "parseBase64Data" ) ;
228
+ #endif
229
229
reqBody = Base64Encode ( data ) ;
230
230
requestTransferEncoding = "base64" ;
231
231
#if MOESIF_INSTRUMENT
232
- parseBase64Data = stopwatch . ElapsedMilliseconds ;
233
- stopwatch . Restart ( ) ;
232
+ perfMetrics . Stop ( ) ;
234
233
#endif
235
234
}
236
235
}
237
236
catch ( Exception )
238
237
{
239
238
LogDebugMessage ( debug , "About to parse Request body as Base64 encoding" ) ;
239
+ #if MOESIF_INSTRUMENT
240
+ perfMetrics . Start ( "exceptionBase64Data" ) ;
241
+ #endif
240
242
reqBody = Base64Encode ( data ) ;
241
243
requestTransferEncoding = "base64" ;
242
244
#if MOESIF_INSTRUMENT
243
- exceptionBase64Data = stopwatch . ElapsedMilliseconds ;
244
- stopwatch . Restart ( ) ;
245
+ perfMetrics . Stop ( ) ;
245
246
#endif
246
247
}
247
248
}
248
249
#if MOESIF_INSTRUMENT
249
- stopwatch . Stop ( ) ;
250
- var strHeader = string . Concat (
251
- "ExitingSerializeBody," ,
252
- "checkForEmptyDataAndInitObj," ,
253
- "parseJsonData," ,
254
- "parseBase64Data," ,
255
- "exceptionBase64Data"
256
- ) ;
257
- var strTimes = string . Concat (
258
- $ "{ checkForEmptyDataAndInitObj + parseJsonData + parseBase64Data + exceptionBase64Data + stopwatch . ElapsedMilliseconds } ,",
259
- $ "{ checkForEmptyDataAndInitObj } ,",
260
- $ "{ parseJsonData } ,",
261
- $ "{ parseBase64Data } ,",
262
- $ "{ exceptionBase64Data } "
263
- ) ;
264
- _logger . LogError ( $@ "
265
- { strHeader }
266
- { strTimes }
267
- " ) ;
268
- // _logger.LogError($@"
269
- // Exiting Serialize Body with time: {checkForEmptyDataAndInitObj + parseJsonData + parseBase64Data + exceptionBase64Data + stopwatch.ElapsedMilliseconds} ms
270
- // checkForEmptyDataAndInitObj took: {checkForEmptyDataAndInitObj} ms
271
- // parseJsonData took: {parseJsonData} ms
272
- // parseBase64Data took: {parseBase64Data} ms
273
- // exceptionBase64Data took: {exceptionBase64Data} ms");
250
+ perfMetrics . PrintMetrics ( Console . WriteLine ) ;
274
251
#endif
275
252
276
253
return new Tuple < object , string > ( reqBody , requestTransferEncoding ) ;
0 commit comments