"
参考了heroku的一个demo,同时集成了perf
环境准备
package.json
{
""name"": ""nodejs-throng"",
""version"": ""1.0.0""//代码效果参考:https://v.youku.com/v_show/id_XNjQwMDQxMTM4NA==.html
,""main"": ""index.js"",
""license"": ""MIT"",
""dependencies"": {
""crypto"": ""^1.0.1"",
""express"": ""^4.17.1"",
""throng"": ""^5.0.0""
}
}
app.js
const throng = require('throng')
const WORKERS = process.env.WEB_CONCURRENCY || 2
const PORT = process.env.PORT || 8080
?
throng(WORKERS, start)
?
function start(workid) {
console.log('Starting:',workid)
const crypto = require('crypto')
const express = require('express')
const app = express()
?
app
.get('/cpu', cpuBound)
.get('/memory', memoryBound)
.get('/io', ioBound)
.get('/', hello)
.listen(PORT, onListen)
?
function hello(req, res, next) {
console.log('workid:',workid)
res.send('Hello, world')
}
?
function cpuBound(req, res, next) {
console.log('workid:',workid)
const key = Math.random() < 0.5 ? 'ninjaturtles' : 'powerrangers'
const hmac = crypto.createHmac('sha512WithRSAEncryption', key)
const date = Date.now() + ''
hmac.setEncoding('base64')
hmac.end(date, () => res.send('A //代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg1NDMyMA==.html
hashed date for you! ' + hmac.read()))}
?
function memoryBound(req, res, next) {
console.log('workid:',workid)
const large = Buffer.alloc(10 1024 1024, 'X')
setTimeout(() => {
const len = large.length // close over the Buffer for 1s to try to foil V8's optimizations and bloat memory
console.log(len)
}, 1000).unref()
res.send('Allocated 10 MB buffer')
}
?
function ioBound(req, res, next) {
console.log('workid:',workid)
setTimeout(function SimulateDb() {
res.send('Got response from fake db!')
}, 300).unref()
}
?
function onListen() {
console.log('Listening on', PORT)
}
}
perf
perf record -e cycles:u -g -- node --perf-basic-prof --perf-prof-unwinding-info app.js
火焰图
可以使用flamescope
运行的多进程效果
说明
最好的对比就是通过不同模式的对比火焰图
参考资料
"