-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserver.js
53 lines (44 loc) · 1.33 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const throng = require('throng')
const WORKERS = process.env.WEB_CONCURRENCY || 1
const PORT = process.env.PORT || 3000
throng({
workers: WORKERS,
lifetime: Infinity
}, start)
function start() {
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) {
res.send('Hello, world')
}
function cpuBound(req, res, next) {
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 hashed date for you! ' + hmac.read()))
}
function memoryBound(req, res, next) {
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) {
setTimeout(function SimulateDb() {
res.send('Got response from fake db!')
}, 300).unref()
}
function onListen() {
console.log('Listening on', PORT)
}
}