forked from mtusi2026/mtusi2026lab5
111 lines
5.2 KiB
Python
Executable File
111 lines
5.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Per-second view of one k6 run (requests.csv) plus the events of the experiment (events.json).
|
|
|
|
Writes per-second.csv and summary.json next to the input. An error is any response that is not 2xx
|
|
(including k6 status 0: connection reset or timeout). Times are seconds from the start of the load.
|
|
"""
|
|
import csv
|
|
import json
|
|
import math
|
|
import sys
|
|
from collections import Counter, defaultdict
|
|
from pathlib import Path
|
|
from urllib.parse import parse_qsl
|
|
|
|
run = Path(sys.argv[1])
|
|
events = json.loads((run / 'events.json').read_text()) if (run / 'events.json').exists() else {}
|
|
|
|
durations = [] # (t_ms, duration_ms, status)
|
|
served = [] # (t_ms, instance, status)
|
|
with open(run / 'requests.csv') as f:
|
|
for row in csv.DictReader(f):
|
|
if row['metric_name'] == 'http_req_duration' and row['scenario'] == 'load':
|
|
durations.append((int(row['timestamp']), float(row['metric_value']), int(row['status'] or 0)))
|
|
elif row['metric_name'] == 'served_by':
|
|
tags = dict(parse_qsl(row['extra_tags']))
|
|
served.append((int(row['timestamp']), tags.get('instance', 'none'), int(tags.get('code', 0))))
|
|
|
|
t0 = min(t for t, _, _ in durations)
|
|
rel = lambda ms: (ms - t0) / 1000
|
|
|
|
|
|
def percentile(values, p):
|
|
if not values:
|
|
return None
|
|
values = sorted(values)
|
|
return values[min(len(values) - 1, math.ceil(p / 100 * len(values)) - 1)]
|
|
|
|
|
|
per_second = defaultdict(lambda: {'ok': 0, 'errors': 0, 'durations': [], 'instances': Counter()})
|
|
for t, d, status in durations:
|
|
second = per_second[math.floor(rel(t))]
|
|
second['durations'].append(d)
|
|
if 200 <= status < 300:
|
|
second['ok'] += 1
|
|
else:
|
|
second['errors'] += 1
|
|
for t, instance, status in served:
|
|
if 200 <= status < 300:
|
|
per_second[math.floor(rel(t))]['instances'][instance] += 1
|
|
|
|
instances = sorted({i for s in per_second.values() for i in s['instances']})
|
|
with open(run / 'per-second.csv', 'w', newline='') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(['second', 'ok', 'errors', 'p99_ms', 'max_ms'] + instances)
|
|
for second in sorted(per_second):
|
|
s = per_second[second]
|
|
writer.writerow([second, s['ok'], s['errors'], round(percentile(s['durations'], 99), 1), round(max(s['durations']), 1)]
|
|
+ [s['instances'].get(i, 0) for i in instances])
|
|
|
|
errors = sorted(rel(t) for t, _, status in durations if not 200 <= status < 300)
|
|
seconds = max(per_second) + 1
|
|
summary = {
|
|
'requests': len(durations),
|
|
'ok': len(durations) - len(errors),
|
|
'errors': len(errors),
|
|
'error_codes': dict(Counter(str(status) for _, _, status in durations if not 200 <= status < 300)),
|
|
'ok_rps': round((len(durations) - len(errors)) / seconds, 1),
|
|
'p50_ms': round(percentile([d for _, d, _ in durations], 50), 1),
|
|
'p95_ms': round(percentile([d for _, d, _ in durations], 95), 1),
|
|
'p99_ms': round(percentile([d for _, d, _ in durations], 99), 1),
|
|
'max_ms': round(max(d for _, d, _ in durations), 1),
|
|
'requests_per_instance': dict(Counter(i for _, i, status in served if 200 <= status < 300)),
|
|
}
|
|
for name, value in events.items():
|
|
if isinstance(value, (int, float)):
|
|
summary[f'event_{name}_s'] = round(rel(value), 2)
|
|
if 'fault' in events:
|
|
fault = rel(events['fault'])
|
|
after = [e - fault for e in errors if e >= fault - 0.5]
|
|
summary['errors_after_fault'] = len(after)
|
|
summary['last_error_after_fault_s'] = round(max(after), 2) if after else None
|
|
target = events.get('target_instance')
|
|
end = rel(events['restore_begin']) if 'restore_begin' in events else float('inf')
|
|
last = [rel(t) - fault for t, i, status in served if i == target and fault <= rel(t) < end]
|
|
summary['target_last_response_after_fault_s'] = round(max(last), 2) if last else None
|
|
if 'restore_begin' in events and events.get('restored_instance'):
|
|
begin = rel(events['restore_begin'])
|
|
first = [rel(t) - begin for t, i, status in served
|
|
if i == events['restored_instance'] and 200 <= status < 300 and rel(t) >= begin]
|
|
summary['restored_replica_first_response_after_start_s'] = round(min(first), 2) if first else None
|
|
|
|
# HAProxy state changes (docker log timestamps): when the balancer took the target out and put it back.
|
|
log = run / 'haproxy.log'
|
|
if log.exists() and 'fault' in events:
|
|
from datetime import datetime
|
|
|
|
def at(line):
|
|
stamp = line.split()[0].rstrip('Z')
|
|
whole, _, fraction = stamp.partition('.')
|
|
return datetime.fromisoformat(whole + '+00:00').timestamp() * 1000 + float('0.' + (fraction or '0')) * 1000
|
|
|
|
changes = [(at(line), line) for line in log.read_text().splitlines() if 'Server api/' in line and '[WARNING]' not in line]
|
|
down = [t for t, line in changes if ('is DOWN' in line or 'going DOWN' in line) and t >= events['fault']]
|
|
summary['lb_marked_down_after_fault_s'] = round((min(down) - events['fault']) / 1000, 2) if down else None
|
|
if 'restore_begin' in events:
|
|
up = [t for t, line in changes if 'is UP, reason' in line and t >= events['restore_begin']]
|
|
summary['lb_marked_up_after_start_s'] = round((min(up) - events['restore_begin']) / 1000, 2) if up else None
|
|
|
|
(run / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False))
|
|
print(json.dumps(summary, indent=2, ensure_ascii=False))
|