Skip to content

Commit 784581d

Browse files
Add some memory analysis
1 parent bb8726c commit 784581d

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckDirtyMemory.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
2828
lastDirtyMemoryReads.RemoveAt(lastDirtyMemoryReads.Count - 1);
2929
}
3030

31-
// evaluate the trends
32-
// if the amount of dirty memory is constantly growing log a warning and fail the check
31+
if (lastDirtyMemoryReads.Count > 3 && AnalyzeTrendUsingRegression(lastDirtyMemoryReads) == TrendDirection.Increasing) // Three means we'll be observing for 15 minutes before calculating the trend
32+
{
33+
// log a warning and fail the check
34+
}
3335

3436
return CheckResult.Pass;
3537
}
@@ -45,4 +47,53 @@ async Task<MemoryInformationRetriever> GetMemoryRetriever(CancellationToken canc
4547
}
4648
return _retriever;
4749
}
50+
51+
static TrendDirection AnalyzeTrendUsingRegression(List<int> values)
52+
{
53+
if (values == null || values.Count <= 1)
54+
{
55+
throw new ArgumentException("Need at least two values to determine a trend");
56+
}
57+
58+
// Calculate slope using linear regression
59+
double n = values.Count;
60+
double sumX = 0;
61+
double sumY = 0;
62+
double sumXY = 0;
63+
double sumXX = 0;
64+
65+
for (int i = 0; i < values.Count; i++)
66+
{
67+
double x = i;
68+
double y = values[i];
69+
70+
sumX += x;
71+
sumY += y;
72+
sumXY += x * y;
73+
sumXX += x * x;
74+
}
75+
76+
double slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
77+
78+
// Determine trend based on slope
79+
if (Math.Abs(slope) < 0.001) // Small threshold to handle floating-point precision
80+
{
81+
return TrendDirection.Flat;
82+
}
83+
84+
if (slope > 0)
85+
{
86+
return TrendDirection.Increasing;
87+
}
88+
89+
return TrendDirection.Decreasing;
90+
}
91+
92+
enum TrendDirection
93+
{
94+
Increasing,
95+
Decreasing,
96+
Flat,
97+
Mixed
98+
}
4899
}

0 commit comments

Comments
 (0)