Skip to content

Commit 3c916e0

Browse files
committed
Spread server starts via a poisson process
1 parent 4b3b55f commit 3c916e0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

simulate.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import random
23
import argparse
34
import asyncio
45
from enum import Enum
@@ -9,7 +10,7 @@
910
from datetime import datetime
1011
from functools import partial
1112
import time
12-
from typing import List
13+
from typing import Any, Coroutine, List
1314

1415
import aiohttp
1516
import aiometer
@@ -160,6 +161,13 @@ async def start_named_server(
160161
# Some kinda error
161162
resp.raise_for_status()
162163

164+
def poisson_distribution_wait_times(rate: float, count: int) -> list[float]:
165+
wait_times = []
166+
t = 0
167+
while(len(wait_times) < count):
168+
t += random.expovariate(rate)
169+
wait_times.append(t)
170+
return wait_times
163171

164172
async def payload(
165173
session: aiohttp.ClientSession,
@@ -193,13 +201,17 @@ async def payload(
193201

194202
print(f"{server.servername}: {timing_info}")
195203

204+
async def delay(duration: float, callable: Coroutine) -> Any:
205+
await asyncio.sleep(duration)
206+
return await callable
196207

197208
async def main():
198209
argparser = argparse.ArgumentParser()
199210
argparser.add_argument("hub_url", help="Full URL to the JupyterHub to test against")
200211
argparser.add_argument("server_prefix", help="Prefix used for named servers started in this run")
201212
argparser.add_argument("username", help="Name of the user")
202213
argparser.add_argument("servers_count", type=int, help="Number of servers to start")
214+
argparser.add_argument("server_startup_rate", type=int, help="Number of servers to start per minute")
203215
# FIXME: This shouldn't be here.
204216
argparser.add_argument(
205217
"--max-concurrency",
@@ -233,10 +245,12 @@ async def main():
233245
Server(f"{args.server_prefix}-{i}", args.username, HubAccess(hub_url, token))
234246
for i in range(args.servers_count)
235247
]
248+
delays = poisson_distribution_wait_times(args.server_startup_rate / 60, args.servers_count)
249+
print(delays)
236250
await aiometer.run_all(
237251
[
238-
partial(payload, session, browser, token, nbgitpuller_url, profile_options, server)
239-
for server in servers_to_start
252+
partial(delay, duration, payload(session, browser, token, nbgitpuller_url, profile_options, server))
253+
for server, duration in zip(servers_to_start, delays)
240254
],
241255
max_at_once=args.max_concurrency,
242256
)

0 commit comments

Comments
 (0)