Run Node Run
-
Upload
kevin-swiber -
Category
Technology
-
view
794 -
download
7
description
Transcript of Run Node Run
![Page 2: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/2.jpg)
Agenda
• Benchmarking • V8 JavaScript Tips and Tricks• Inside Crankshaft• Profiling Node
![Page 3: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/3.jpg)
From 365 to 5,000 requests per second.
![Page 4: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/4.jpg)
This shit works. Swears it.
![Page 5: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/5.jpg)
Note: This is complex, but you’ll sleep better at night.
![Page 6: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/6.jpg)
If you can sleep at all.
![Page 7: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/7.jpg)
This is about speed.
![Page 8: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/8.jpg)
Speed is addictive.
![Page 9: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/9.jpg)
Benchmarking
![Page 10: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/10.jpg)
HTTP
![Page 11: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/11.jpg)
$ ab -n1000 –c100 –k http://127.0.0.1/
![Page 12: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/12.jpg)
$ siege –r100 –c100 -b http://127.0.0.1/
![Page 13: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/13.jpg)
JMeter
![Page 14: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/14.jpg)
ab output
Concurrency Level: 100Time taken for tests: 8.680 secondsRequests per second: 11521.27 [#/sec] (mean)Time per request: 8.680 [ms] (mean)Time per request: 0.087 [ms] (mean, across all concurrent requests)
![Page 15: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/15.jpg)
Non-HTTP
![Page 16: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/16.jpg)
var maxRuns = 1000;var start = Date.now();for (var i = 0; i < maxRuns; i++) { // do stuff...}var end = Date.now();
var elapsed = end – start;var timePerRun = elapsed / maxRuns;
console.log(elapsed + ‘ ms’);console.log(timePerRun + ‘ ms/run’
![Page 17: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/17.jpg)
Faster!
![Page 18: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/18.jpg)
Hidden Classes
function Point(x, y) { this.x = x; this.y = y;}
var p1 = new Point(1, 2);var p2 = new Point(3, 4);
![Page 19: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/19.jpg)
Hidden Classes
function Point(x, y) { this.x = x; this.y = y;}
var p1 = new Point(1, 2);var p2 = new Point(3, 4);p2.z = 9; // ohnoes!!
![Page 20: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/20.jpg)
Dictionary Mode
function takeOrder(row) { var order = { food: row.get(‘food’), quantity: row.get(‘qty’); }; process(order);}
![Page 21: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/21.jpg)
Dictionary Mode
function takeOrder(row) { var order = { food: row.get(‘food’), quantity: row.get(‘qty’); }; process(order);}// ohnoes!!!for(var i = 0; i < db.length; i++) { takeOrder(db[i]);}
![Page 22: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/22.jpg)
Try-Catch
try { // hot code} catch(e) { console.log(e);}
![Page 23: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/23.jpg)
Try-Catch
try { process();} catch(e) { console.log(e);}
function process() { // hot code}
![Page 24: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/24.jpg)
Hot Code
• Don’t monkey-patch.• Don’t mix types for the same property.• Avoid dictionary mode.• Set all properties in the constructor.• Move high-performance code out of try-catch.• If array.length < 65000, specify the size.
![Page 25: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/25.jpg)
V8’s Crankshaft is Awesome
![Page 26: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/26.jpg)
Base Compiler
![Page 27: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/27.jpg)
Runtime Profiler
![Page 28: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/28.jpg)
Optimizing Compiler
![Page 29: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/29.jpg)
Deoptimization Support
![Page 30: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/30.jpg)
JavaScript ->Hydrogen ->Lithium ->(Native)
![Page 31: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/31.jpg)
Profiling Node
![Page 32: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/32.jpg)
$ node --prof server.js
![Page 33: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/33.jpg)
Note: Your benchmark will be slower.
![Page 34: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/34.jpg)
$ lsserver.js v8.log
$ export D8_PATH=~/node/src/deps/v8$ ~/nvm/src/node-v0.8.18/deps/v8/tools/mac-tick-processor > profile.log
![Page 35: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/35.jpg)
[JavaScript]: ticks total nonlib name 251 0.5% 0.5% LazyCompile: *Socket.write net.js:465
[C++]: ticks total nonlib name 48625 94.4% 94.4% ___psynch_rw_unlock
[GC]: ticks total nonlib name 263 0.5%
![Page 36: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/36.jpg)
c4milo/node-webkit-agent
![Page 37: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/37.jpg)
$ cat server.jsvar agent = require('webkit-devtools-agent');// create serverconsole.log(process.pid)
$ node server.js22341
$ kill –SIGUSR2 22341$ open http://c4milo.github.com/node-webkit-agent/21.0.1180.57/inspector.html?host=localhost:1337&page=0
![Page 38: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/38.jpg)
More Profiling Options
![Page 39: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/39.jpg)
--trace_opt
• Shows code optimizations.• Good for spotting hot code.
![Page 40: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/40.jpg)
--trace_deopt
• Shows code de-optimizations.• Good for spotting type information changes in
hot code.
![Page 41: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/41.jpg)
“If you ain’t first, you’re last!”
![Page 42: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/42.jpg)
The plumbing should work like greased lightning.
![Page 43: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/43.jpg)
You can’t control everything.
![Page 44: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/44.jpg)
Fail with grace.
![Page 45: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/45.jpg)
Benchmark regularly.
![Page 46: Run Node Run](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b514b84a7959fd6d8b48e5/html5/thumbnails/46.jpg)
Additional Resources
• Understanding V8• Breaking the JavaScript Speed Limit• V8 Internals• Profiling Node Programs on Mac OS X• Node Webkit Agent