forked from mtusi2026/mtusi2026lab4
lab4
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"""Редкие снимки Docker CPU/RAM и ожиданий PostgreSQL; overhead отражён в методике."""
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from scripts.stack import command, containers
|
||||
|
||||
|
||||
class Monitor:
|
||||
def __init__(self):
|
||||
self.samples = []
|
||||
self.stop = threading.Event()
|
||||
self.thread = threading.Thread(target=self.loop)
|
||||
|
||||
def __enter__(self):
|
||||
self.started = time.monotonic()
|
||||
self.thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.stop.set()
|
||||
self.thread.join(timeout=120)
|
||||
|
||||
def loop(self):
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
ids = [r['id'] for r in containers() if r['running'] and r['service'] != 'migrate']
|
||||
raw = command(['docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids])
|
||||
stats = [json.loads(line) for line in raw.splitlines()]
|
||||
query = "SELECT coalesce(json_agg(x),'[]') FROM (SELECT state,wait_event_type," \
|
||||
"wait_event,count(*) FROM pg_stat_activity WHERE backend_type='client backend' " \
|
||||
"AND pid<>pg_backend_pid() GROUP BY state,wait_event_type,wait_event) x"
|
||||
waits = json.loads(command(['docker', 'compose', 'exec', '-T', 'db',
|
||||
'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query]))
|
||||
pools = []
|
||||
for row in containers('api'):
|
||||
if row['running']:
|
||||
output = command(['docker', 'exec', row['id'], 'python', '-c',
|
||||
'import urllib.request; print(urllib.request.urlopen("http://localhost:8080/internal/pool",timeout=3).read().decode())'])
|
||||
pools.append(dict(instance=row['hostname'], stats=json.loads(output)))
|
||||
self.samples.append(dict(seconds=round(time.monotonic()-self.started, 3),
|
||||
docker=stats, postgres=waits, pools=pools))
|
||||
except Exception as exc:
|
||||
self.samples.append(dict(seconds=round(time.monotonic()-self.started, 3), error=str(exc)))
|
||||
self.stop.wait(3)
|
||||
Reference in New Issue
Block a user