KEMBAR78
Node.js Test | PPTX
Node.js
Thomas Amsler
June 14, 2012
GDG Sacramento
Node
●Created by Ryan Dahl in 2009. First
presented at JSConf EU.
●Node is a platform built on Chrome's V8
JavaScript runtime for easily building fast,
scalable network applications.
●Node uses an event-driven, non-blocking I/O
model that makes it lightweight and efficient.
●Node is for data-intensive real-time
applications that run across distributed
devices.
Node ...
●Server Side JavaScript
●Built on Google's V8 Runtime
●Non-blocking I/O (Asynchronous)
●Evented (Event Loop)
●Module System
●Native on Linux, Mac OS X, Windows
●Enables Real-time Web
●It's all about LATENCY
History
●Netscape LiveWire (1996)
●Rhino (1997) [for Java]
●Aptana Jaxer (2008)
●RingoJS [for Java]
●Narwhale
●Node.js (2009)
●IronJS [for .NET]
Synchronous vs Asynchronous
I/O is not free
●L1: 3 cycles
●L2: 14 cycles
●RAM: 250 cycles
●DISK: 41,000,000 cycles
●NETWORK: 240,000,000 cycles
http://duartes.org/gustavo/blog/post/what-your-computer-
does-while-you-wait
Blocking I/O (Sync)
<?php
echo "Hello";
file_get_contents("/path/to/file");
echo " World";
?>
Threads
●Each Thread takes memory
●Threads introduce additional complexity
oRace conditions
oDeadlock
oAdditional setup overhead
o...
Non-blocking I/O (Async)
console.log('Hello');
fs.readFile('/path/to/file', function(err, data) {
// do something ...
});
console.log(' World');
Event Loop
●Efficient (if used asynchronously)
●Only one stack
●No memory overhead
●Simpler model (no deadlocks, no race
conditions ...)
The C10K Problem
●C10K refers to the problem of optimizing a
web server to handle a large number of
clients at the same time.
●C = CONCURRENT
●Apache uses one thread per connection
●NGINX doesn't use multiple threads but
instead uses an event loop
●NGINX and Node.js are similar with respect
to utilizing an event loop to achieve high
concurrency on low latency
The C10K Problem ...
●C = Concurrent
●http://www.kegel.com/c10k.html
http://blog.webfaction.com/a-little-holiday-present
The C10K Problem ...
●Using one thread per connection is memory-
bound
http://blog.webfaction.com/a-little-holiday-present
... back to Node.js ...
Why JavaScript?
●JS devs already think asynchronously
(Browsers + AJAX)
●JS is fast and getting faster
●JS quickly is becoming a compilation target
o https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-
compile-to-JS
●Code sharing between the client and server
o Maybe common libs ...
●JSON native and full application stack
o MongoDB, Node.js, Angular.js
Google's V8 JavaScript Engine
●V8 is a JavaScript engine specifically
designed for fast execution of large
JavaScript applications
●Used in Google's Chrome browser
●Written in C++
●Fast property access
●Dynamic machine code generation
●Efficient garbage collection
● https://developers.google.com/v8/
● http://blog.chromium.org/2012/05/better-code-optimization-decisions-
for.html
The Node.js Darkside
●Still relatively young
●Bad idea to do raw computation in an event
loop. Use node-webworker
●Debugging is hard but will significantly
improve in future versions
●Callbacks (not really an issue)
doA( argA, function() {
doB(argB, function() {
doC(argC, function() {
// etc.
});
});
});
Node Package Manager (NPM)
●NPM is a package manager for node. You
can use it to install and publish your node
programs. It manages dependencies and
does other cool stuff.
●http://npmjs.org/
●http://search.npmjs.org/
o Express
o Request
o Socket.io (LearnBoost)
o Jade
o ... 10k + more ...
Node Package Manager (NPM) ...
Node Core
(V8, libev, libeio, ...)
Core Modules
(http, net, ...)
Community-created Modules
(express, socket.io, restify, ...)
Let's Node
●Read Evaluate Print Loop (REPL)
oSimilar to Perl, Python, and Ruby
●It's great for testing out and learning about
Node.js
●Since Node.js uses V8, Node REPL is an
ideal place to easily try out and learn
JavaScript
●DEMO
First Node Server
var http = require('http');
var s = http.createServer( function(req, res) {
res.writeHead( 200, { 'Content-Type' : 'text/plain' } );
res.end('Hello Worldn');
});
s.listen(8080);
●curl http://localhost:8080/
●curl -i -X GET http://localhost:8080/
●DEMO
Load Test & Profiling
●Using Apache Bench (ab) for Simple Load
Testing
oab -n 200 -c 200 http://127.0.0.1:8080/
●Profiling Node.js
o http://dtrace.org/blogs/dap/2012/04/25/profiling-node-js/
Node.js Information
●API
ohttp://nodejs.org/api
●SRC
ohttps://github.com/joyent/node
●How To Node
ohttp://howtonode.org
●StackOverflow
ohttp://stackoverflow.com/questions/tagged/node.js
●Projects & Companies using Node.js
ohttp://goo.gl/Wcqtj
Other Resources
●Introduction to Node.js with Ryan Dahl
ohttp://youtu.be/jo_B4LTHi3I
●The Node Beginner Book
ohttp://www.nodebeginner.org
●Ryan Dahl - History of Node.js
ohttp://youtu.be/SAc0vQCC6UQ
●What is Node? (Free)
ohttp://shop.oreilly.com/product/0636920021506.do
●Ryan Dahl's 2009 slides
ohttp://s3.amazonaws.com/four.livejournal/20091117/j
sconf.pdf

