(
host: str,
port: int,
path: str,
rng: random.Random,
read_bps: int,
chunk: int,
deadline: float,
test_start: float,
stats: dict,
)
| 480 | |
| 481 | |
| 482 | async def _slow_reader( |
| 483 | host: str, |
| 484 | port: int, |
| 485 | path: str, |
| 486 | rng: random.Random, |
| 487 | read_bps: int, |
| 488 | chunk: int, |
| 489 | deadline: float, |
| 490 | test_start: float, |
| 491 | stats: dict, |
| 492 | ) -> None: |
| 493 | while time.perf_counter() < deadline: |
| 494 | cmd = gen_bot_cmd(rng) |
| 495 | import urllib.parse as _up |
| 496 | |
| 497 | qs = _up.urlencode({"cmd": cmd}) |
| 498 | req = ( |
| 499 | f"GET {path}?{qs} HTTP/1.1\r\n" |
| 500 | f"Host: {host}:{port}\r\n" |
| 501 | f"User-Agent: loadtest-slowread\r\n" |
| 502 | f"Accept: */*\r\n" |
| 503 | f"Connection: close\r\n\r\n" |
| 504 | ) |
| 505 | t0 = time.perf_counter() |
| 506 | reader = writer = None |
| 507 | try: |
| 508 | reader, writer = await asyncio.wait_for( |
| 509 | asyncio.open_connection(host, port), timeout=10 |
| 510 | ) |
| 511 | writer.write(req.encode("ascii")) |
| 512 | await writer.drain() |
| 513 | stats["opened"] += 1 |
| 514 | sleep_s = max(0.001, chunk / max(1, read_bps)) |
| 515 | bytes_read = 0 |
| 516 | while time.perf_counter() < deadline: |
| 517 | try: |
| 518 | data = await asyncio.wait_for(reader.read(chunk), timeout=30) |
| 519 | except asyncio.TimeoutError: |
| 520 | stats["read_timeouts"] += 1 |
| 521 | break |
| 522 | if not data: |
| 523 | break |
| 524 | bytes_read += len(data) |
| 525 | await asyncio.sleep(sleep_s) |
| 526 | stats["completed"] += 1 |
| 527 | stats["total_bytes"] += bytes_read |
| 528 | stats["durations_s"].append(time.perf_counter() - t0) |
| 529 | except Exception as e: |
| 530 | stats["errors"] += 1 |
| 531 | stats["err_hist"][type(e).__name__] = ( |
| 532 | stats["err_hist"].get(type(e).__name__, 0) + 1 |
| 533 | ) |
| 534 | finally: |
| 535 | if writer is not None: |
| 536 | try: |
| 537 | writer.close() |
| 538 | await writer.wait_closed() |
| 539 | except Exception: |
no test coverage detected