From 9e73a76c6a873365de4ad9ce726e6e09e3589c30 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Fri, 18 Dec 2020 04:28:49 -0500
Subject: [PATCH] Add "message creation" logic to test many calls at once

---
 app.js                    | 19 +++++++++++--------
 src/database_functions.js | 26 +++++++++++++++++++++++++-
 src/helper_functions.js   | 26 ++++++++++++++++++++++++++
 src/mysql.js              |  6 +++---
 4 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/app.js b/app.js
index 8356a3e..2c46f4d 100644
--- a/app.js
+++ b/app.js
@@ -6,7 +6,7 @@
 
 // User Imports.
 const config = require('./config.js');
-const { generate_users, test_db_functions } = require('./src/database_functions.js');
+const { create_message, generate_users, test_db_functions } = require('./src/database_functions.js');
 const { userConfirmation } = require('./src/helper_functions.js');
 const { Logging, testLogLevels } = require('./src/logging.js');
 const MySql = require('./src/mysql.js');
@@ -24,19 +24,22 @@ async function main() {
     logger.log('');
 
     // Create MySql helper class.
-    var debug = true;
-    const mysql = new MySql(config['mysql'], debug);
+    var debug = false;
+    const mysql = await new MySql(config['mysql'], debug);
 
     // Connect to database.
     await mysql.open_conn();
 
+    // Reset database by emptying all tables.
+    await mysql.reset_db(true);
+
     // Generate database users.
-    for (let index = 0; index < 1000; index++) {
-        generate_users(mysql);
-    }
+    generate_users(mysql);
 
-    // Reset database by emptying all tables.
-    // await mysql.reset_db(true);
+    // Generate 100,0000 messages.
+    for (let index = 0; index < 100000; index++) {
+        create_message(mysql);
+    }
 
     // Close existing database connections.
     await mysql.close_conn();
diff --git a/src/database_functions.js b/src/database_functions.js
index 6b8b13c..b39cfbd 100644
--- a/src/database_functions.js
+++ b/src/database_functions.js
@@ -7,7 +7,7 @@
 const mysql = require('mysql2/promise');
 
 // User Imports.
-const { userConfirmation } = require('./helper_functions.js');
+const { generateRandomNum, userConfirmation } = require('./helper_functions.js');
 const { Logging } = require('./logging.js');
 
 
@@ -67,7 +67,31 @@ async function generate_users(mysql, debug) {
 }
 
 
+/**
+ * Creates a single message.
+ * :param mysql: Instance of MySQL connector class.
+ * :param debug: Bool indicating if debug mode or not.
+ */
+async function create_message(mysql, debug) {
+    if (debug) {
+        logger.log('');
+        logger.debug('Executing `generate_users()` function:');
+    }
+
+    // Get pk of sender. Sender is chosen at random.
+    user_list_length = await mysql.query('SELECT * FROM user;').length;
+    sender_id = generateRandomNum(1, user_list_length);
+
+    // Here, we would normally generate a message too. But we're lazy so instead just reuse the sender_id value.
+    message = sender_id;
+
+    // Add message to database.
+    mysql.query('INSERT INTO message (message, user_id) VALUES (?, ?)', [sender_id, message]);
+}
+
+
 module.exports = {
+    create_message,
     generate_users,
     test_db_functions,
 };
diff --git a/src/helper_functions.js b/src/helper_functions.js
index 16d2f06..d412387 100644
--- a/src/helper_functions.js
+++ b/src/helper_functions.js
@@ -79,8 +79,34 @@ function sleep(ms) {
 }
 
 
+/**
+ * Generates a random number between two values.
+ * :param min: Inclusive floor for random number.
+ * :param max: Inclusive ceiling for random number.
+ */
+function generateRandomNum(min, max) {
+    // Validate input.
+    if (isNaN(parseInt(min))) {
+        // Not a valid number. Default to 0.
+        min = 0;
+    }
+    if (isNaN(parseInt(max))) {
+        // Not a valid number. Default to 10, or [ min+1 ], whichever is greater.
+        max = 10;
+        if (max <= min) {
+            max = min + 1;
+        }
+    }
+
+    Math.ceil(min);
+    Math.floor(max)
+    return Math.floor(Math.random() * (max - min + 1) + min);
+}
+
+
 
 module.exports = {
+    generateRandomNum,
     getCurrentDate,
     sleep,
     userConfirmation,
diff --git a/src/mysql.js b/src/mysql.js
index 80d027d..b37a963 100644
--- a/src/mysql.js
+++ b/src/mysql.js
@@ -88,9 +88,9 @@ class MySql {
                 let conn_queue_head = this.connection_pool.pool['_connectionQueue']['_head'];
                 let conn_queue_tail = this.connection_pool.pool['_connectionQueue']['_tail'];
                 if (this.debug) {
-                    console.debug('total_connections: {}'.format(total_conns));
-                    console.debug('free_connections: {}'.format(free_conns));
-                    console.debug('con_queue: {}, {}'.format(conn_queue_head, conn_queue_tail));
+                    logger.debug('total_connections: {}'.format(total_conns));
+                    logger.debug('free_connections: {}'.format(free_conns));
+                    logger.debug('con_queue: {}, {}'.format(conn_queue_head, conn_queue_tail));
                 }
 
                 // Check if connections are open and connection queue is empty.
-- 
GitLab