console/packages/services/usage/__tests__/buffer-estimator.spec.ts
Dimitri POSTOLOV de7ba835e4
migrate to vitest (#921)
Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>
2023-02-10 11:11:23 +01:00

144 lines
2.9 KiB
TypeScript

import { createEstimator } from '../src/buffer';
const eventHubLimitInBytes = 900_000;
const bufferSize = 1200;
const defaultBytesPerUnit = eventHubLimitInBytes / bufferSize;
function waitFor(time: number) {
return new Promise(resolve => setTimeout(resolve, time));
}
test('default estimation should be a ratio of max size and the limit in bytes', () => {
const estimator = createEstimator({
logger: console as any,
defaultBytesPerUnit,
resetAfter: 5000,
increaseBy() {
return 0;
},
});
expect(estimator.estimate(bufferSize)).toBeCloseTo(eventHubLimitInBytes);
});
test('teach estimator that 1 op takes 100 bytes', () => {
const estimator = createEstimator({
logger: console as any,
defaultBytesPerUnit,
resetAfter: 5000,
increaseBy() {
return 0;
},
});
estimator.teach({
bytes: 1000,
operations: 10,
});
expect(estimator.estimate(10)).toBeCloseTo(1000);
});
test('increasing the defaultBytesPerUnit', () => {
const estimator = createEstimator({
defaultBytesPerUnit,
resetAfter: 5000,
logger: {
info: vi.fn(),
} as any,
increaseBy() {
return 0.25;
},
});
expect(estimator.estimate(bufferSize)).toBeCloseTo(eventHubLimitInBytes);
estimator.overflowed('test');
expect(estimator.estimate(bufferSize)).toBeCloseTo(1.25 * eventHubLimitInBytes);
});
test('increasing the defaultBytesPerUnit should not go over 50% of original estimation', () => {
const estimator = createEstimator({
defaultBytesPerUnit,
resetAfter: 5000,
logger: {
info: vi.fn(),
} as any,
increaseBy() {
return 0.6;
},
});
expect(estimator.estimate(bufferSize)).toBeCloseTo(eventHubLimitInBytes);
estimator.overflowed('test');
expect(estimator.estimate(bufferSize)).toBeCloseTo(1.5 * eventHubLimitInBytes);
});
test('teach estimator multiple times', () => {
const estimator = createEstimator({
defaultBytesPerUnit,
resetAfter: 5000,
logger: {
info: vi.fn(),
} as any,
increaseBy() {
return 0;
},
});
estimator.teach({
bytes: 1000,
operations: 10,
});
estimator.teach({
bytes: 500,
operations: 5,
});
estimator.teach({
bytes: 10_000,
operations: 100,
});
expect(estimator.estimate(10)).toBeCloseTo(1000);
});
test('reset after N milliseconds', async () => {
const estimator = createEstimator({
defaultBytesPerUnit,
resetAfter: 100,
logger: {
info: vi.fn(),
} as any,
increaseBy() {
return 0;
},
});
estimator.teach({
bytes: 1000,
operations: 10,
});
expect(estimator.estimate(10)).toBeCloseTo(1000);
await waitFor(150);
// reached 100 ms, so reset
estimator.teach({
bytes: 500,
operations: 10,
});
// teaching from start
estimator.teach({
bytes: 500,
operations: 10,
});
expect(estimator.estimate(10)).toBeCloseTo(500);
});