Skip to content

Commit

Permalink
fix: waited on time should not include wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
renanccastro committed May 4, 2024
1 parent 0354827 commit c00de0d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/models/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ MethodsModel.prototype.trackMsgSize = function (method, size) {
methodMetrics.sentMsgSize += size;
};

MethodsModel.prototype.trackWaitedOn = function (method, messageQueue) {
MethodsModel.prototype.trackWaitedOn = function (method, messageQueue, started) {
const timestamp = Ntp._now();
const dateId = this._getDateId(timestamp);

let methodMetrics = this._getMetrics(dateId, method);
methodMetrics.waitedOn += calculateWaitedOnTime(messageQueue);
methodMetrics.waitedOn += calculateWaitedOnTime(messageQueue, started);
};

/*
Expand Down
5 changes: 3 additions & 2 deletions lib/models/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ PubsubModel.prototype.trackMsgSize = function (name, type, size) {
}
};

PubsubModel.prototype.trackWaitedOn = function (name, messageQueue) {
PubsubModel.prototype.trackWaitedOn = function (name, messageQueue, started) {
let timestamp = Ntp._now();
let publicationName = this._getPublicationName(name);

let metrics = this._getMetrics(timestamp, publicationName);

metrics.waitedOn += calculateWaitedOnTime(messageQueue);
metrics.waitedOn += calculateWaitedOnTime(messageQueue, started);
};
PubsubModel.prototype.trackWaitTime = function (msg) {
let subscriptionState = this.subscriptions[msg.id];
Expand Down
5 changes: 4 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function pick (obj, keys) {
}, {});
}

export function calculateWaitedOnTime (messageQueue) {
export function calculateWaitedOnTime (messageQueue, startedTime) {
let waitedOnTime = 0;

const now = Ntp._now();
Expand All @@ -124,6 +124,9 @@ export function calculateWaitedOnTime (messageQueue) {
messageQueue.toArray().forEach(function (msg) {
if (msg._waitEventId) {
waitedOnTime += now - msg._waitEventId.at;
if(msg._waitEventId.at < startedTime) {

Check failure on line 127 in lib/utils.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
waitedOnTime -= startedTime - msg._waitEventId.at

Check failure on line 128 in lib/utils.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions lib/wait_time_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ export class WaitTimeBuilder {
unblocked = true;

if (msg.msg === 'method') {
Kadira.models.methods.trackWaitedOn(msg.method, session.inQueue);
Kadira.models.methods.trackWaitedOn(msg.method, session.inQueue, started);
} else if (msg.msg === 'sub') {
Kadira.models.pubsub.trackWaitedOn(msg.name, session.inQueue);
Kadira.models.pubsub.trackWaitedOn(msg.name, session.inQueue, started);
}

unblock();
Expand Down
37 changes: 36 additions & 1 deletion tests/models/methods.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EJSON } from 'meteor/ejson';
import { MethodsModel } from '../../lib/models/methods';
import { TestData } from '../_helpers/globals';
import { CleanTestData, GetMeteorClient, RegisterMethod, Wait, WithDocCacheGetSize, findMetricsForMethod } from '../_helpers/helpers';
import { CleanTestData, CloseClient, GetMeteorClient, RegisterMethod, Wait, WithDocCacheGetSize, findMetricsForMethod, waitForConnection } from '../_helpers/helpers';
import { Meteor } from 'meteor/meteor';

Tinytest.add(
Expand Down Expand Up @@ -211,6 +211,41 @@ Tinytest.addAsync('Models - Method - Waited On - track wait time of queued messa
done();
});


Tinytest.addAsync('Models - Method - Waited On - track waitedOn without wait time', async (test, done) => {
CleanTestData();

let slowMethod = RegisterMethod(function () {
console.log('slow method start');
Meteor._sleepForMs(100);
console.log('slow method end');
});
let unblockedMethod = RegisterMethod(function () {
this.unblock()

Check failure on line 224 in tests/models/methods.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
Meteor._sleepForMs(100);
console.log('slow method end');
});
let fastMethod = RegisterMethod(function () {
console.log('fastMethod');
});


let client = GetMeteorClient();

// subscriptions and method calls made before connected are not run in order
waitForConnection(client);

client.call(slowMethod, () => {});
client.call(unblockedMethod, () => {});
await new Promise(r => client.call(fastMethod, () => r()));

const metrics = findMetricsForMethod(unblockedMethod);

test.isTrue(metrics.waitedOn < 10, `${metrics.waitedOn} should be less than 10`);
CloseClient(client);

done();
});
Tinytest.addAsync('Models - Method - Waited On - check unblock time', async (test, done) => {
let methodId = RegisterMethod( function (id) {
this.unblock();
Expand Down
38 changes: 38 additions & 0 deletions tests/models/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,44 @@ Tinytest.addAsync('Models - PubSub - Waited On - track wait time of queued messa
done();
});

Meteor.publish('tinytest-unblock-fast', function () {
console.log('unblock-fast-pub')

Check failure on line 783 in tests/models/pubsub.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
this.unblock();
Meteor._sleepForMs(100);
this.ready();
});

Tinytest.addAsync('Models - PubSub - Waited On - track waitedOn without wait time', async (test, done) => {
CleanTestData();

let slowMethod = RegisterMethod(function () {
console.log('slow method start');
Meteor._sleepForMs(100);
console.log('slow method end');
});
let fastMethod = RegisterMethod(function () {
console.log('fastMethod');
});


let client = GetMeteorClient();

// subscriptions and method calls made before connected are not run in order
waitForConnection(client);

const pubName = 'tinytest-unblock-fast';
client.call(slowMethod, () => {});
client.subscribe(pubName);
await new Promise(r => client.call(fastMethod, () => r()));

const metrics = FindMetricsForPub(pubName);

test.isTrue(metrics.waitedOn < 10 && metrics.waitedOn >= 0, `${metrics.waitedOn} should be less than 10`);
CloseClient(client);

done();
});

Tinytest.addAsync('Models - PubSub - Waited On - track waited on time of next message', async (test, done) => {
CleanTestData();
let fastMethod = RegisterMethod(function () {
Expand Down

0 comments on commit c00de0d

Please sign in to comment.