This commit is contained in:
2020-05-25 15:01:04 +00:00
parent d13c1e42df
commit e1934e46c4

View File

@@ -16,8 +16,14 @@ namespace AyaNova.Biz
internal static class CoreJobMetricsSnapshot internal static class CoreJobMetricsSnapshot
{ {
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobMetricsSnapshot"); private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobMetricsSnapshot");
private static Process _process = Process.GetCurrentProcess();
private static TimeSpan _oldCPUTime = TimeSpan.Zero;
private static DateTime _lastSnapshot = DateTime.UtcNow;
private static DateTime _lastRpsTime = DateTime.UtcNow;
private static double _cpu = 0, _rps = 0;
private static DateTime lastSnapshot = DateTime.MinValue;
private static TimeSpan tsOneMinute = new TimeSpan(0, 1, 0); private static TimeSpan tsOneMinute = new TimeSpan(0, 1, 0);
private static TimeSpan tsOneHour = new TimeSpan(1, 0, 0); private static TimeSpan tsOneHour = new TimeSpan(1, 0, 0);
private static TimeSpan ts24Hours = new TimeSpan(24, 0, 0); private static TimeSpan ts24Hours = new TimeSpan(24, 0, 0);
@@ -27,6 +33,8 @@ namespace AyaNova.Biz
// //
public static async Task DoJobAsync(AyContext ct) public static async Task DoJobAsync(AyContext ct)
{ {
//https://github.com/sebastienros/memoryleak/blob/master/src/MemoryLeak/MemoryLeak/Controllers/DiagnosticsController.cs
//Gather stats, output to database but only every minute or more //Gather stats, output to database but only every minute or more
/* /*
//TODO: figure out teh best format to store it in based on what I need at the client end //TODO: figure out teh best format to store it in based on what I need at the client end
@@ -35,8 +43,10 @@ namespace AyaNova.Biz
(test storing null in two columns results in same size so no saving) (test storing null in two columns results in same size so no saving)
// todo: store data using Postgres REAL / c# float datatype, is 38mb vs 55 for double precision with one year 10 column test data // todo: store data using Postgres REAL / c# float datatype, is 38mb vs 55 for double precision with one year 10 column test data
what others track:
https://www.dynatrace.com/technologies/net-monitoring/#&gid=0&pid=1
https://docs.microsoft.com/en-us/aspnet/core/performance/memory?view=aspnetcore-3.1
Make a chart at client with test data from digital ocean to play with Make a chart at client with test data from digital ocean to play with
try to replicate their stuff to learn how to best do it try to replicate their stuff to learn how to best do it
@@ -138,7 +148,7 @@ from generate_series(1, 525600) s(i)
*/ */
//Nothing is gathered less than one minute frequency //Nothing is gathered less than one minute frequency
if (!DateUtil.IsAfterDuration(lastSnapshot, tsOneMinute)) if (!DateUtil.IsAfterDuration(_lastSnapshot, tsOneMinute))
return; return;
log.LogTrace("Starting metrics snapshot"); log.LogTrace("Starting metrics snapshot");
@@ -147,24 +157,48 @@ from generate_series(1, 525600) s(i)
//ONE MINUTE SNAPS //ONE MINUTE SNAPS
// //
//Gather core metrics here var now = DateTime.UtcNow;
using (Process proc = Process.GetCurrentProcess()) _process.Refresh();
{
//PHYSICAL MEMORY //CPU
// metrics.Measure.Gauge.SetValue(MetricsRegistry.PhysicalMemoryGauge, process.WorkingSet64); var cpuElapsedTime = now.Subtract(_lastSnapshot).TotalMilliseconds;
var newCPUTime = _process.TotalProcessorTime;
var elapsedCPU = (newCPUTime - _oldCPUTime).TotalMilliseconds;
_cpu = elapsedCPU * 100 / Environment.ProcessorCount / cpuElapsedTime;
_oldCPUTime = newCPUTime;
//PRIVATE BYTES //MEMORY
// metrics.Measure.Gauge.SetValue(MetricsRegistry.PrivateBytesGauge, process.PrivateMemorySize64); // The memory occupied by objects.
} var Allocated = GC.GetTotalMemory(false);
// The working set includes both shared and private data. The shared data includes the pages that contain all the
// instructions that the process executes, including instructions in the process modules and the system libraries.
var WorkingSet = _process.WorkingSet64;
// The value returned by this property represents the current size of memory used by the process, in bytes, that
// cannot be shared with other processes.
var PrivateBytes = _process.PrivateMemorySize64;
// The number of generation 0 collections
var Gen0 = GC.CollectionCount(0);
// The number of generation 1 collections
var Gen1 = GC.CollectionCount(1);
// The number of generation 2 collections
var Gen2 = GC.CollectionCount(2);
var CPU=_cpu;
//write to db
MetricMM mm = new MetricMM();
///////////////////////////////////////////// /////////////////////////////////////////////
//EVERY HOUR SNAPS //EVERY HOUR SNAPS
// //
if (DateUtil.IsAfterDuration(lastSnapshot, tsOneHour)) if (DateUtil.IsAfterDuration(_lastSnapshot, tsOneHour))
{ {
//RECORDS IN TABLE //RECORDS IN TABLE
//Only do this once per hour //Only do this once per hour
@@ -207,7 +241,7 @@ from generate_series(1, 525600) s(i)
///////////////////////////////////////////// /////////////////////////////////////////////
//ONCE A DAY SNAPS //ONCE A DAY SNAPS
// //
if (DateUtil.IsAfterDuration(lastSnapshot, ts24Hours)) if (DateUtil.IsAfterDuration(_lastSnapshot, ts24Hours))
{ {
//FILES ON DISK //FILES ON DISK
log.LogTrace("Files on disk information"); log.LogTrace("Files on disk information");
@@ -226,7 +260,7 @@ from generate_series(1, 525600) s(i)
} }
lastSnapshot = DateTime.UtcNow; _lastSnapshot = now;
//just to hide compiler warning for now //just to hide compiler warning for now