Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update simple-profiling.md #1975

Closed
wants to merge 6 commits into from
Closed
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
41 changes: 29 additions & 12 deletions locale/en/docs/guides/simple-profiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ Let's see how the built-in profiler can help provide insight into application
performance.

To illustrate the use of the tick profiler, we will work with a simple Express
application. Our application will have two handlers, one for adding new users to
application (See [here](http://expressjs.com/en/starter/installing.html) for
installation instructions). Our application will have two handlers, one for
adding new users to
our system:

```javascript

const express = require('express');
const crypto = require('crypto');
const app = express();
const port = 8080;
mcollina marked this conversation as resolved.
Show resolved Hide resolved
const users = {};

app.get('/newUser', (req, res) => {
let username = req.query.username || '';
const password = req.query.password || '';
Expand All @@ -48,11 +57,9 @@ app.get('/newUser', (req, res) => {

res.sendStatus(200);
});
```

and another for validating user authentication attempts:
// and another for validating user authentication attempts:

```javascript
app.get('/auth', (req, res) => {
let username = req.query.username || '';
const password = req.query.password || '';
Expand All @@ -72,6 +79,10 @@ app.get('/auth', (req, res) => {
res.sendStatus(401);
}
});

app.listen(port, () => {
console.log(`example app running on port ${port}`);
});
```

*Please note that these are NOT recommended handlers for authenticating users in
Expand All @@ -86,10 +97,15 @@ high latency on requests. We can easily run the app with the built in profiler:
NODE_ENV=production node --prof app.js
```

and put some load on the server using `ab` (ApacheBench):
We'll create a new user:

```
curl -X GET "http://localhost:8080/newUser?username=matt&password=password"
kcacciatore marked this conversation as resolved.
Show resolved Hide resolved
```

and put some load on the server using `ab` (ApacheBench):

```
ab -k -c 20 -n 250 "http://localhost:8080/auth?username=matt&password=password"
kcacciatore marked this conversation as resolved.
Show resolved Hide resolved
```

Expand Down Expand Up @@ -228,13 +244,14 @@ app.get('/auth', (req, res) => {
return res.sendStatus(400);
}

crypto.pbkdf2(password, users[username].salt, 10000, 512, (err, hash) => {
if (users[username].hash.toString() === hash.toString()) {
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
crypto.pbkdf2(password, users[username].salt, 10000, 512, 'sha512',
kcacciatore marked this conversation as resolved.
Show resolved Hide resolved
(err, hash) => {
if (users[username].hash.toString() == hash.toString()) {
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
});
```

Expand Down