Skip to content

Commit

Permalink
enhanced testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sponzillo committed Jun 21, 2021
1 parent 2a523e1 commit f4dfb65
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 182 deletions.
94 changes: 94 additions & 0 deletions benchmarks/direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var assert = require('assert');
const { Chat21Client } = require('../mqttclient/chat21client.js');

const user1 = {
userid: 'USER1',
fullname: 'User 1',
firstname: 'User',
lastname: '1',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2NzI5NDc4ZS1mOWIwLTRiODctYjNhYS03ZjU1OWExNzc5YjIiLCJzdWIiOiJVU0VSMSIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjEuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIxLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjEiLCJjaWQiOiJVU0VSMSIsImF6cCI6IlVTRVIxIiwidXNlcl9pZCI6IlVTRVIxIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjEiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.r-GBXo1fIUtl1QjOkXxcRaenVNQBElRkus3omh9YtjQ'
};

const user2 = {
userid: 'USER2',
fullname: 'User 2',
firstname: 'User',
lastname: '2',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0MmEwMjc2ZC1lODUzLTQ5YjMtOTU4ZS0xODBkMjFjZGZjNWMiLCJzdWIiOiJVU0VSMiIsInNjb3BlIj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fYXBpX3JvbGVzIjoidXNlciJ9.-Cio8ITPCQswv_4KnxJrRbm-5RCXMefuT91wWUNZJmU'
};

const MQTT_ENDPOINT = 'ws://localhost:15675/ws';
const APPID = 'tilechat';

let TOTAL_MESSAGES = 1;
let starttime;
let endtime;

startDirectBenchmark();

function startDirectBenchmark() {
console.log("Sending " + TOTAL_MESSAGES + " direct messages from 1 user to 1 other user. Delivering a total of " + TOTAL_MESSAGES + " messages.");
let messages_count = 0;
let chatClient1 = new Chat21Client(
{
appId: APPID,
MQTTendpoint: MQTT_ENDPOINT
}
);
let chatClient2 = new Chat21Client(
{
appId: APPID,
MQTTendpoint: MQTT_ENDPOINT
}
);
chatClient2.connect(user2.userid, user2.token, () => {
chatClient2.onMessageAdded((message, topic) => {
// console.log("Received:", message.text + ' ID:' + message.message_id);
assert(message != null);
assert(message.text != null);
messages_count += 1;
if (messages_count == TOTAL_MESSAGES) {
endtime = Date.now();
console.log("End:" + endtime);
let totaltime = endtime - starttime;
console.log("TOTAL TIME:" + totaltime);
chatClient2.close(() => {
console.log("...TEST TERMINATED. DISCONNECTING CLIENT2. TOTAL RECEIVED MESSAGES:" + messages_count);
});
chatClient1.close(() => {
console.log("CLIENT 1 DISCONNECTED.");
});
}
});
});

chatClient1.connect(user1.userid, user1.token, () => {
console.log("User1 connected...");
starttime = Date.now();
console.log("Start:" + starttime);
for (i = 0; i < TOTAL_MESSAGES; i++) {
let message_text = "Message" + i;
chatClient1.sendMessage(
message_text,
'text',
user2.userid,
user2.fullname,
user1.fullname,
null,
null,
'direct',
() => {
// console.log("Message sent.");
}
);
}
});
}
162 changes: 162 additions & 0 deletions benchmarks/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
var assert = require('assert');
const { uuid } = require('uuidv4');
const { Chat21Client } = require('../mqttclient/chat21client.js');

const user1 = {
userid: 'USER1',
fullname: 'User 1',
firstname: 'User',
lastname: '1',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e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kzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjIiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.Zkbr3e9MfGGDKRdVUyG4330LxeNaKYS0y3upPtS4Wgg'
};

const user3 = {
userid: 'USER3',
fullname: 'User 3',
firstname: 'User',
lastname: '3',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlMmI2Y2RhMi0yNjhmLTQxZDMtYjBjYy1kZWNjN2I0M2UwMjEiLCJzdWIiOiJVU0VSMyIsInNjb3BlIjpbInJhYmJpdG1xLnJlYWQ6Ki8qL2FwcHMudGlsZWNoYXQudXNlcnMuVVNFUjMuKiIsInJhYmJpdG1xLndyaXRlOiovKi9hcHBzLnRpbGVjaGF0LnVzZXJzLlVTRVIzLioiLCJyYWJiaXRtcS5jb25maWd1cmU6Ki8qLyoiXSwiY2xpZW50X2lkIjoiVVNFUjMiLCJjaWQiOiJVU0VSMyIsImF6cCI6IlVTRVIzIiwidXNlcl9pZCI6IlVTRVIzIiwiYXBwX2lkIjoidGlsZWNoYXQiLCJpYXQiOjE2MjM3Njc1MjAsImV4cCI6MTkzNDgwNzUyMCwiYXVkIjpbInJhYmJpdG1xIiwiVVNFUjMiXSwia2lkIjoidGlsZWRlc2sta2V5IiwidGlsZWRlc2tfYXBpX3JvbGVzIjoidXNlciJ9.-Cio8ITPCQswv_4KnxJrRbm-5RCXMefuT91wWUNZJmU'
};
const MQTT_ENDPOINT = 'ws://localhost:15675/ws';
const API_ENDPOINT = 'http://localhost:8004/api'
const APPID = 'tilechat';

