Skip to content

Commit

Permalink
#188509201: Performance Improvement + Cleanup + Latest Middleware (#45)
Browse files Browse the repository at this point in the history
* Refactored metrics as CSV for easy collection. Also added a few more api for 75 / 150 kb response body size payload tests.

* Updated tests.

* Updated tests to use latest.
  • Loading branch information
praves77 authored Dec 7, 2024
1 parent 4b68aa1 commit fd83c86
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 25 deletions.
10 changes: 10 additions & 0 deletions LambdaExtension/LambdaExtension.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
20 changes: 10 additions & 10 deletions MoesifMiddlewareExample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNet8Example", "Moesif
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoesifNet6Example", "MoesifNet6Example\MoesifNet6Example.csproj", "{F248B0AC-68C5-46F8-BA5D-9E275ADC4F08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moesif.Middleware", "..\moesif-dotnet\Moesif.Middleware\Moesif.Middleware.csproj", "{2D2E12C2-4C03-4939-A0B7-4FDD367D29D3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moesif.Api", "..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj", "{B4A40605-57BA-4C5D-B68F-348717EF61B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moesif.Api", "..\moesifapi-csharp\Moesif.Api\Moesif.Api.csproj", "{1A2A5E0A-E7F2-4780-B253-6DE1ABBAB4FC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moesif.Middleware", "..\moesif-dotnet\Moesif.Middleware\Moesif.Middleware.csproj", "{E49606CF-77F8-48BA-B265-AA85BC980193}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -42,14 +42,14 @@ Global
{F248B0AC-68C5-46F8-BA5D-9E275ADC4F08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F248B0AC-68C5-46F8-BA5D-9E275ADC4F08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F248B0AC-68C5-46F8-BA5D-9E275ADC4F08}.Release|Any CPU.Build.0 = Release|Any CPU
{2D2E12C2-4C03-4939-A0B7-4FDD367D29D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D2E12C2-4C03-4939-A0B7-4FDD367D29D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D2E12C2-4C03-4939-A0B7-4FDD367D29D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D2E12C2-4C03-4939-A0B7-4FDD367D29D3}.Release|Any CPU.Build.0 = Release|Any CPU
{1A2A5E0A-E7F2-4780-B253-6DE1ABBAB4FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A2A5E0A-E7F2-4780-B253-6DE1ABBAB4FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A2A5E0A-E7F2-4780-B253-6DE1ABBAB4FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A2A5E0A-E7F2-4780-B253-6DE1ABBAB4FC}.Release|Any CPU.Build.0 = Release|Any CPU
{B4A40605-57BA-4C5D-B68F-348717EF61B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4A40605-57BA-4C5D-B68F-348717EF61B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4A40605-57BA-4C5D-B68F-348717EF61B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4A40605-57BA-4C5D-B68F-348717EF61B1}.Release|Any CPU.Build.0 = Release|Any CPU
{E49606CF-77F8-48BA-B265-AA85BC980193}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E49606CF-77F8-48BA-B265-AA85BC980193}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E49606CF-77F8-48BA-B265-AA85BC980193}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E49606CF-77F8-48BA-B265-AA85BC980193}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
86 changes: 84 additions & 2 deletions MoesifNet6Example/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,114 @@
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using MoesifNet6Example.Models;
using System.Threading;

namespace MoesifNet6Example.Controllers
{
[Route("api/[controller]")]
public class EmployeeController : ControllerBase
{
private const int MAX_SIZE_75_KB = 75;
private const int MAX_SIZE_150_KB = 150;

public EmployeeController()
{
Console.WriteLine("EmployeeController constructor called");
}

private static string GenerateLargeResponseBody(int sizeInKB)
{
const string baseString = "This is a test string. ";
int repetitions = (sizeInKB * 1024); // / baseString.Length;
return new string('A', repetitions); // Create a large string filled with 'A's
}

[HttpGet("{id}")]
public IActionResult GetByID(int id)
{
Console.WriteLine($"BEGIN: EmployeeController GetByID method called with id: {id}");
var employee = new Employee()
{
ID = id,
FirstName = "firstName",
FirstName = "No Delay",
LastName = "Very Long LastName From Moesif for API Awesomeness of Moesif",
DateOfBirth = DateTime.Now.AddYears(-30)
};

Console.WriteLine($"END : Employee object created: {employee.ID}, {employee.FirstName}, {employee.LastName}");

return Ok(employee);
}

[HttpGet("delay_random/{id}")]
public IActionResult GetEventId(int id)
{
var employee = new Employee()
{
ID = id,
FirstName = $"Get-Event-{id}",
LastName = $"Delayed-By-{id}-MS",
DateOfBirth = DateTime.Now.AddYears(-30)
};

Thread.Sleep(id); // 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";
string lastName = $"Delayed-By-{sleepTime}-MS";
if (id > 10)
{
lastName = GenerateLargeResponseBody(MAX_SIZE_75_KB);
}
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("payload150/{id}")]
public IActionResult GetSubsId(int id)
{
int sleepTime = 100; // in ms
string firstName = $"Get-{id}-{MAX_SIZE_150_KB}-KB-{sleepTime}-MS";
string lastName = $"Delayed-By-{sleepTime}-MS";
if (id > 10)
{
lastName = GenerateLargeResponseBody(MAX_SIZE_150_KB);
}

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]
public IActionResult Post([FromBody] Employee employee)
{

Console.WriteLine("Post method called");

return Ok(employee);
}
}
Expand Down
24 changes: 16 additions & 8 deletions MoesifNet6Example/MoesifNet6Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<DefineConstants>NET6_0_OR_GREATER</DefineConstants>
<DefineConstants>NETCORE</DefineConstants>
<!-- IMPORTANT please comment following lines before release build/publish -->
<!-- <DefineConstants>MOESIF_INSTRUMENT</DefineConstants>-->
</PropertyGroup>

<ItemGroup>
<Folder Include="publish\" />
<Folder Include="Settings\" />
Expand All @@ -15,16 +23,16 @@
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.10.0" />
<!-- <PackageReference Include="Microsoft.Owin" Version="4.2.2" /> -->
<!-- <PackageReference Include="Moesif.Middleware" Version="3.1.0"/>-->
<PackageReference Include="Moesif.Api" Version="3.1.1" />
<PackageReference Include="Moesif.Middleware" Version="3.1.2"/>
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\moesif-dotnet\Moesif.Middleware\Moesif.Middleware.csproj">
<ReferenceSourceTarget></ReferenceSourceTarget>
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
</ItemGroup>
<!-- <ItemGroup> -->
<!-- <ProjectReference Include="..\..\moesif-dotnet\Moesif.Middleware\Moesif.Middleware.csproj"> -->
<!-- <ReferenceSourceTarget></ReferenceSourceTarget> -->
<!-- <GlobalPropertiesToRemove></GlobalPropertiesToRemove> -->
<!-- </ProjectReference> -->
<!-- </ItemGroup> -->

</Project>
12 changes: 12 additions & 0 deletions MoesifNet6Example/Settings/MoesifOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ public bool isConfiguredMoesifApplicationId()
}
return !string.IsNullOrWhiteSpace(appId) && !appId.StartsWith("<");
}

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

