Process
process module in general
child process
Process & Thread
- Process has more than one thread.
Threads are unit of instructions that will be executed by CPU.
Scheduler controlled by Operating System.
There are two ways to improve the performance such as adding more CPU core or allowing OS scheduler to detect big poses in processing time.
Event loop
Node process has one thread and event loop. Event loop is the control structure that checks the call stack and the event queue and decides what one thread should be doing at any given point of time.
Thread pool
Change thread pool size
process.env.UV_THREADPOOL_SIZE = 5
File system behavior
var fs = require('fs');
fs.readFile('./hello.txt', 'utf8', function() {});
This operation is put aside until hard disk returns information of file. Suppose we have thread pool whose size is 4 and 5 tasks to be run including fs.readFile.
process.env.UV_THREADPOOL_SIZE = 4
var crypto = require('crypto');
var fs = require('fs');
fs.readFile('./index.js', () => {
console.log('0');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('1');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('2');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('3');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('4');
});
4
1
3
2
0
process.env.UV_THREADPOOL_SIZE = 5
var crypto = require('crypto');
var fs = require('fs');
fs.readFile('./index.js', () => {
console.log('0');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('1');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('2');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('3');
});
crypto.pbkdf2('a', 'b', 10000, 512, 'sha512', () => {
console.log('4');
});
0
1
2
4
3
Libuv OS delegation
The HTTP request will not be in thread pool. Instead of that, OS async helper takes care of the procedure. No matter how small UV_THREADPOOL_SIZE
is, we can send HTTP requests with concurrency .
Cluster
Cluster manager is responsible for monitoring the health of each instances. Cluster can start and stop instances. Node.js runs single instance by default yet we can increase the amount of instance.
To test server response, use apache benchmark
-c
takes number as concurrency and -n
is the number of request you want to send to.
ab -c 50 -n 5000 localhost:3333/fast
Server Software:
Server Hostname: localhost
Server Port: 3333
Document Path: /fast
Document Length: 23 bytes
Concurrency Level: 50
Time taken for tests: 3.181 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 1150000 bytes
HTML transferred: 115000 bytes
Requests per second: 1571.72 [#/sec] (mean)
Time per request: 31.812 [ms] (mean)
Time per request: 0.636 [ms] (mean, across all concurrent requests)
Transfer rate: 353.02 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 4 4.7 1 46
Processing: 2 28 16.6 23 146
Waiting: 2 26 17.5 21 146
Total: 9 31 15.6 26 146
Percentage of the requests served within a certain time (ms)
50% 26
66% 31
75% 36
80% 39
90% 53
95% 62
98% 78
99% 93
100% 146 (longest request)
Important metrics are Requests per second, Time per request, and Percentage of the requests served within a certain time.
Theprocess
object is aglobal
that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without usingrequire().
Process can handle event on Node.js process as well.
Process events
Event handlers can be attatched by calling on method.
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
Event list is here
https://nodejs.org/api/process.html#process_process_events
Process properties
process.chdir(directory) | The process.chdir() method changes the current working directory of the Node.js process |
---|---|
process.argv | The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched |
process.cwd() | The process.cwd() method returns the current working directory of the Node.js process. |
process.env | The process.env property returns an object containing the user environment |
process.pid | The process.pid property returns the PID of the process. |
process.stdin | The process.stdin property returns a stream connected to stdin (fd 0) |
process.stdout | The process.stdout property returns a stream connected to stdout (fd 1). |
process.nextTick |
Difference betweensetImmediate
andprocess.nextTick
process.nextTick
allows to put function on the head of event queue, which means execute next tick.setImmediate
is done
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
child_process
child_process module enables us to access OS functionalities besides main process.
Usage of child process is
- Put CPU-intesive work aside
- Run command and get the result of command
method | Timing | |
---|---|---|
exec | async | |
spawn | async | |
fork | async | |
execFile | async |
spawn
- The spawn function launches a command in a new process
- The result of executing the spawn function if a
ChildProcess
instance, which implements the EventEmitter. You can register event on the child process.disconnect
,error
,close
,message.
- Multiple child process uses same
stdio
stream soclose
means not the stream is closed.
stdio | inherit use default stdio |
---|---|
shell | Using shell when you set true |
cwd | Setting current working directory |
env | Default is `process.env`. It can not access to global environment variable `$HOME` but only this env variable. |
detached |
exec
- Takes callback function
Use shell
Makes buffer for the output generated by command.
Pass the output to callback. (spawn uses stream)
fork
The fork
function is a variation of the spawn function. The difference between spawn and
fork
is
the way to communicate to main process. We can use send
function to send data to parent process.
*Sync functions
sync functions are synchronous version of child_process. It is useful to simplify task and launching processing tasks but usually it should be avoided.
Exit Codes
https://nodejs.org/api/process.html#process_exit_codes
https://www.youtube.com/watch?v=JPg1hJxXh9w&list=PL7si9zpgyyVdNDlXO6PP1yWvluggxL6wE&index=21
https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a