File size: 2,537 Bytes
bad179a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | // Synthetic 3-day history -> store reload + analyzer. Expected output: analysis_expected.txt (availability may differ by 0.05).
#include "core/History.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <cstdio>
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
const QString dir = argv[1];
QDir(dir).removeRecursively();
const qint64 now = QDateTime::currentSecsSinceEpoch(), start = now - 3 * 86400;
{
HistoryStore st(dir);
for (qint64 t = start; t <= now; t += 30) {
float v[H_COUNT] = {};
const double days = (t - start) / 86400.0;
v[H_CPU] = 20 + 10 * ((t / 3600) % 24 > 8 && (t / 3600) % 24 < 18);
v[H_DISK] = 70 + 5 * days;
v[H_MEM] = (now - t < 6 * 3600) ? 50 + 2.0 * (6 - (now - t) / 3600.0) : 50;
v[H_LOAD] = 1; v[H_RX] = 1e6; v[H_TX] = 5e5;
st.add(t, v, !(t > now - 86400 && t < now - 86400 + 1800));
}
}
HistoryStore st(dir);
printf("reloaded raw=%d minute=%d hour=%d day=%d bytes=%lld\n", st.tier(Tier::Raw).size(), st.tier(Tier::Minute).size(), st.tier(Tier::Hour).size(), st.tier(Tier::Day).size(), st.diskBytes());
Tier used; auto r = st.range(now - 7 * 86400, now, &used);
printf("range 7d -> %s, %d pts\n", qPrintable(tierName(used)), r.size());
Metrics m; m.valid = true; m.cpuCount = 4; m.memPct = 62; m.maxDiskPct = 85; m.fullestMount = "/";
AutopilotConfig ap; ap.diskCleanup = true; ap.diskForecastDays = 5;
ProcTrend p; p.pid = 4242; p.name = "leaky"; p.minutesTracked = 45; p.rssNow = 900e6; p.rssGrowth = 400e6; p.rssGrowthPct = 80; p.rssMonotonic = 0.95;
ProcTrend q; q.pid = 777; q.name = "spinner"; q.cpuNow = 99; q.highCpuMinutes = 12;
ServiceTrend s1{"web.service", true, false, 1, 0}, s2{"old.service", true, true, 0, 0};
Analysis a = analyze(st, m, ap, {p, q}, {s1, s2}, true);
printf("health=%d grade=%s diskFullDays=%.2f slope/day=%.2f memSlope/h=%.2f r2=%.2f up24=%.2f up7=%.2f\n", a.health, qPrintable(a.grade), a.diskFullDays, a.diskSlopePerDay, a.memSlopePerHour, a.memTrendR2, a.up[0], a.up[1]);
for (const auto &f : a.findings) printf(" [%s] %s | auto=%d action=%s %s\n", qPrintable(f.severity), qPrintable(f.title), f.autoEligible, qPrintable(f.action), qPrintable(f.actionArg));
Stats s = stats(r, H_DISK);
printf("disk stats min=%.1f avg=%.1f p95=%.1f max=%.1f n=%d; capacity: %s\n", s.min, s.avg, s.p95, s.max, s.count, qPrintable(a.capacity.join(" | ")));
}
|