Professional Documents
Culture Documents
js
ry@tinyclouds.org
May 5, 2010
node.js is a set of bindings to the V8
javascript VM.
Focused on performance.
First a silly benchmark:
100 concurrent clients
1 megabyte response
(bigger is better)
The code:
1 http = require(’http’)
2 Buffer = require(’buffer’).Buffer;
3
4 n = 1024*1024;
5 b = new Buffer(n);
6 for (var i = 0; i<n; i++) b[i] = 100;
7
8 http.createServer(function (req, res) {
9 res.writeHead(200);
10 res.end(b);
11 }).listen(8000);
Please take with a grain of salt. Very
special circumstances.
L2: 14 cycles
—————————
—————————
“Blocking”
DISK, NETWORK
result = query(’select * from T’);
// use result
BLOCKS
Better software can multitask.
NGINX 8000
Apache 6000
Req/sec
4000
2000
0
0 1000 2000 3000 4000
Concurrent Clients
http://blog.webfaction.com/a-little-holiday-present
40
30
20 Memory (MB)
10
Apache
NGINX
0
0 1000 2000 3000 4000
Concurrent Clients
http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX
The difference?
result = query(’select..’);
// use result
No machinery required.
query(’select..’, function (result) {
// use result
});
I Chunked encoding
I Pipelined messages
I Hanging requests for comet
applications.
Design Goals
Be platform independent.
Design Goals
Few dependencies
Static linking
Design Goals
Make it enjoyable.
Architecture
JavaScript Node standard library
C Node bindings
thread event
pool loop
V8
(libeio) (libev)
The JavaScript layer can access the
main thread.
ev_loop()
Node execution stack
socket_readable(1)
ev_loop()
Node execution stack
http_parse(1)
socket_readable(1)
ev_loop()
Node execution stack
load("index.html")
http_parse(1)
socket_readable(1)
ev_loop()
Node sends a request to the thread
pool to load "index.html".
http_parse(1)
socket_readable(1)
ev_loop()
Node execution stack
socket_readable(1)
ev_loop()
Node execution stack
ev_loop()
The request is sent to the disk.
ev_loop()
Node execution stack
socket_readable(2)
ev_loop()
Node execution stack
http_parse(2)
socket_readable(2)
ev_loop()
Node execution stack
http_respond(2)
http_parse(2)
socket_readable(2)
ev_loop()
Node execution stack
http_parse(2)
socket_readable(2)
ev_loop()
Node execution stack
socket_readable(2)
ev_loop()
Node execution stack
ev_loop()
The process sits idle.
ev_loop()
Node execution stack
file_loaded()
ev_loop()
Node execution stack
http_respond(1)
file_loaded()
ev_loop()
Node execution stack
file_loaded()
ev_loop()
Node execution stack
ev_loop()
Usage and
Examples
(using node 0.1.93)
Download, configure, compile, and
make install it.
http://nodejs.org/
3 setTimeout(function () {
4 sys.puts(’world’);
5 }, 2000);
6 sys.puts(’hello’);
3 setTimeout(function () {
4 sys.puts(’world’);
5 }, 2000);
6 sys.puts(’hello’);
2 seconds later...
% node hello_world.js
hello
world
%
A program which:
3 var s = net.createServer();
4 s.addListener(’connection’,
5 function (c) {
6 c.end(’hello!\n’);
7 });
8
9 s.listen(8000);
File I/O is non-blocking too.
NGINX
NGINX
Node
When Node is stable, remove the
front-end web server entirely.
Internet
Node
Questions...?
http://nodejs.org/
ry@tinyclouds.org