Skip to content

Commit

Permalink
GC Infor Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Sep 16, 2023
1 parent cc2e18e commit af2d85b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Globalization;

namespace ClassifiedAds.Infrastructure.Web.Endpoints;

/// <summary>
/// https://learn.microsoft.com/en-us/dotnet/api/system.gc
/// </summary>
public static class GcInforEndpoint
{
public static void MapGcInforEndpoint(this IEndpointRouteBuilder builder, string path = "/gcinfor")
{
builder.MapGet(path, () =>
{
var gcMemoryInfo = GC.GetGCMemoryInfo();
return Results.Ok(new
{
Timestamp = DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff zzz", CultureInfo.InvariantCulture),
MachineName = Environment.MachineName,
ProcessId = Environment.ProcessId,
TotalMemory = GC.GetTotalMemory(false),
Gen0Collections = GC.CollectionCount(0),
Gen1Collections = GC.CollectionCount(1),
Gen2Collections = GC.CollectionCount(2),
TotalPauseDuration = GC.GetTotalPauseDuration(),
GCMemoryInfo = new
{
gcMemoryInfo.HighMemoryLoadThresholdBytes,
gcMemoryInfo.MemoryLoadBytes,
gcMemoryInfo.HeapSizeBytes,
gcMemoryInfo.FragmentedBytes,
gcMemoryInfo.PauseTimePercentage,
gcMemoryInfo.PinnedObjectsCount
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Globalization;

namespace ClassifiedAds.Infrastructure.Web.Endpoints;

/// <summary>
/// https://learn.microsoft.com/en-us/dotnet/api/system.gc
/// </summary>
public static class GcInforEndpoint
{
public static void MapGcInforEndpoint(this IEndpointRouteBuilder builder, string path = "/gcinfor")
{
builder.MapGet(path, () =>
{
var gcMemoryInfo = GC.GetGCMemoryInfo();
return Results.Ok(new
{
Timestamp = DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff zzz", CultureInfo.InvariantCulture),
MachineName = Environment.MachineName,
ProcessId = Environment.ProcessId,
TotalMemory = GC.GetTotalMemory(false),
Gen0Collections = GC.CollectionCount(0),
Gen1Collections = GC.CollectionCount(1),
Gen2Collections = GC.CollectionCount(2),
TotalPauseDuration = GC.GetTotalPauseDuration(),
GCMemoryInfo = new
{
gcMemoryInfo.HighMemoryLoadThresholdBytes,
gcMemoryInfo.MemoryLoadBytes,
gcMemoryInfo.HeapSizeBytes,
gcMemoryInfo.FragmentedBytes,
gcMemoryInfo.PauseTimePercentage,
gcMemoryInfo.PinnedObjectsCount
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Globalization;

namespace ClassifiedAds.Infrastructure.Web.Endpoints;

/// <summary>
/// https://learn.microsoft.com/en-us/dotnet/api/system.gc
/// </summary>
public static class GcInforEndpoint
{
public static void MapGcInforEndpoint(this IEndpointRouteBuilder builder, string path = "/gcinfor")
{
builder.MapGet(path, () =>
{
var gcMemoryInfo = GC.GetGCMemoryInfo();
return Results.Ok(new
{
Timestamp = DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff zzz", CultureInfo.InvariantCulture),
MachineName = Environment.MachineName,
ProcessId = Environment.ProcessId,
TotalMemory = GC.GetTotalMemory(false),
Gen0Collections = GC.CollectionCount(0),
Gen1Collections = GC.CollectionCount(1),
Gen2Collections = GC.CollectionCount(2),
TotalPauseDuration = GC.GetTotalPauseDuration(),
GCMemoryInfo = new
{
gcMemoryInfo.HighMemoryLoadThresholdBytes,
gcMemoryInfo.MemoryLoadBytes,
gcMemoryInfo.HeapSizeBytes,
gcMemoryInfo.FragmentedBytes,
gcMemoryInfo.PauseTimePercentage,
gcMemoryInfo.PinnedObjectsCount
}
});
});
}
}
1 change: 1 addition & 0 deletions src/Monolith/ClassifiedAds.WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,6 @@

app.MapProcessInforEndpoint();
app.MapThreadPoolInforEndpoint();
app.MapGcInforEndpoint();

app.Run();

0 comments on commit af2d85b

Please sign in to comment.