Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5825): add minRoundTripTime to ServerDescription and change roundTripTime to a moving average #4059

Merged
merged 23 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add MovingWindow tests
  • Loading branch information
W-A-James committed Mar 29, 2024
commit 0bcf2e87f1e822c6f95ff1de92abad4614a32d65
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1348,8 +1348,8 @@ export async function fileIsAccessible(fileName: string, mode?: number) {
export class MovingWindow {
/** Index of the next slot to be overwritten */
private writeIndex: number;
private length: number;
private samples: Float64Array;
length: number;
samples: Float64Array;

constructor(windowSize = 10) {
this.samples = new Float64Array(windowSize);
Expand Down
145 changes: 145 additions & 0 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MongoDBCollectionNamespace,
MongoDBNamespace,
MongoRuntimeError,
MovingWindow,
ObjectId,
shuffle,
TimeoutController
Expand Down Expand Up @@ -1045,4 +1046,148 @@ describe('driver utils', function () {
});
});
});

describe('class MovingWindow', () => {
describe('constructor', () => {
it('Constructs a Float64 array of length windowSize', () => {
const window = new MovingWindow(10);
expect(window.samples).to.have.length(10);
});
});

describe('addSample', () => {
context('when length < windowSize', () => {
it('increments the length', () => {
const window = new MovingWindow(10);
expect(window.length).to.equal(0);

window.addSample(1);

expect(window.length).to.equal(1);
});
});
context('when length === windowSize', () => {
let window: MovingWindow;
const size = 10;

beforeEach(() => {
window = new MovingWindow(size);
for (let i = 1; i <= size; i++) {
window.addSample(i);
}
});

it('does not increment the length', () => {
window.addSample(size + 1);
expect(window.length).to.equal(size);
});

it('overwrites the oldest element', () => {
window.addSample(size + 1);
for (const el of window.samples) {
if (el === 1) expect.fail('Did not overwrite oldest element');
}
});

it('appends the new element to the end of the window', () => {
window.addSample(size + 1);
expect(window.last).to.equal(size + 1);
});
});
});

describe('min()', () => {
context('when length < 2', () => {
it('returns 0', () => {
const window = new MovingWindow(10);
// length 0
expect(window.min()).to.equal(0);

window.addSample(1);
// length 1
expect(window.min()).to.equal(0);
});
});

context('when 2 <= length < windowSize', () => {
let window: MovingWindow;
beforeEach(() => {
window = new MovingWindow(10);
for (let i = 1; i <= 3; i++) {
window.addSample(i);
}
});

it('correctly computes the minimum', () => {
expect(window.min()).to.equal(1);
});
});

context('when length == windowSize', () => {
let window: MovingWindow;
const size = 10;

beforeEach(() => {
window = new MovingWindow(size);
for (let i = 1; i <= size * 2; i++) {
window.addSample(i);
}
});

it('correctly computes the minimum', () => {
expect(window.min()).to.equal(size + 1);
});
});
});

describe('average()', () => {
it('correctly computes the mean', () => {
const window = new MovingWindow(10);
let sum = 0;

for (let i = 1; i <= 10; i++) {
sum += i;
window.addSample(i);
}

expect(window.average()).to.equal(sum / 10);
});
});

describe('last', () => {
context('when length == 0', () => {
it('returns null', () => {
const window = new MovingWindow(10);
expect(window.last).to.be.null;
});
});

context('when length > 0', () => {
it('returns the most recently inserted element', () => {
const window = new MovingWindow(10);
for (let i = 0; i < 11; i++) {
window.addSample(i);
}
expect(window.last).to.equal(10);
});
});
});

describe('clear', () => {
let window: MovingWindow;

beforeEach(() => {
window = new MovingWindow(10);
for (let i = 0; i < 20; i++) {
window.addSample(i);
}
expect(window.length).to.equal(10);
});

it('sets length to 0', () => {
window.clear();
expect(window.length).to.equal(0);
});
});
});
});