Skip to content

Commit

Permalink
refactor(execute): await connection to send query
Browse files Browse the repository at this point in the history
Waiting for the connection to become available lets us move executiontime back into the resource.
  • Loading branch information
thelindat committed Nov 30, 2021
1 parent e685ff5 commit a352273
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import { FormatError } from './errors';
const execute = async (query, parameters, resource) => {
try {
[query, parameters] = parseParameters(query, parameters);
const connection = await pool.getConnection();
ScheduleResourceTick(resourceName);
const [executionTime, rows] = await pool.query(query, parameters);

const startTime = process.hrtime();
const [rows] = await connection.query(query, parameters);
const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond

if (executionTime >= slowQueryWarning || debug)
console.log(
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${executionTime}ms to execute a query!
${query} ${JSON.stringify(parameters)}^0`
);

connection.release();
return rows;
} catch (error) {
console.log(
Expand Down Expand Up @@ -51,26 +56,29 @@ const preparedStatement = async (query, parameters, resource) => {
const type = queryType(query);
if (!type) throw new FormatError(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);

const connection = await pool.getConnection();
ScheduleResourceTick(resourceName);

const results = [];
let totalTime = 0;
let queryCount = parameters.length;
const startTime = process.hrtime();

for (let i = 0; i < queryCount; i++) {
const [executionTime, rows] = await pool.execute(query, parameters[i]);
totalTime + executionTime;
const [rows] = await connection.execute(query, parameters[i]);
results[i] = rows && (type === 3 ? rows.affectedRows : type === 2 ? rows.insertId : rows);
}

totalTime = totalTime / queryCount;
if (totalTime >= slowQueryWarning || debug)
const executionTime = process.hrtime(startTime)[1] / 1000000; // nanosecond to millisecond
if (executionTime >= slowQueryWarning || debug)
console.log(
`^3[${debug ? 'DEBUG' : 'WARNING'}] ${resource} took ${totalTime}ms to execute ${
queryCount > 1 ? queryCount + ' queries' : 'a query'
}!
${query} ${JSON.stringify(parameters)}^0`
);

connection.release();

if (results.length === 1) {
if (type === 1) {
if (results[0][0] && Object.keys(results[0][0]).length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const transaction = async (queries, parameters, resource) => {
ScheduleResourceTick(resourceName);
const connection = await pool.getConnection();
try {
const time = debug ? process.hrtime.bigint() : Date.now();
const startTime = process.hrtime(startTime);

const fullQuery = parseTransaction(queries, parameters);
const transactionAmount = fullQuery.length;
Expand All @@ -26,7 +26,7 @@ const transaction = async (queries, parameters, resource) => {

await connection.commit();

const executionTime = debug ? Number(process.hrtime.bigint() - time) / 1e6 : Date.now() - time;
const executionTime = process.hrtime(startTime)[1] / 1000000;

if (executionTime >= slowQueryWarning * transactionAmount || debug)
console.log(
Expand Down

0 comments on commit a352273

Please sign in to comment.