public class MoesifOptionsParamNames
Expand Down
49 changes: 47 additions & 2 deletions MoesifNet6Example/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#define MOESIF_INSTRUMENT

using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json.Nodes;
using Amazon.Lambda.APIGatewayEvents;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -29,28 +34,68 @@ public void ConfigureServices(IServiceCollection services)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
#if MOESIF_INSTRUMENT
Console.WriteLine($"Begin: Configure");
#endif
var isLambda = false;
MoesifOptions mo = new MoesifOptions(Configuration);
ensureValidConfig(mo);
app.UseMiddleware<MoesifMiddleware>(mo.getMoesifOptions());
isLambda = mo.IsLambda();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

#if MOESIF_INSTRUMENT
Console.WriteLine($"Before: Configure / UseRouting");
#endif
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
var msg = $"Hello World! isLambd = {isLambda}";
Console.WriteLine($"Hit the home page: {msg}");
// if (isLambda)
// {
// var response = new APIGatewayProxyResponse
// {
// StatusCode = (int)HttpStatusCode.OK,
// Body = $"Welcome to Home! Request path: /",
// Headers = new Dictionary<string, string> { { "Content-Type", "text/json" } }
// };
// }
// else
// {
await context.Response.WriteAsync(new JsonObject
{
["msg"] = msg,
["zipcode"] = 94709
}.ToString());
// }
});

endpoints.MapGet("/foo", async context =>
{
// var hdrs = new Dictionary<string, string> { { "Content-Type", "text/json" } };
var response = new JsonObject
{
["StatusCode"] = (int)HttpStatusCode.OK,
["Body"] = $"Hello from Lambda! Request path: /foo"
};
await context.Response.WriteAsJsonAsync(response);
});

endpoints.MapControllerRoute(
name: "api",
pattern: "{controller}/{id}");
});

#if MOESIF_INSTRUMENT
Console.WriteLine($"End: Configure");
#endif
}

private static void ensureValidConfig(MoesifOptions mo)
Expand Down
7 changes: 4 additions & 3 deletions MoesifNet6Example/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"MoesifOptions": {
"MyTag": "ILT_Org_PK_App",
"ApplicationId": "<Your-Moesif-Application-Id>",
"LocalDebug": false,
"LogBody": true,
"RequestMaxBodySize": 100,
"ResponseMaxBodySize": 100,
"RequestMaxBodySize": 100000,
"ResponseMaxBodySize": 100000,
"LogBodyOutgoing": true,
"ApiVersion": "3.1.0",
"ApiVersion": "3.1.2",
"EnableBatching": true,
"BatchSize": 1,
"IsLambda": true
Expand Down

0 comments on commit fd83c86

Please sign in to comment.