Skip to content

Commit dd9d7ea

Browse files
authored
#188509201 [Part 2 of 3] : Performance improvement (#102)
* Performance improvement + Performance Metrics + Version Bump. * Refactor + Code Cleanup * Refactor + Code Cleanup * Refactor + Code Cleanup + Use latest base sdk * Fix nuspec file.
1 parent 61132e4 commit dd9d7ea

File tree

9 files changed

+116
-375
lines changed

9 files changed

+116
-375
lines changed

Moesif.Middleware/Helpers/AppConfigHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #define MOESIF_INSTRUMENT
1+
//#define MOESIF_INSTRUMENT
22
using System;
33
using System.Linq;
44
using System.Collections.Generic;

Moesif.Middleware/Helpers/GovernanceHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #define MOESIF_INSTRUMENT
1+
//#define MOESIF_INSTRUMENT
22

33
using System;
44
using Moesif.Api;
@@ -68,11 +68,11 @@ public static bool enforceGovernaceRule(EventModel eventModel, Governance govern
6868
var matchingRules = new List<(Rule, GovernanceRule)>();
6969
var matchingUser = findMatchingEntityRule(eventModel.UserId, "user", governace, config, requestMap);
7070
matchingRules.AddRange(matchingUser);
71-
72-
71+
72+
7373
var matchingCompany = findMatchingEntityRule(eventModel.CompanyId, "company", governace, config, requestMap);
7474
matchingRules.AddRange(matchingCompany);
75-
75+
7676

7777
var regexRules = new List<GovernanceRule>();
7878
var matchingRegexRules = new List<(Rule, GovernanceRule)>();

Moesif.Middleware/Helpers/Tasks.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
//#define MOESIF_INSTRUMENT
2+
3+
using System;
24
using System.Linq;
35
using System.Collections.Generic;
46
using Moesif.Api;
@@ -73,7 +75,9 @@ public static async Task AsyncClientCreateEvent(MoesifApiClient client, Concurre
7375
governanceEvent.Set();
7476
logger.LogDebug("GoveranceRule has changed at {time}", DateTime.UtcNow);
7577
}
76-
78+
#if MOESIF_INSTRUMENT
79+
logger.LogError($"Events [{batchEvents.Count}] sent successfully to Moesif at {DateTime.UtcNow}");
80+
#endif
7781
logger.LogDebug("Events sent successfully to Moesif at {time}", DateTime.UtcNow);
7882
}
7983
catch (Exception e)

Moesif.Middleware/Moesif.Middleware.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Moesif.Api" Version="3.1.1" />
22+
<PackageReference Include="Moesif.Api" Version="3.1.2" />
2323
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.32" />
2424
<!-- <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.0" /> -->
2525
<!-- <PackageReference Include="Microsoft.Owin" Version="4.2.2">-->
@@ -59,9 +59,9 @@
5959
<ItemGroup>
6060
<Compile Remove="Helpers\ClientIp.cs" />
6161
</ItemGroup>
62-
<!-- <ItemGroup> -->
63-
<!-- <ProjectReference Include="..\..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj" /> -->
64-
<!-- </ItemGroup> -->
62+
<!-- <ItemGroup> -->
63+
<!-- <ProjectReference Include="..\..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj" /> -->
64+
<!-- </ItemGroup> -->
6565

6666
<!-- <ItemGroup> -->
6767
<!-- <None Remove="Microsoft.Owin" /> -->

Moesif.Middleware/Moesif.Middleware.nuspec

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<package >
33
<metadata>
44
<id>Moesif.Middleware</id>
5-
<version>3.1.2</version>
5+
<version>3.1.3</version>
66
<title>MoesifMiddleware</title>
77
<authors>Moesif</authors>
8-
<owners>Moesif</owners>
8+
<owners>Moesif</owners>
99
<license type="expression">Apache-2.0</license>
10-
<projectUrl>https://www.moesif.com</projectUrl>
10+
<projectUrl>https://www.moesif.com</projectUrl>
1111
<iconUrl>https://cdn.moesif.com/images/icon.png</iconUrl>
1212
<readme>docs\readme.md</readme>
1313
<requireLicenseAcceptance>false</requireLicenseAcceptance>
@@ -19,12 +19,12 @@
1919
<tags>azure web app moesif API analytics insights debug debugging log logging apm performance monitor monitoring restful rest graphql ethereum web3 json-rpc soap net java node python php go golang ruby microsoft</tags>
2020
<dependencies>
2121
<group>
22-
<dependency id="Moesif.Api" version="3.1.1" />
22+
<dependency id="Moesif.Api" version="3.1.2" />
2323
</group>
2424
<group targetFramework="net461">
25-
<dependency id="Microsoft.Owin" version="4.2.2" />
26-
<dependency id="System.ValueTuple" version="4.5.0" />-->
27-
<dependency id="Newtonsoft.Json" version="13.0.2" />-->
25+
<dependency id="Microsoft.Owin" version="4.2.2" />
26+
<!-- <dependency id="System.ValueTuple" version="4.5.0" />-->
27+
<!-- <dependency id="Newtonsoft.Json" version="13.0.2" />-->
2828
<dependency id="System.IdentityModel.Tokens.Jwt" version="6.35.0" />
2929
<dependency id="Microsoft.Extensions.Logging" version="3.1.32" />
3030
</group>