let TOTAL_SENT_MESSAGES = 100;
let TOTAL_DELIVERED_MESSAGES = TOTAL_SENT_MESSAGES * 3;
let starttime;
let endtime;

startBenchmark();

function startBenchmark() {
console.log("Sending " + TOTAL_SENT_MESSAGES + " messages to 3 users. Delivering a total of " + TOTAL_DELIVERED_MESSAGES + " messages.");
let messages_count = 0;
const group_id = "group-" + uuid();
const group_name = "test send message group";
const group_members = {}
group_members[user1.userid] = 1;
group_members[user2.userid] = 1;
group_members[user3.userid] = 1;
let received_messages = {}
const USER1_RECEIVED_KEY = 'user1';
const USER2_RECEIVED_KEY = 'user2';
const USER3_RECEIVED_KEY = 'user3';
const SENT_MESSAGE = "Welcome everybody 2";
let chatClient1 = new Chat21Client( // group creator
{
appId: APPID,
MQTTendpoint: MQTT_ENDPOINT,
APIendpoint: API_ENDPOINT
}
);
let chatClient2 = new Chat21Client( // group member, will receive sent message
{
appId: APPID,
MQTTendpoint: MQTT_ENDPOINT
}
);
let chatClient3 = new Chat21Client( // group member, will receive sent message
{
appId: APPID,
MQTTendpoint: MQTT_ENDPOINT
}
);
chatClient2.connect(user2.userid, user2.token, () => {
console.log("User2 connected...");
chatClient2.onMessageAdded((message, topic) => {
console.log("user2 message added:", message.text);
if (
message &&
message.text &&
!message.attributes
) {
messages_count += 1;
console.log("user2: " + messages_count);
}
check_finish(messages_count, chatClient1, chatClient2, chatClient3);
});
chatClient3.connect(user3.userid, user3.token, () => {
console.log("User3 connected...");
chatClient3.onMessageAdded((message, topic) => {
console.log("user3 message added:", message.text);
if (
message &&
message.text &&
!message.attributes
) {
messages_count += 1;
console.log("user3: " + messages_count);
}
check_finish(messages_count, chatClient1, chatClient2, chatClient3);
});
chatClient1.connect(user1.userid, user1.token, () => {
console.log("User1 (group owner) connected...");
chatClient1.createGroup(
group_name,
group_id,
group_members,
(err, result) => {
assert(err == null);
assert(result != null);
assert(result.success == true);
assert(result.group.name === group_name);
chatClient1.onMessageAdded((message, topic) => {
console.log("user1 message added:", message.text);
if (
message &&
message.text &&
!message.attributes
) {
messages_count += 1;
console.log("user1: " + messages_count);
}
check_finish(messages_count, chatClient1, chatClient2, chatClient3);
});
starttime = Date.now();
console.log("Start:" + starttime);
for (i = 0; i < TOTAL_SENT_MESSAGES; i++) {
let message_text = "Message" + i;
chatClient1.sendMessage(
message_text,
'text',
group_id,
group_name,
user1.fullname,
null,
null,
'group',
() => {
//console.log("Message sent to group", group_name);
}
);
}
}
);
});
});
});
}

function check_finish(messages_count, chatClient1, chatClient2, chatClient3) {
if (messages_count == TOTAL_DELIVERED_MESSAGES) {
endtime = Date.now();
console.log("End:" + endtime);
let totaltime = endtime - starttime;
console.log("TOTAL TIME:" + totaltime);
chatClient1.close(() => {
chatClient2.close(() => {
chatClient3.close(() => {
console.log("BENCHMARK END. TOTAL MESSAGES DELIVERED:", messages_count);
});
});
});
}
}
2 changes: 1 addition & 1 deletion chatdb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChatDB {
delete message['_id'] // if present (message is coming from a mongodb query?) it is illegal. It produces: MongoError: E11000 duplicate key error collection: tiledesk-dialogflow-proxy.messages index: _id_ dup key: { : "5ef72c2494e08ffec88a033a" }
this.db.collection(this.messages_collection).updateOne({timelineOf: message.timelineOf, message_id: message.message_id}, { $set: message }, { upsert: true }, function(err, doc) {
if (err) {
logger.error("db error...", err)
console.error("db error...", err)
if (callback) {
callback(err, null)
}
Expand Down
5 changes: 3 additions & 2 deletions chatservermq.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ if (webhook_enabled == undefined || webhook_enabled === "true" || webhook_enable
}else {
webhook_enabled = false;
}
logger.info("webhook_enabled: " + webhook_enabled);
logger.debug("webhook_enabled: " + webhook_enabled);


var webhook_endpoint = process.env.WEBHOOK_ENDPOINT;
logger.info("webhook_endpoint: " + webhook_endpoint);
logger.debug("webhook_endpoint: " + webhook_endpoint);


let webhook_events_array = null;
Expand All @@ -44,6 +44,7 @@ async function start() {
observer.setWebHookEnabled(webhook_enabled);
observer.setWebHookEndpoint(webhook_endpoint);
observer.setWebHookEvents(webhook_events_array);
// observer.setPersistentMessages(true);

await startServer({app_id: process.env.APP_ID, exchange: 'amq.topic', rabbitmq_uri:process.env.RABBITMQ_URI, mongo_uri: process.env.MONGODB_URI});
}
Expand Down
Loading

0 comments on commit f4dfb65

Please sign in to comment.