Asynchronously generates requests at a specified rate with OPTIONAL burstiness. Args: input_requests: A list of input requests, each represented as a SampleRequest. request_rate: The rate at which requests are generated (requests/s). burs
(
input_requests: list[SampleRequest],
request_rate: float,
burstiness: float = 1.0,
)
| 410 | |
| 411 | |
| 412 | async def get_request( |
| 413 | input_requests: list[SampleRequest], |
| 414 | request_rate: float, |
| 415 | burstiness: float = 1.0, |
| 416 | ) -> AsyncGenerator[SampleRequest, None]: |
| 417 | """ |
| 418 | Asynchronously generates requests at a specified rate |
| 419 | with OPTIONAL burstiness. |
| 420 | |
| 421 | Args: |
| 422 | input_requests: |
| 423 | A list of input requests, each represented as a SampleRequest. |
| 424 | request_rate: |
| 425 | The rate at which requests are generated (requests/s). |
| 426 | burstiness (optional): |
| 427 | The burstiness factor of the request generation. |
| 428 | Only takes effect when request_rate is not inf. |
| 429 | Default value is 1, which follows a Poisson process. |
| 430 | Otherwise, the request intervals follow a gamma distribution. |
| 431 | A lower burstiness value (0 < burstiness < 1) results |
| 432 | in more bursty requests, while a higher burstiness value |
| 433 | (burstiness > 1) results in a more uniform arrival of requests. |
| 434 | """ |
| 435 | input_requests: Iterable[SampleRequest] = iter(input_requests) |
| 436 | |
| 437 | # Calculate scale parameter theta to maintain the desired request_rate. |
| 438 | assert burstiness > 0, f"A positive burstiness factor is expected, but given {burstiness}." |
| 439 | theta = 1.0 / (request_rate * burstiness) |
| 440 | |
| 441 | for request in input_requests: |
| 442 | yield request |
| 443 | |
| 444 | if request_rate == float("inf"): |
| 445 | # If the request rate is infinity, then we don't need to wait. |
| 446 | continue |
| 447 | |
| 448 | # Sample the request interval from the gamma distribution. |
| 449 | # If burstiness is 1, it follows exponential distribution. |
| 450 | interval = np.random.gamma(shape=burstiness, scale=theta) |
| 451 | # The next request will be sent after the interval. |
| 452 | await asyncio.sleep(interval) |
| 453 | |
| 454 | |
| 455 | def calculate_metrics( |