File size: 2,463 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
40
41
42
43
44
45
"""Python port of ref/analysis_test.cpp: synthetic 3-day history -> store reload + analyzer."""
import os, shutil, sys, time
sys.dont_write_bytecode = True
sys.path.insert(0, sys.argv[1])
from history import *
from metrics import Metrics
from model import AutopilotConfig

d = sys.argv[2]
shutil.rmtree(d, ignore_errors=True)
now = int(time.time()); start = now - 3 * 86400
st = Store(d)
t0 = time.time()
for t in range(start, now + 1, 30):
    v = [0.0] * H_COUNT
    days = (t - start) / 86400.0
    v[CPU] = 20 + 10 * (8 < (t // 3600) % 24 < 18)
    v[DISK] = 70 + 5 * days
    v[MEM] = 50 + 2.0 * (6 - (now - t) / 3600.0) if now - t < 6 * 3600 else 50
    v[LOAD] = 1; v[RX] = 1e6; v[TX] = 5e5
    st.add(t, v, not (now - 86400 < t < now - 86400 + 1800))
t1 = time.time()
st = Store(d)
print('reloaded raw=%d minute=%d hour=%d day=%d bytes=%d' % (*map(len, st.tiers), st.disk_bytes()))
r, used = st.span(now - 7 * 86400, now)
print('range 7d -> %s, %d pts' % (TIER_NAMES[used], len(r)))
m = Metrics(); m.valid = True; m.cpu_count = 4; m.mem_pct = 62; m.max_disk_pct = 85; m.fullest_mount = '/'
ap = AutopilotConfig(); ap.diskCleanup = True; ap.diskForecastDays = 5
p = ProcTrend(pid=4242, name='leaky', minutes=45, rss_now=900e6, rss_growth=400e6, rss_growth_pct=80, rss_monotonic=0.95)
q = ProcTrend(pid=777, name='spinner', cpu_now=99, high_cpu_minutes=12)
s1, s2 = ServiceTrend('web.service', True, False, 1, 0), ServiceTrend('old.service', True, True, 0, 0)
t2 = time.time()
a = analyze(st, m, ap, [p, q], [s1, s2], True)
t3 = time.time()
print('health=%d grade=%s diskFullDays=%.2f slope/day=%.2f memSlope/h=%.2f r2=%.2f up24=%.2f up7=%.2f' % (a.health, a.grade, a.disk_full_days, a.disk_slope_per_day, a.mem_slope_per_hour, a.mem_trend_r2, a.up[0], a.up[1]))
for f in a.findings:
    print('  [%s] %s | auto=%d action=%s %s' % (f.severity, f.title, f.auto, f.action, f.arg))
s = stats(r, DISK)
print('disk stats min=%.1f avg=%.1f p95=%.1f max=%.1f n=%d; capacity: %s' % (s.min, s.avg, s.p95, s.max, s.count, ' | '.join(a.capacity)))
print('# timing: write %.2fs analyze %.3fs' % (t1 - t0, t3 - t2), file=sys.stderr)
for t, name in enumerate(TIERS):
    size = os.path.getsize(os.path.join(d, name + '.bin'))
    assert open(os.path.join(d, name + '.bin'), 'rb').read(8) == b'SMH1\x0f\x00\x88\x00' and (size - 8) % 136 == 0 and (size - 8) // 136 == len(st.tiers[t]), name
print('# file layout ok: header 8 bytes + 136 bytes per record', file=sys.stderr)