Skip to content

Commit

Permalink
#188509201 [Part 2 of 3] : Performance improvement (#102)
Browse files Browse the repository at this point in the history
* Performance improvement + Performance Metrics + Version Bump.

* Refactor + Code Cleanup

* Refactor + Code Cleanup

* Refactor + Code Cleanup + Use latest base sdk

* Fix nuspec file.
  • Loading branch information
praves77 authored Dec 16, 2024
1 parent 61132e4 commit dd9d7ea
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 375 deletions.
2 changes: 1 addition & 1 deletion Moesif.Middleware/Helpers/AppConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #define MOESIF_INSTRUMENT
//#define MOESIF_INSTRUMENT
using System;
using System.Linq;
using System.Collections.Generic;
Expand Down
8 changes: 4 additions & 4 deletions Moesif.Middleware/Helpers/GovernanceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #define MOESIF_INSTRUMENT
//#define MOESIF_INSTRUMENT

using System;
using Moesif.Api;
Expand Down Expand Up @@ -68,11 +68,11 @@ public static bool enforceGovernaceRule(EventModel eventModel, Governance govern
var matchingRules = new List<(Rule, GovernanceRule)>();
var matchingUser = findMatchingEntityRule(eventModel.UserId, "user", governace, config, requestMap);
matchingRules.AddRange(matchingUser);


var matchingCompany = findMatchingEntityRule(eventModel.CompanyId, "company", governace, config, requestMap);
matchingRules.AddRange(matchingCompany);


var regexRules = new List<GovernanceRule>();
var matchingRegexRules = new List<(Rule, GovernanceRule)>();
Expand Down
8 changes: 6 additions & 2 deletions Moesif.Middleware/Helpers/Tasks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
//#define MOESIF_INSTRUMENT

using System;
using System.Linq;
using System.Collections.Generic;
using Moesif.Api;
Expand Down Expand Up @@ -73,7 +75,9 @@ public static async Task AsyncClientCreateEvent(MoesifApiClient client, Concurre
governanceEvent.Set();
logger.LogDebug("GoveranceRule has changed at {time}", DateTime.UtcNow);
}

#if MOESIF_INSTRUMENT
logger.LogError($"Events [{batchEvents.Count}] sent successfully to Moesif at {DateTime.UtcNow}");
#endif
logger.LogDebug("Events sent successfully to Moesif at {time}", DateTime.UtcNow);
}
catch (Exception e)
Expand Down
8 changes: 4 additions & 4 deletions Moesif.Middleware/Moesif.Middleware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moesif.Api" Version="3.1.1" />
<PackageReference Include="Moesif.Api" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.32" />
<!-- <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.0" /> -->
<!-- <PackageReference Include="Microsoft.Owin" Version="4.2.2">-->
Expand Down Expand Up @@ -59,9 +59,9 @@
<ItemGroup>
<Compile Remove="Helpers\ClientIp.cs" />
</ItemGroup>
<!-- <ItemGroup> -->
<!-- <ProjectReference Include="..\..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj" /> -->
<!-- </ItemGroup> -->
<!-- <ItemGroup> -->
<!-- <ProjectReference Include="..\..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj" /> -->
<!-- </ItemGroup> -->

<!-- <ItemGroup> -->
<!-- <None Remove="Microsoft.Owin" /> -->
Expand Down
14 changes: 7 additions & 7 deletions Moesif.Middleware/Moesif.Middleware.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<package >
<metadata>
<id>Moesif.Middleware</id>
<version>3.1.2</version>
<version>3.1.3</version>
<title>MoesifMiddleware</title>
<authors>Moesif</authors>
<owners>Moesif</owners>
<owners>Moesif</owners>
<license type="expression">Apache-2.0</license>
<projectUrl>https://www.moesif.com</projectUrl>
<projectUrl>https://www.moesif.com</projectUrl>
<iconUrl>https://cdn.moesif.com/images/icon.png</iconUrl>
<readme>docs\readme.md</readme>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand All @@ -19,12 +19,12 @@
<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>
<dependencies>
<group>
<dependency id="Moesif.Api" version="3.1.1" />
<dependency id="Moesif.Api" version="3.1.2" />
</group>
<group targetFramework="net461">
<dependency id="Microsoft.Owin" version="4.2.2" />
<dependency id="System.ValueTuple" version="4.5.0" />-->
<dependency id="Newtonsoft.Json" version="13.0.2" />-->
<dependency id="Microsoft.Owin" version="4.2.2" />
<!-- <dependency id="System.ValueTuple" version="4.5.0" />-->
<!-- <dependency id="Newtonsoft.Json" version="13.0.2" />-->
<dependency id="System.IdentityModel.Tokens.Jwt" version="6.35.0" />
<dependency id="Microsoft.Extensions.Logging" version="3.1.32" />
</group>
Expand Down
63 changes: 20 additions & 43 deletions Moesif.Middleware/NetCore/Helpers/LoggerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define MOESIF_INSTRUMENT
// #define MOESIF_INSTRUMENT

