diff --git a/app.js b/app.js index 8356a3e877374652cf1da59e3ffbbfae2ab286bb..2c46f4d8ac7bb4e21206e451071fe6610aba72cd 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 6b8b13c423f1bd407d16cd32ed9a39b4d068fdf6..b39cfbd901e18c008bc06bef757daac50e629213 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 16d2f06c8af92d1082addbece924ed0ce620d50a..d412387ade16677d67a350ab4c546b2373670754 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 80d027d3837e495e8ef45c5e49487c58c33d7222..b37a963b375bd27409e16302e1485ee401920428 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.