Влияние типа хэндлера на обработку запросов в express.js
Результаты для express.js 4.17.1
cores / fn type | rps @ v16.3.0 | rps @ v17.1.0 |
---|---|---|
12 / normal | 15526 | 16365 |
12 / normal + end() | 26969 | 26151 |
12 / const async | 17238 | 15651 |
12 / const async + end() | 28950 | 27514 |
12 / const async + end() + compression | 25352 | 25179 |
12 / const | 16556 | 15965 |
12 / const + res.end() | 27252 | 26774 |
Скрипт тестирования
Выложен отдельным постом.
Используемые типы хэндлеров
- normal:
function name() {}
- const:
const name = () => {}
- const async:
const name = async () => {}
Методы express.js
Отправляемые заголовки для end() и send()
- end()
HTTP/1.1 200 OK
X-Powered-By: Express
Date: Tue, 24 Nov 2021 17:09:47 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 64000
- send()
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 64000
ETag: W/"fa00-JhUqyth9MfL6vFsWm1q79DtIpYU"
Date: Tue, 24 Nov 2021 17:10:09 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Пример кода сервера
const express = require('express')
const os = require("os")
const cluster = require("cluster")
const compression = require('compression')
const port = 18000
const app = express()
const cluster_size = os.cpus().length
app.use(compression())
const response = async (req, res) => {
res.end('')
}
app.get('/', response)
if (cluster_size > 1) {
if (cluster.isMaster) {
for (let i = 0; i < cluster_size; i++) {
cluster.fork()
}
} else {
app.listen(port, () => {})
}
} else {
app.listen(port, () => {})
}