Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

[MO] How to log your queries duration with Knex #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SUMMARY.MD
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
- [1. Cache your routes using varnish](/performance/backend/cache-routes-using-varnish.mo.md)
- [2. Serve images as static files](/performance/backend/serve-images-as-static-files.mo)
- [3. Minimize your number of SQL queries](/performance/backend/minimize-number-sql-queries.mo.md)
- [4. Log queries durations with Knex](/performance/backend/log-queries-durations-with-knex.mo.md)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummm... je viens de voir que les titres ne sont pas claires, mais le but de cette partie du sommaire, c'est de donner des tips de solutions technique.
Il faut surtout ajouter ton article ici : https://github.com/bamlab/dev-standards/edit/master/performance/performance-decision-flow.s.md

Le sommaire principal !! :-)

Ton outil est un outil d'investigation, il est plus adapté à la 1ere partie des standards perf:

- Front
- 1. Pure components **-> [To do]**
- Have you a mean of keeping the performance under control?
Expand Down
60 changes: 60 additions & 0 deletions performance/backend/log-queries-durations-with-knex.mo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# [MO] How to log your queries durations with Knex (~10)

## Owner: [Guillaume Renouvin](https://github.com/GuillaumeRenouvin)
## Creation date: August, 1st 2018
## Update date: August, 1st 2018

## Context
- You want to log your database requests durations using Knex query builder

## Prerequisites
- Have Knex installed

## Steps

### Log your queries informations (~5min)
- After your Knex instantiation add listeners on each query, query response and query errors
```javascript
db.on('query', query => {
console.log(query.sql);
})
.on('query-response', (response, query) => {
console.log(query.sql);
})
.on('query-error', err => {
console.log(err);
});
```
> Check: Launch your server, call an API route and you should see your queries on the logs

### Log query duration (~5min)

```javascript
import now from 'performance-now';

const durations = {};

knex
.on('query', query => {
durations[query.__knexQueryUid] = {
query,
startTime: now(),
};
})
.on('query-response', (response, query) => {
durations[query.__knexQueryUid].endTime = now();

console.log(query.sql, ',', `[${query.bindings ? query.bindings.join(',') : ''}]`);
console.log(
`Time: ${(
durations[query.__knexQueryUid].endTime - durations[query.__knexQueryUid].startTime
).toFixed(3)} ms\n`
);
delete durations[uid];
});

```
> Check: Launch your server, call an API route and you should see your queries durations on the logs

### Sources
- [Timing Your Queries in Knex.js for Node.js](https://spin.atomicobject.com/2017/03/27/timing-queries-knexjs-nodejs/)