lab2
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import json
|
||||
import urllib.request
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from statistics import median
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from client import Client, check
|
||||
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
COMPOSE = os.environ.get('COMPOSE_BIN', 'docker-compose')
|
||||
BASE_URL = os.environ.get('BASE_URL', 'http://localhost:8080')
|
||||
USERS = 4
|
||||
CHATS_PER_USER = 15
|
||||
REQUESTS_PER_USER = 250
|
||||
WORKERS = 8
|
||||
|
||||
for proxy_name in ('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy'):
|
||||
os.environ.pop(proxy_name, None)
|
||||
os.environ['NO_PROXY'] = 'localhost,127.0.0.1,::1'
|
||||
os.environ['no_proxy'] = os.environ['NO_PROXY']
|
||||
|
||||
|
||||
def restart_api(cache_enabled):
|
||||
env = dict(os.environ)
|
||||
env['REDIS_ENABLED'] = 'true'
|
||||
env['CACHE_ENABLED'] = 'true' if cache_enabled else 'false'
|
||||
subprocess.run(
|
||||
[COMPOSE, '-f', 'docker-compose.local.yaml', 'up', '-d', '--force-recreate', 'api'],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
Client(BASE_URL, timeout=2).call('GET', '/health/ready', schema='Health')
|
||||
return
|
||||
except Exception:
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError('API did not become ready after restart')
|
||||
|
||||
|
||||
def percentile(values, percent):
|
||||
values = sorted(values)
|
||||
index = max(0, (len(values) * percent + 99) // 100 - 1)
|
||||
return values[index]
|
||||
|
||||
|
||||
def benchmark(actors):
|
||||
requests = [(actor, i) for i in range(REQUESTS_PER_USER) for actor in actors]
|
||||
latencies = []
|
||||
failures = []
|
||||
started = time.monotonic()
|
||||
|
||||
def request(item):
|
||||
actor, _ = item
|
||||
request_started = time.monotonic()
|
||||
request = urllib.request.Request(
|
||||
BASE_URL + '/api/v1/chats?limit=50',
|
||||
headers={
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Bearer ' + actor['token'],
|
||||
},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
data = response.read()
|
||||
status = response.status
|
||||
check(status == 200,
|
||||
'unexpected HTTP status: ' + str(status) + ' body=' + data.decode())
|
||||
result = json.loads(data)
|
||||
check(len(result['items']) == 30, 'unexpected chat count')
|
||||
check(all(actor['id'] in chat['member_ids'] for chat in result['items']),
|
||||
'response contains another user chat')
|
||||
return time.monotonic() - request_started, None
|
||||
except Exception as exc:
|
||||
return 0, type(exc).__name__ + ': ' + str(exc)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=WORKERS) as pool:
|
||||
for latency, error in pool.map(request, requests):
|
||||
if error is None:
|
||||
latencies.append(latency)
|
||||
else:
|
||||
failures.append(error)
|
||||
|
||||
duration = time.monotonic() - started
|
||||
if failures:
|
||||
raise RuntimeError(
|
||||
'API benchmark failed: %d/%d requests; first error: %s'
|
||||
% (len(failures), len(requests), failures[0])
|
||||
)
|
||||
return {
|
||||
'requests': len(latencies),
|
||||
'workers': WORKERS,
|
||||
'duration_seconds': duration,
|
||||
'rps': len(latencies) / duration,
|
||||
'p50_ms': median(latencies) * 1000,
|
||||
'p95_ms': percentile(latencies, 95) * 1000,
|
||||
'p99_ms': percentile(latencies, 99) * 1000,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
client = Client(BASE_URL, timeout=10)
|
||||
actors = [client.user('perf_api_' + str(i)) for i in range(USERS)]
|
||||
|
||||
for index, owner in enumerate(actors):
|
||||
member = actors[(index + 1) % len(actors)]
|
||||
for chat_index in range(CHATS_PER_USER):
|
||||
client.call(
|
||||
'POST',
|
||||
'/api/v1/chats',
|
||||
expected=201,
|
||||
actor=owner,
|
||||
body={
|
||||
'title': 'API performance chat ' + str(index) + '-' + str(chat_index),
|
||||
'member_ids': [owner['id'], member['id']],
|
||||
},
|
||||
schema='Chat',
|
||||
)
|
||||
|
||||
results = []
|
||||
try:
|
||||
for trial in range(2):
|
||||
for enabled in (False, True):
|
||||
restart_api(enabled)
|
||||
for actor in actors:
|
||||
for _ in range(5):
|
||||
client.call('GET', '/api/v1/chats?limit=50', actor=actor, schema='ChatPage')
|
||||
print(
|
||||
'running trial %d, cache=%s, requests=%d' %
|
||||
(trial + 1, enabled, len(actors) * REQUESTS_PER_USER),
|
||||
flush=True,
|
||||
)
|
||||
result = benchmark(actors)
|
||||
result.update(trial=trial + 1, cache_enabled=enabled)
|
||||
results.append(result)
|
||||
print(json.dumps(result), flush=True)
|
||||
finally:
|
||||
restart_api(True)
|
||||
print(json.dumps({'results': results, 'users': USERS, 'chats': USERS * CHATS_PER_USER,
|
||||
'sessions_cached_in_both_modes': True}), flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit operator-authored hook; tokens remain in memory, volumes are never removed by this test."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
from client import Client, Failure, LAB, arguments, check, run
|
||||
|
||||
|
||||
def history(c, actor, chat):
|
||||
path = '/api/v1/chats/'+chat['id']+'/messages?limit=100'
|
||||
result = []
|
||||
seen = set()
|
||||
for _ in range(1000):
|
||||
page = c.call('GET',path,actor=actor,schema='MessagePage')['json']
|
||||
result.extend(page['items'])
|
||||
cursor = page['next_cursor']
|
||||
if cursor is None:
|
||||
return result
|
||||
check(cursor not in seen,'cursor cycle')
|
||||
seen.add(cursor)
|
||||
path = '/api/v1/chats/'+chat['id']+'/messages?limit=100&cursor='+quote(cursor,safe='')
|
||||
raise Failure('history pagination exceeded safety bound')
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--mode', choices=['persistence','failover'], required=True)
|
||||
p.add_argument('--action-script',required=True,help='Executable script authored by student; restarts stack or stops ONE API replica')
|
||||
args = arguments('Explicit restart/failover scenario; modifies only synthetic API data',configure)
|
||||
check(LAB>=2,'persistence requires lab2+')
|
||||
check(args.mode!='failover' or LAB>=3,'failover requires lab3+')
|
||||
script = Path(args.action_script).expanduser().resolve()
|
||||
check(script.is_file() and os.access(script,os.X_OK),'action script must exist and be executable (chmod +x)')
|
||||
check('.example.' not in script.name,'copy and implement the example hook first')
|
||||
c = Client(args.base_url,args.ca_file,timeout=2)
|
||||
alice,bob,eve,chat = c.bootstrap()
|
||||
sent = c.message(alice,chat,'Сохранить при отказе')
|
||||
original = sent['json']
|
||||
target = sent['headers'].get('X-Instance-Id','')
|
||||
attachment = content = None
|
||||
if LAB>=4:
|
||||
attachment,content = c.attachment(alice,chat)
|
||||
if LAB>=5:
|
||||
attachment = c.wait_ready(alice,attachment)
|
||||
env = dict(os.environ)
|
||||
env['TARGET_INSTANCE_ID'] = target
|
||||
observed = set()
|
||||
if args.mode=='failover':
|
||||
for _ in range(40):
|
||||
observed.add(c.call('GET','/api/v1/users/me',actor=alice,schema='User')['headers']['X-Instance-Id'])
|
||||
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
|
||||
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
|
||||
# No shell interpolation and no Docker commands in this test.
|
||||
proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
||||
started = time.monotonic()
|
||||
successes,failures,after_instances = [],0,[]
|
||||
try:
|
||||
if args.mode=='persistence':
|
||||
try:
|
||||
code = proc.wait(timeout=90)
|
||||
except subprocess.TimeoutExpired:
|
||||
raise Failure('restart hook exceeded 90 seconds') from None
|
||||
check(code==0,'restart hook failed (inspect your script locally)')
|
||||
deadline = time.monotonic()+90
|
||||
while True:
|
||||
try:
|
||||
c.call('GET','/health/ready',schema='Health')
|
||||
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
||||
break
|
||||
except Failure:
|
||||
check(time.monotonic()<deadline,'stack/session did not recover within 90 seconds')
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
first_recovery = None
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Запись во время отказа ' + key
|
||||
while time.monotonic()-started < 15:
|
||||
code = proc.poll()
|
||||
check(code is None or code==0,'stop-one hook failed (inspect your script locally)')
|
||||
check(code is not None or time.monotonic()-started<10,'stop-one hook must return within 10 seconds')
|
||||
try:
|
||||
response = c.message(alice,chat,text,key=key)
|
||||
msg = response['json']
|
||||
if code==0:
|
||||
if first_recovery is None:
|
||||
first_recovery = time.monotonic()-started
|
||||
after_instances.append(response['headers']['X-Instance-Id'])
|
||||
successes.append(msg)
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Запись во время отказа ' + key
|
||||
except Failure:
|
||||
failures += 1
|
||||
# Keep SAME key/body after an uncertain POST result.
|
||||
time.sleep(0.1)
|
||||
check(proc.poll()==0,'stop-one hook did not finish successfully')
|
||||
check(first_recovery is not None and first_recovery<=10,'writes did not recover within 10 seconds from hook start')
|
||||
check(len(after_instances)>=5,'too few successful writes after stop')
|
||||
check(target not in after_instances[-5:],'target replica still serves requests; hook must leave it stopped')
|
||||
print(json.dumps({'observed_instances_before':len(observed),'successful_writes':len(successes),'failed_attempts':failures,'first_recovery_seconds':round(first_recovery,3)},ensure_ascii=False))
|
||||
# Existing token, no re-login. Read all confirmations and reject duplicates.
|
||||
messages = history(c,bob,chat)
|
||||
ids = [m['id'] for m in messages]
|
||||
check(len(ids)==len(set(ids)),'duplicated message IDs in history')
|
||||
check(any(m==original for m in messages),'confirmed pre-failure message lost or altered')
|
||||
for msg in successes:
|
||||
check(any(m==msg for m in messages),'confirmed in-flight message lost or altered')
|
||||
# Bodies are unique in this scenario: an uncertain request may exist once, never twice.
|
||||
texts = [m['text'] for m in messages]
|
||||
check(len(texts)==len(set(texts)),'uncertain retry created duplicate message')
|
||||
if attachment:
|
||||
c.call('GET','/api/v1/attachments/'+attachment['id'],actor=bob,schema='Attachment')
|
||||
downloaded = c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',actor=bob)
|
||||
check(downloaded['bytes']==content,'attachment lost or changed across failure')
|
||||
print('PASS: '+args.mode+' preserved confirmed data and existing session. Hook scope needs operator evidence.')
|
||||
finally:
|
||||
if proc.poll() is None:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
|
||||
if __name__=='__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user