Question:
How to measure the time queries take to run in NodeJS?

Problem

I have a simple Nodejs program to test query performance but I have this issue where I have an array of queries and when I loop over them and measure their timings, the second one is always faster than the first one, like let's say I have queries A and B, if the array looks like this [A, B], B is gonna be faster, but if I changed the order to [B, A], A is gonna be faster, but running them individually ( like having a list of just A or B ) or in psql I found that they both almost have identical results. Here is the code.


index.js


const { Client } = require('pg');

const queryPref = require('./src/QueryPref');


require('dotenv').config();


(async ()=> await queryPref())();


queryPref.js


const { Client } = require('pg');



const queries = [

  {

    'queryStr': 'SELECT * FROM tabluno where user_name = $1',

    'options': ['08544f75d007ae439925bb21fe48c64c'],

  },

  {

    'queryStr': 'SELECT * FROM tabluno2 where user_name = $1',

    'options': ['f67a10e60fde706bb6c5948374b385e5'],

  },

]


function initClient() {

  console.log('opening a new connection');

  const client = new Client({

    user: 'postgres',

    host: 'localhost',

    database: 'indexing-is-really-hard',

    password: process.env.DB_PASSWORD,

    port: 5432 // default port for postgres

  });


  return client;

}


async function queryPref() {

  for(const query of queries) {

    let pool = new Client({

      user: 'postgres',

      host: 'localhost',

      database: 'indexing-is-really-hard',

      password: process.env.DB_PASSWORD,

      port: 5432, // default port for postgres

    });


    await pool.connect();

    await pool.query('SELECT 1');


    let avg = 0;

    for(let i = 0; i < 1000; i++)    {

      const start = performance.now();

      await pool.query(query.queryStr, query.options)

      const end = performance.now();

      avg += end - start;

    }

    avg /= 1000;


    console.log(`${query.queryStr} took an avg of ${avg.toFixed(3)}ms`)


    pool.end();

    pool = null;

  }

}



module.exports = queryPref;


as you can see I'm trying to initiate a new client for each query, but it seems like it's doing nothing.


I've tried using connection Pool instead of a client, yet got the same results (the second one is always faster ). I've also tried measuring the average over 1000 queries with client and pool but gave the same result again.


Solution

We could simply calculate like below inside your for loop, const startTime = Date.now(); const timeTaken = Date.now() - startTime;

and add the timeTaken in the common variable outside of your for loop.

Answered by: >Jeyaprakash Ayyanan

Credit: >StackOverflow


Blog Links

>How to manage the Text in the container in Django?

>Fix webapp stops working issue in Django- Python webapp

>Creating a form in Django to upload a picture from the website

>Sending Audio file from Django to Vue

>How to keep all query parameters intact when changing page in Django?

>Solved: TaskList View in Django

>Implement nested serializers in the Django rest framework

>How to filter events by month in Django?

>Sorting the restframework in Django

>Ways to access instances of models in view in order to save both forms at once in Django

>What makes index.html have such kind of name in Django?

>Fix Module Not Found during Deployment- Django

>Creating a Django with existing directories, files, etc.?

>How to Read a CSV file with PHP using cURL?


Nisha Patel

Nisha Patel

Submit
0 Answers