using System;
using System.IO;
Expand Down Expand Up @@ -182,32 +182,27 @@ public string Base64Encode(string plainText)
public Tuple<object, string> Serialize(string data, string contentType, bool logBody, bool debug)
{
#if MOESIF_INSTRUMENT
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var logStage = false;
var perfMetrics = new PerformanceMetrics("Serialize", logStage);
perfMetrics.Start("checkForEmptyDataAndInitObj");
_logger.LogError($@"
Serialize Body: {data}");

long checkForEmptyDataAndInitObj = 0;
long parseJsonData = 0;
long parseBase64Data = 0;
long exceptionBase64Data = 0;
#endif

if (string.IsNullOrEmpty(data))
{
#if MOESIF_INSTRUMENT
stopwatch.Stop();
_logger.LogError($@"
Exiting Serialize Body as empty data with time: {stopwatch.ElapsedMilliseconds} ms");
Exiting Serialize Body as empty data with time");
perfMetrics.PrintMetrics(Console.WriteLine);
#endif
return new Tuple<object, string>(null, null);
}

var reqBody = new object();
string requestTransferEncoding = null;
#if MOESIF_INSTRUMENT
checkForEmptyDataAndInitObj = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
perfMetrics.Stop();
#endif
if (logBody && contentType != null && !(contentType.ToLower().StartsWith("multipart/form-data")))
{
Expand All @@ -216,61 +211,43 @@ public Tuple<object, string> Serialize(string data, string contentType, bool lo
// Only try parse if is JSON or looks like JSON
if (contentType != null && contentType.ToLower().Contains("json") || data.StartsWith("{") || data.StartsWith("["))
{
#if MOESIF_INSTRUMENT
perfMetrics.Start("parseJsonData");
#endif
reqBody = ApiHelper.JsonDeserialize<object>(data);
requestTransferEncoding = "json";
#if MOESIF_INSTRUMENT
parseJsonData = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
perfMetrics.Stop();
#endif
}
else
{
LogDebugMessage(debug, "About to parse Request body as Base64 encoding");
#if MOESIF_INSTRUMENT
perfMetrics.Start("parseBase64Data");
#endif
reqBody = Base64Encode(data);
requestTransferEncoding = "base64";
#if MOESIF_INSTRUMENT
parseBase64Data = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
perfMetrics.Stop();
#endif
}
}
catch (Exception)
{
LogDebugMessage(debug, "About to parse Request body as Base64 encoding");
#if MOESIF_INSTRUMENT
perfMetrics.Start("exceptionBase64Data");
#endif
reqBody = Base64Encode(data);
requestTransferEncoding = "base64";
#if MOESIF_INSTRUMENT
exceptionBase64Data = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
perfMetrics.Stop();
#endif
}
}
#if MOESIF_INSTRUMENT
stopwatch.Stop();
var strHeader = string.Concat(
"ExitingSerializeBody,",
"checkForEmptyDataAndInitObj,",
"parseJsonData,",
"parseBase64Data,",
"exceptionBase64Data"
);
var strTimes = string.Concat(
$"{checkForEmptyDataAndInitObj + parseJsonData + parseBase64Data + exceptionBase64Data + stopwatch.ElapsedMilliseconds},",
$"{checkForEmptyDataAndInitObj},",
$"{parseJsonData},",
$"{parseBase64Data},",
$"{exceptionBase64Data}"
);
_logger.LogError($@"
{strHeader}
{strTimes}
");
// _logger.LogError($@"
// Exiting Serialize Body with time: {checkForEmptyDataAndInitObj + parseJsonData + parseBase64Data + exceptionBase64Data + stopwatch.ElapsedMilliseconds} ms
// checkForEmptyDataAndInitObj took: {checkForEmptyDataAndInitObj} ms
// parseJsonData took: {parseJsonData} ms
// parseBase64Data took: {parseBase64Data} ms
// exceptionBase64Data took: {exceptionBase64Data} ms");
perfMetrics.PrintMetrics(Console.WriteLine);
#endif

return new Tuple<object, string>(reqBody, requestTransferEncoding);
Expand Down
Loading

0 comments on commit dd9d7ea

Please sign in to comment.