Skip to content

Commit

Permalink
Configuration based ability to enable/disable moesif to avoid rebuild…
Browse files Browse the repository at this point in the history
…ing app for perf-testing (#46)

* Moved deprecated examples to depcrecated folder

* Configuration based ability to enable/disable moesif to avoid rebuilding app for perf-testing. Added more endpoints.

* Update appsettings.json
  • Loading branch information
praves77 authored Dec 13, 2024
1 parent fd83c86 commit 44600e7
Show file tree
Hide file tree
Showing 57 changed files with 424 additions and 376 deletions.
10 changes: 0 additions & 10 deletions LambdaExtension/LambdaExtension.csproj

This file was deleted.

18 changes: 0 additions & 18 deletions MoesifMiddlewareExample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNetCore2Example", "MoesifNetCore2Example\MoesifNetCore2Example.csproj", "{836C1D42-EC53-4E93-94BC-CA0234B81FA0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNetCore3Example", "MoesifNetCore3Example\MoesifNetCore3Example.csproj", "{E7F50E4B-8270-43CC-8470-633542583B17}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNet5Example", "MoesifNet5Example\MoesifNet5Example.csproj", "{C3A7293C-E822-42A5-ACE4-061242842066}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNet8Example", "MoesifNet8Example\MoesifNet8Example.csproj", "{8778A078-D55C-40E4-9AA5-80B002A8FE0A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNet6Example", "MoesifNet6Example\MoesifNet6Example.csproj", "{F248B0AC-68C5-46F8-BA5D-9E275ADC4F08}"
Expand All @@ -22,18 +16,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{836C1D42-EC53-4E93-94BC-CA0234B81FA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{836C1D42-EC53-4E93-94BC-CA0234B81FA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{836C1D42-EC53-4E93-94BC-CA0234B81FA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{836C1D42-EC53-4E93-94BC-CA0234B81FA0}.Release|Any CPU.Build.0 = Release|Any CPU
{E7F50E4B-8270-43CC-8470-633542583B17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7F50E4B-8270-43CC-8470-633542583B17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7F50E4B-8270-43CC-8470-633542583B17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7F50E4B-8270-43CC-8470-633542583B17}.Release|Any CPU.Build.0 = Release|Any CPU
{C3A7293C-E822-42A5-ACE4-061242842066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3A7293C-E822-42A5-ACE4-061242842066}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3A7293C-E822-42A5-ACE4-061242842066}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3A7293C-E822-42A5-ACE4-061242842066}.Release|Any CPU.Build.0 = Release|Any CPU
{8778A078-D55C-40E4-9AA5-80B002A8FE0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8778A078-D55C-40E4-9AA5-80B002A8FE0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8778A078-D55C-40E4-9AA5-80B002A8FE0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
62 changes: 55 additions & 7 deletions MoesifNet6Example/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,75 @@ public IActionResult GetByID(int id)
[HttpGet("delay_random/{id}")]
public IActionResult GetEventId(int id)
{
Random random = new Random();
int sleepTime = 300; // random.Next(1, 101);
string lastName = $"Delayed-By-{sleepTime}-MS";
var employee = new Employee()
{
ID = id,
FirstName = $"Get-Event-{id}",
LastName = $"Delayed-By-{id}-MS",
LastName = lastName,
DateOfBirth = DateTime.Now.AddYears(-30)
};

Thread.Sleep(id); // Sleeps for id milliseconds

Thread.Sleep(sleepTime); // Sleeps for id milliseconds

return Ok(employee);
}

[HttpPost("event/{id}")]
public IActionResult PostEventId(int id)
{
Random random = new Random();
int sleepTime = 1600; // random.Next(1, 101); // 500 - 1000 (Min - Median)
string firstName = $"Post-Event-{id}";
string lastName = $"Delayed-By-{sleepTime}-MS";
var employee = new Employee()
{
ID = id,
FirstName = firstName,
LastName = lastName,
DateOfBirth = DateTime.Now.AddYears(-30)
};

Thread.Sleep(sleepTime); // Sleeps for id milliseconds

return Ok(employee);
}

[HttpPost("readunread/{id}")]
public IActionResult PostReadUnreadId(int id)
{
Random random = new Random();
int sleepTime = 1300; // random.Next(1, 101); // 200 - 1000
string firstName = $"Post-ReadUnRead-{id}";
string lastName = $"Delayed-By-{sleepTime}-MS";
var employee = new Employee()
{
ID = id,
FirstName = firstName,
LastName = lastName,
DateOfBirth = DateTime.Now.AddYears(-30)
};

Thread.Sleep(sleepTime); // Sleeps for id milliseconds

return Ok(employee);
}

[HttpGet("payload75/{id}")]
public IActionResult GetNotificationId(int id)
{
int sleepTime = 100;
string firstName = $"Get-{id}-{MAX_SIZE_75_KB}-KB-{sleepTime}-MS";
int sleepTime = 8000; // random.Next(1, 101); // 300 - 9000
var size = 1;
string lastName = $"Delayed-By-{sleepTime}-MS";
if (id > 10)
{
size = MAX_SIZE_75_KB;
lastName = GenerateLargeResponseBody(MAX_SIZE_75_KB);
}
string firstName = $"Get-{id}-{size}-KB-{sleepTime}-MS";

var employee = new Employee()
{
ID = id,
Expand All @@ -86,13 +132,15 @@ public IActionResult GetNotificationId(int id)
[HttpGet("payload150/{id}")]
public IActionResult GetSubsId(int id)
{
int sleepTime = 100; // in ms
string firstName = $"Get-{id}-{MAX_SIZE_150_KB}-KB-{sleepTime}-MS";
int sleepTime = 3500; // random.Next(1, 101); // 300 - 4000
var size = 1;
string lastName = $"Delayed-By-{sleepTime}-MS";
if (id > 10)
{
size = MAX_SIZE_150_KB;
lastName = GenerateLargeResponseBody(MAX_SIZE_150_KB);
}
string firstName = $"Get-{id}-{MAX_SIZE_150_KB}-KB-{sleepTime}-MS";

var employee = new Employee()
{
Expand Down
14 changes: 14 additions & 0 deletions MoesifNet6Example/Settings/MoesifOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Dictionary<string, object> getMoesifOptions()
{
Dictionary<string, object> moesifOptions = new Dictionary<string, object>
{
{MoesifOptionsParamNames.IsMoesifEnabled, getConfigBool(MoesifOptionsParamNames.IsMoesifEnabled)},
{MoesifOptionsParamNames.ApplicationId, getConfigString(MoesifOptionsParamNames.ApplicationId)},
{MoesifOptionsParamNames.LocalDebug, getConfigBool(MoesifOptionsParamNames.LocalDebug)},
{MoesifOptionsParamNames.LogBody, getConfigBool(MoesifOptionsParamNames.LogBody)},
Expand Down Expand Up @@ -115,6 +116,18 @@ public bool IsLambda()
}
return isLambda;
}

public bool IsMoesifEnabled()
{
var moesifEnabled = true;
try {
moesifEnabled = (bool) getMoesifOptions().GetValueOrDefault(MoesifOptionsParamNames.IsMoesifEnabled);
}
catch (Exception ex){
Console.WriteLine("Error Reading Moesif Application Id in appsettings(.env).json: " + ex.Message );
}
return moesifEnabled;
}
}

public class MoesifOptionsParamNames
Expand All @@ -132,6 +145,7 @@ public class MoesifOptionsParamNames

// Read from appsettings.json
public static string IsLambda = "IsLambda";
public static string IsMoesifEnabled = "IsMoesifEnabled";

// Read from appsettings.json
public static string LogBodyOutgoing = "LogBodyOutgoing";
Expand Down
19 changes: 16 additions & 3 deletions MoesifNet6Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,24 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Console.WriteLine($"Begin: Configure");
#endif
var isLambda = false;
var isMoesifEnabled = true;
var msg = "";
MoesifOptions mo = new MoesifOptions(Configuration);
ensureValidConfig(mo);
app.UseMiddleware<MoesifMiddleware>(mo.getMoesifOptions());
isLambda = mo.IsLambda();

isLambda = mo.IsLambda();
isMoesifEnabled = mo.IsMoesifEnabled();
if (isMoesifEnabled)
{
app.UseMiddleware<MoesifMiddleware>(mo.getMoesifOptions());
msg = $"++++++ Moesif is Enabled because [IsMoesifEnabled = {isMoesifEnabled}] and [IsLambda = {isLambda}]";
}
else
{
msg = $"++++++ Moesif is Disabled because [IsMoesifEnabled = {isMoesifEnabled}] and [IsLambda = {isLambda}]";
}
Console.WriteLine($"{msg}");

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -56,7 +69,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapGet("/", async context =>
{
var msg = $"Hello World! isLambd = {isLambda}";
var msg = $"Hello World! isLambda = {isLambda}";
Console.WriteLine($"Hit the home page: {msg}");
// if (isLambda)
// {
Expand Down
5 changes: 3 additions & 2 deletions MoesifNet6Example/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"MoesifOptions": {
"IsMoesifEnabled": true,
"MyTag": "ILT_Org_PK_App",
"ApplicationId": "<Your-Moesif-Application-Id>",
"ApplicationId": "<MOESIF_APPLICATION_ID>",
"LocalDebug": false,
"LogBody": true,
"RequestMaxBodySize": 100000,
Expand All @@ -22,4 +23,4 @@
},
"AllowedHosts": "*"
}


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Settings\" />
<Folder Include="Controllers\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="4.2.2" />
<PackageReference Include="Moesif.Middleware" Version="1.4.7" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Settings\" />
<Folder Include="Controllers\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="4.2.2" />
<PackageReference Include="Moesif.Middleware" Version="1.4.7" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace MoesifNet5Example
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.ConfigureKestrel((context, options) => {
options.AllowSynchronousIO = true;
});
});
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace MoesifNet5Example
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.ConfigureKestrel((context, options) => {
options.AllowSynchronousIO = true;
});
});
}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 44600e7

Please sign in to comment.