Skip to content

Commit

Permalink
Add request processing time logging to middleware benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mRcOol7 committed Feb 8, 2024
1 parent 2a00da2 commit c8909b9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions benchmarks/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,36 @@ while (n--) {
});
}

app.use(function(req, res){var express = require('..');
var app = express();

// Number of middleware
var n = parseInt(process.env.MW || '1', 10);
console.log(' %s middleware', n);

// Adding a middleware to log the request processing time
app.use(function(req, res, next){
var start = process.hrtime();
res.on('finish', () => {
var diff = process.hrtime(start);
console.log(`Request processing time: ${diff[0] * 1e9 + diff[1]} nanoseconds`);
});
next();
});

// Adding user-defined middlewares
while (n--) {
app.use(function(req, res, next){
next();
});
}

app.use(function(req, res){
res.send('Hello World');
});

app.listen(3333, () => console.log('Server running on port 3333'));

res.send('Hello World')
});

Expand Down

0 comments on commit c8909b9

Please sign in to comment.