Реализовать ЛР 7 на оценку 4
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Measure concurrent bad passwords and healthy authenticated reads on the lab."""
|
||||
|
||||
import base64
|
||||
from collections import Counter
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from statistics import quantiles
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
LOGIN = '/api/v1/auth/sessions'
|
||||
READ = '/api/v1/users/me'
|
||||
|
||||
|
||||
def p95(samples):
|
||||
return round(1000 * quantiles(samples, n=20, method='inclusive')[18], 2)
|
||||
|
||||
|
||||
def main():
|
||||
def options(parser):
|
||||
parser.add_argument('--budget', type=int, default=20)
|
||||
|
||||
args = arguments(__doc__, options)
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
actor = client.user('budget')
|
||||
username = actor['registration']['username']
|
||||
invalid = base64.b64encode(f'{username}:wrong-synthetic-password'.encode()).decode()
|
||||
|
||||
def wrong():
|
||||
return client.call('POST', LOGIN, [401, 429], headers={'Authorization': 'Basic ' + invalid})
|
||||
|
||||
def normal():
|
||||
return client.call('GET', READ, 200, actor=actor, schema='User')
|
||||
|
||||
baseline = [normal()['elapsed'] for _ in range(20)]
|
||||
with ThreadPoolExecutor(max_workers=16) as pool:
|
||||
attempts, reads = [], []
|
||||
for _ in range(24):
|
||||
attempts.extend(pool.submit(wrong) for _ in range(3))
|
||||
reads.append(pool.submit(normal))
|
||||
results = [future.result() for future in attempts]
|
||||
regular = [future.result() for future in reads]
|
||||
|
||||
statuses = Counter(item['status'] for item in results)
|
||||
replicas = {item['headers']['X-Instance-Id'] for item in results}
|
||||
shared = [item for item in results if item['status'] == 429 and
|
||||
item['json']['error']['message'] == 'слишком много попыток входа']
|
||||
check(0 < statuses[401] < args.budget, 'Expected some password work within the shared budget')
|
||||
check(shared and len(replicas) == 2, 'Expected shared throttling with two replicas')
|
||||
check(all(int(item['headers']['Retry-After']) > 0 for item in results if item['status'] == 429),
|
||||
'Retry-After must be positive')
|
||||
print(json.dumps({
|
||||
'parallel_clients': 16,
|
||||
'bad_logins': len(results),
|
||||
'password_verifications': statuses[401],
|
||||
'shared_limit_429': len(shared),
|
||||
'slot_limit_429': statuses[429] - len(shared),
|
||||
'api_replicas': len(replicas),
|
||||
'healthy_reads_under_load': len(regular),
|
||||
'read_p95_ms_baseline': p95(baseline),
|
||||
'read_p95_ms_under_load': p95([item['elapsed'] for item in regular]),
|
||||
}))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user