Skip to content

Commit

Permalink
Gaining Performance: Use Performance Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Valles committed Feb 4, 2024
1 parent db814bb commit 0fa1c9f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A series of _NodeJS_ projects **without external libraries**:

- [RESTful API](./restful-api)
- [Web App GUI](./web-app-gui)
- [Admin CLI](./admin-cli/) (this also includes unit and integration tests)
- [Admin CLI](./admin-cli/) (this also includes tests, and stability and performance improvements)

## Generic Notes

Expand Down
33 changes: 31 additions & 2 deletions admin-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,45 @@ The app will respond to a number of requests (inputs) by an admin user.
- `list logs`
- `more log info --{logId}`

## How to run the existing app
## Run the existing app

```bash
NODE_ENV=staging node index.js
```

> NOTE: For more notes related to RESTful APIs please check the [restful-api/README.md](../restful-api/README.md) file.
## How to run test
## Run tests

```bash
node test
```

## Run th performance test

- Run the app in performance mode:

```bash
NODE_ENV=staging NODE_DEBUG=performance node .
```

- Make a request to the API:

```bash
curl --location 'localhost:3000/api/tokens' \
--data '{
"phone":"1234567891",
"password": "superSecret"
}'
```

- Output:

```bash
PERFORMANCE 10368: Beginning to end 2.9969610003754497
PERFORMANCE 10368: Validating user inputs 0.18770300038158894
PERFORMANCE 10368: User lookup 1.4989050002768636
PERFORMANCE 10368: Password hashing 0.26225699996575713
PERFORMANCE 10368: Token data creation 0.2491029999218881
PERFORMANCE 10368: Token storing 0.7726020002737641
```
2 changes: 1 addition & 1 deletion admin-cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const startCli = () => {
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ',
prompt: '',
});

// Create an initial prompt
Expand Down
33 changes: 30 additions & 3 deletions admin-cli/lib/handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { randomUUID } = require('crypto');
const urlModule = require('url');
const dns = require('dns');
const performance = require('perf_hooks').performance;
const util = require('util');
const debug = util.debuglog('performance');
const { maxChecks } = require('./config');
const _data = require('./data');
const _helpers = require('./helpers');
Expand Down Expand Up @@ -162,18 +165,25 @@ const users = (data, callback) => {
const _tokens = {};

_tokens.post = (data, callback) => {
performance.mark('entered function');
const { phone, password } = data.payload;
const _phone = _helpers.trimStringIfValid(phone, 9, 10);
const _password = _helpers.trimStringIfValid(password);
performance.mark('inputs validated');

if (!_phone || !_password) return callback(400, { error: 'Missing required fields' });

performance.mark('beginning user lookup');
_data.read('users', _phone, (err, userData) => {
performance.mark('user lookup complete');
if (err || !userData) return callback(400, { error: 'Could not find the specified user' });

performance.mark('beginning password hashing');
const hashedPassword = _helpers.hash(_password);
performance.mark('password hashing complete');
if (hashedPassword !== userData.hashedPassword) return callback(400, { error: 'Invalid credentials' });

performance.mark('creating data for token');
const id = randomUUID();
const expires = Date.now() + 60 * 60 * 1000; // 1 hour
const token = {
Expand All @@ -182,9 +192,26 @@ _tokens.post = (data, callback) => {
phone: _phone,
};

_data.create('tokens', id, token, (err) =>
!err ? callback(200, token) : callback(500, { error: 'Could not create a token' }),
);
performance.mark('beginning storing token');
_data.create('tokens', id, token, (err) => {
performance.mark('storing token complete');

// Gather measurements
performance.measure('Beginning to end', 'entered function', 'storing token complete');
performance.measure('Validating user inputs', 'entered function', 'inputs validated');
performance.measure('User lookup', 'beginning user lookup', 'user lookup complete');
performance.measure('Password hashing', 'beginning password hashing', 'password hashing complete');
performance.measure('Token data creation', 'creating data for token', 'beginning storing token');
performance.measure('Token storing', 'beginning storing token', 'storing token complete');

// Log out them
const measurements = performance.getEntriesByType('measure');
measurements.forEach((measurement) => {
debug('\x1b[33m%s\x1b[0m', `${measurement.name} ${measurement.duration}`);
});

!err ? callback(200, token) : callback(500, { error: 'Could not create a token' });
});
});
};

Expand Down

0 comments on commit 0fa1c9f

Please sign in to comment.