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,
)
| 115 | |
| 116 | |
| 117 | async def get_request( |
| 118 | input_requests: list[SampleRequest], |
| 119 | request_rate: float, |
| 120 | burstiness: float = 1.0, |
| 121 | ) -> AsyncGenerator[SampleRequest, None]: |
| 122 | """ |
| 123 | Asynchronously generates requests at a specified rate |
| 124 | with OPTIONAL burstiness. |
| 125 | |
| 126 | Args: |
| 127 | input_requests: |
| 128 | A list of input requests, each represented as a SampleRequest. |
| 129 | request_rate: |
| 130 | The rate at which requests are generated (requests/s). |
| 131 | burstiness (optional): |
| 132 | The burstiness factor of the request generation. |
| 133 | Only takes effect when request_rate is not inf. |
| 134 | Default value is 1, which follows a Poisson process. |
| 135 | Otherwise, the request intervals follow a gamma distribution. |
| 136 | A lower burstiness value (0 < burstiness < 1) results |
| 137 | in more bursty requests, while a higher burstiness value |
| 138 | (burstiness > 1) results in a more uniform arrival of requests. |
| 139 | """ |
| 140 | input_requests: Iterable[SampleRequest] = iter(input_requests) |
| 141 | |
| 142 | # Calculate scale parameter theta to maintain the desired request_rate. |
| 143 | assert burstiness > 0, f"A positive burstiness factor is expected, but given {burstiness}." |
| 144 | theta = 1.0 / (request_rate * burstiness) |
| 145 | |
| 146 | for request in input_requests: |
| 147 | yield request |
| 148 | |
| 149 | if request_rate == float("inf"): |
| 150 | # If the request rate is infinity, then we don't need to wait. |
| 151 | continue |
| 152 | |
| 153 | # Sample the request interval from the gamma distribution. |
| 154 | # If burstiness is 1, it follows exponential distribution. |
| 155 | interval = np.random.gamma(shape=burstiness, scale=theta) |
| 156 | # The next request will be sent after the interval. |
| 157 | await asyncio.sleep(interval) |
| 158 | |
| 159 | |
| 160 | def calculate_metrics( |