-
Notifications
You must be signed in to change notification settings - Fork 55
/
load-test-scheduler.ts
35 lines (31 loc) · 1.33 KB
/
load-test-scheduler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Logger } from 'ts-log';
import { Observable, map, mergeMap, take, timer } from 'rxjs';
export interface LoadTestSchedulerProps<T> {
/** Time in seconds during which the scheduler calls the `callUnderTest` */
duration: number;
/** How many times scheduler will call `callUnderTest` in the given `duration` */
callsPerDuration: number;
/** Each call will have a unique incrementing numeric id. This is the start value. */
idOffset?: number;
/** Asynchronous call that will be called `callsPerDuration` times, evenly split in `duration` seconds */
callUnderTest: (id: number) => Observable<T>;
}
export interface LoadTestSchedulerDependencies {
logger: Logger;
}
/** Use `LoadTestSchedulerProps` to schedule calls to `callUnderTest` */
export const getLoadTestScheduler = <T>(
{ duration, callsPerDuration, idOffset = 0, callUnderTest }: LoadTestSchedulerProps<T>,
{ logger }: LoadTestSchedulerDependencies
): Observable<T> =>
new Observable<T>((observer) => {
const callIntervalMs = (duration * 1000) / callsPerDuration;
logger.info(`Running ${callsPerDuration} calls in ${duration}s, 1 call / ${callIntervalMs}ms`);
return timer(0, callIntervalMs)
.pipe(
map((id) => id + idOffset),
take(callsPerDuration),
mergeMap((id) => callUnderTest(id))
)
.subscribe(observer);
});