├── .gitignore ├── health-check.sh ├── healthcheck.sh ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /health-check.sh: -------------------------------------------------------------------------------- 1 | while true; do date && curl -m 5 http://localhost:1337/healthcheck; sleep 1; done 2 | -------------------------------------------------------------------------------- /healthcheck.sh: -------------------------------------------------------------------------------- 1 | while true; do date && curl -m 5 http://localhost:1337/healthcheck && echo; sleep 1; done 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-async-block", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "author": "michael-go", 7 | "license": "ISC", 8 | "dependencies": { 9 | "express": "^4.16.3" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const crypto = require('crypto'); 3 | 4 | const PID = process.pid; 5 | 6 | function log(msg) { 7 | console.log(`[${PID}]` ,new Date(), msg); 8 | } 9 | 10 | function randomString() { 11 | return crypto.randomBytes(100).toString('hex'); 12 | } 13 | 14 | const app = express(); 15 | app.get('/healthcheck', function healthcheck(req, res) { 16 | log('they check my health'); 17 | res.send('all good!\n') 18 | }); 19 | 20 | 21 | app.get('/compute-sync', function computeSync(req, res) { 22 | log('computing sync!'); 23 | const hash = crypto.createHash('sha256'); 24 | for (let i=0; i < 10e6; i++) { 25 | hash.update(randomString()) 26 | } 27 | res.send(hash.digest('hex') + '\n'); 28 | }); 29 | 30 | app.get('/compute-async', async function computeAsync(req, res) { 31 | log('computing async!'); 32 | 33 | const hash = crypto.createHash('sha256'); 34 | 35 | const asyncUpdate = async () => hash.update(randomString()); 36 | 37 | for (let i = 0; i < 10e6; i++) { 38 | await asyncUpdate(); 39 | } 40 | res.send(hash.digest('hex') + '\n'); 41 | }); 42 | 43 | app.get('/compute-with-promises', function computeWPromises(req, res) { 44 | log('computing with promises!'); 45 | 46 | const hash = crypto.createHash('sha256'); 47 | 48 | const updatePromise = () => new Promise((resolve) =>{ 49 | hash.update(randomString()) 50 | resolve(); 51 | }); 52 | 53 | let i = 0; 54 | 55 | const loop = () => { 56 | if (i < 10e6) { 57 | i++; 58 | updatePromise().then(loop); 59 | } else { 60 | res.send(hash.digest('hex') + '\n'); 61 | } 62 | } 63 | 64 | loop(); 65 | }); 66 | 67 | app.get('/compute-with-set-timeout', async function computeWSetTimeout (req, res) { 68 | log('computing async with setTimeout!'); 69 | 70 | function setTimeoutPromise(delay) { 71 | return new Promise((resolve) => { 72 | setTimeout(() => resolve(), delay); 73 | }); 74 | } 75 | 76 | const hash = crypto.createHash('sha256'); 77 | for (let i = 0; i < 10e6; i++) { 78 | hash.update(randomString()); 79 | await setTimeoutPromise(0); 80 | } 81 | res.send(hash.digest('hex') + '\n'); 82 | }); 83 | 84 | app.get('/compute-with-set-immediate', async function computeWSetImmediate(req, res) { 85 | log('computing async with setImmidiate!'); 86 | 87 | function setImmediatePromise() { 88 | return new Promise((resolve) => { 89 | setImmediate(() => resolve()); 90 | }); 91 | } 92 | 93 | const hash = crypto.createHash('sha256'); 94 | for (let i = 0; i < 10e6; i++) { 95 | hash.update(randomString()); 96 | await setImmediatePromise() 97 | } 98 | res.send(hash.digest('hex') + '\n'); 99 | }); 100 | 101 | app.get('/compute-with-next-tick', async function computeWNextTick (req, res) { 102 | log('computing async with nextTick!'); 103 | 104 | function nextTickPromise() { 105 | return new Promise((resolve) => { 106 | process.nextTick(() => resolve()); 107 | }); 108 | } 109 | 110 | const hash = crypto.createHash('sha256'); 111 | for (let i = 0; i < 10e6; i++) { 112 | hash.update(randomString()); 113 | await nextTickPromise() 114 | } 115 | res.send(hash.digest('hex') + '\n'); 116 | }); 117 | 118 | const PORT = process.env.PORT || 1337; 119 | let server = app.listen(PORT, () => log('server listening on :' + PORT)); 120 | server.setTimeout(5 * 60 * 1000); 121 | --------------------------------------------------------------------------------