Moesif.Middleware/NetCore/Helpers/LoggerHelper.cs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//#define MOESIF_INSTRUMENT
1+
// #define MOESIF_INSTRUMENT
22

33
using System;
44
using System.IO;
@@ -182,32 +182,27 @@ public string Base64Encode(string plainText)
182182
public Tuple<object, string> Serialize(string data, string contentType, bool logBody, bool debug)
183183
{
184184
#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");
187188
_logger.LogError($@"
188189
Serialize Body: {data}");
189-
190-
long checkForEmptyDataAndInitObj = 0;
191-
long parseJsonData = 0;
192-
long parseBase64Data = 0;
193-
long exceptionBase64Data = 0;
194190
#endif
195191

196192
if (string.IsNullOrEmpty(data))
197193
{
198194
#if MOESIF_INSTRUMENT
199-
stopwatch.Stop();
200195
_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);
202198
#endif
203199
return new Tuple<object, string>(null, null);
204200
}
205201

206202
var reqBody = new object();
207203
string requestTransferEncoding = null;
208204
#if MOESIF_INSTRUMENT
209-
checkForEmptyDataAndInitObj = stopwatch.ElapsedMilliseconds;
210-
stopwatch.Restart();
205+
perfMetrics.Stop();
211206
#endif
212207
if (logBody && contentType != null && !(contentType.ToLower().StartsWith("multipart/form-data")))
213208
{
@@ -216,61 +211,43 @@ public Tuple<object, string> Serialize(string data, string contentType, bool lo
216211
// Only try parse if is JSON or looks like JSON
217212
if (contentType != null && contentType.ToLower().Contains("json") || data.StartsWith("{") || data.StartsWith("["))
218213
{
214+
#if MOESIF_INSTRUMENT
215+
perfMetrics.Start("parseJsonData");
216+
#endif
219217
reqBody = ApiHelper.JsonDeserialize<object>(data);
220218
requestTransferEncoding = "json";
221219
#if MOESIF_INSTRUMENT
222-
parseJsonData = stopwatch.ElapsedMilliseconds;
223-
stopwatch.Restart();
220+
perfMetrics.Stop();
224221
#endif
225222
}
226223
else
227224
{
228225
LogDebugMessage(debug, "About to parse Request body as Base64 encoding");
226+
#if MOESIF_INSTRUMENT
227+
perfMetrics.Start("parseBase64Data");
228+
#endif
229229
reqBody = Base64Encode(data);
230230
requestTransferEncoding = "base64";
231231
#if MOESIF_INSTRUMENT
232-
parseBase64Data = stopwatch.ElapsedMilliseconds;
233-
stopwatch.Restart();
232+
perfMetrics.Stop();
234233
#endif
235234
}
236235
}
237236
catch (Exception)
238237
{
239238
LogDebugMessage(debug, "About to parse Request body as Base64 encoding");
239+
#if MOESIF_INSTRUMENT
240+
perfMetrics.Start("exceptionBase64Data");
241+
#endif
240242
reqBody = Base64Encode(data);
241243
requestTransferEncoding = "base64";
242244
#if MOESIF_INSTRUMENT
243-
exceptionBase64Data = stopwatch.ElapsedMilliseconds;
244-
stopwatch.Restart();
245+
perfMetrics.Stop();
245246
#endif
246247
}
247248
}
248249
#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);
274251
#endif
275252

276253
return new Tuple<object, string>(reqBody, requestTransferEncoding);

0 commit comments

Comments
 (0)