Node.js Test

  • 1.
    Node.js Thomas Amsler June 14,2012 GDG Sacramento
  • 2.
    Node ●Created by RyanDahl in 2009. First presented at JSConf EU. ●Node is a platform built on Chrome's V8 JavaScript runtime for easily building fast, scalable network applications. ●Node uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. ●Node is for data-intensive real-time applications that run across distributed devices.
  • 3.
    Node ... ●Server SideJavaScript ●Built on Google's V8 Runtime ●Non-blocking I/O (Asynchronous) ●Evented (Event Loop) ●Module System ●Native on Linux, Mac OS X, Windows ●Enables Real-time Web ●It's all about LATENCY
  • 4.
    History ●Netscape LiveWire (1996) ●Rhino(1997) [for Java] ●Aptana Jaxer (2008) ●RingoJS [for Java] ●Narwhale ●Node.js (2009) ●IronJS [for .NET]
  • 5.
  • 6.
    I/O is notfree ●L1: 3 cycles ●L2: 14 cycles ●RAM: 250 cycles ●DISK: 41,000,000 cycles ●NETWORK: 240,000,000 cycles http://duartes.org/gustavo/blog/post/what-your-computer- does-while-you-wait
  • 7.
    Blocking I/O (Sync) <?php echo"Hello"; file_get_contents("/path/to/file"); echo " World"; ?>
  • 8.
    Threads ●Each Thread takesmemory ●Threads introduce additional complexity oRace conditions oDeadlock oAdditional setup overhead o...
  • 9.
    Non-blocking I/O (Async) console.log('Hello'); fs.readFile('/path/to/file',function(err, data) { // do something ... }); console.log(' World');
  • 10.
    Event Loop ●Efficient (ifused asynchronously) ●Only one stack ●No memory overhead ●Simpler model (no deadlocks, no race conditions ...)
  • 11.
    The C10K Problem ●C10Krefers to the problem of optimizing a web server to handle a large number of clients at the same time. ●C = CONCURRENT ●Apache uses one thread per connection ●NGINX doesn't use multiple threads but instead uses an event loop ●NGINX and Node.js are similar with respect to utilizing an event loop to achieve high concurrency on low latency
  • 12.
    The C10K Problem... ●C = Concurrent ●http://www.kegel.com/c10k.html http://blog.webfaction.com/a-little-holiday-present
  • 13.
    The C10K Problem... ●Using one thread per connection is memory- bound http://blog.webfaction.com/a-little-holiday-present
  • 14.
    ... back toNode.js ...
  • 15.
    Why JavaScript? ●JS devsalready think asynchronously (Browsers + AJAX) ●JS is fast and getting faster ●JS quickly is becoming a compilation target o https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that- compile-to-JS ●Code sharing between the client and server o Maybe common libs ... ●JSON native and full application stack o MongoDB, Node.js, Angular.js
  • 16.
    Google's V8 JavaScriptEngine ●V8 is a JavaScript engine specifically designed for fast execution of large JavaScript applications ●Used in Google's Chrome browser ●Written in C++ ●Fast property access ●Dynamic machine code generation ●Efficient garbage collection ● https://developers.google.com/v8/ ● http://blog.chromium.org/2012/05/better-code-optimization-decisions- for.html
  • 17.
    The Node.js Darkside ●Stillrelatively young ●Bad idea to do raw computation in an event loop. Use node-webworker ●Debugging is hard but will significantly improve in future versions ●Callbacks (not really an issue) doA( argA, function() { doB(argB, function() { doC(argC, function() { // etc. }); }); });
  • 18.
    Node Package Manager(NPM) ●NPM is a package manager for node. You can use it to install and publish your node programs. It manages dependencies and does other cool stuff. ●http://npmjs.org/ ●http://search.npmjs.org/ o Express o Request o Socket.io (LearnBoost) o Jade o ... 10k + more ...
  • 19.
    Node Package Manager(NPM) ... Node Core (V8, libev, libeio, ...) Core Modules (http, net, ...) Community-created Modules (express, socket.io, restify, ...)
  • 20.
    Let's Node ●Read EvaluatePrint Loop (REPL) oSimilar to Perl, Python, and Ruby ●It's great for testing out and learning about Node.js ●Since Node.js uses V8, Node REPL is an ideal place to easily try out and learn JavaScript ●DEMO
  • 21.
    First Node Server varhttp = require('http'); var s = http.createServer( function(req, res) { res.writeHead( 200, { 'Content-Type' : 'text/plain' } ); res.end('Hello Worldn'); }); s.listen(8080); ●curl http://localhost:8080/ ●curl -i -X GET http://localhost:8080/ ●DEMO
  • 22.
    Load Test &Profiling ●Using Apache Bench (ab) for Simple Load Testing oab -n 200 -c 200 http://127.0.0.1:8080/ ●Profiling Node.js o http://dtrace.org/blogs/dap/2012/04/25/profiling-node-js/
  • 23.
    Node.js Information ●API ohttp://nodejs.org/api ●SRC ohttps://github.com/joyent/node ●How ToNode ohttp://howtonode.org ●StackOverflow ohttp://stackoverflow.com/questions/tagged/node.js ●Projects & Companies using Node.js ohttp://goo.gl/Wcqtj
  • 24.
    Other Resources ●Introduction toNode.js with Ryan Dahl ohttp://youtu.be/jo_B4LTHi3I ●The Node Beginner Book ohttp://www.nodebeginner.org ●Ryan Dahl - History of Node.js ohttp://youtu.be/SAc0vQCC6UQ ●What is Node? (Free) ohttp://shop.oreilly.com/product/0636920021506.do ●Ryan Dahl's 2009 slides ohttp://s3.amazonaws.com/four.livejournal/20091117/j sconf.pdf