Run Node Run

1,307 views

Published on

A brief overview of profiling Node.js and writing code for the V8 runtime.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,307
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Run Node Run

  1. 1. Run Node Run Fine-tuning JavaScript for the V8 Runtime@kevinswiber kswiber@gmail.com
  2. 2. Agenda• Benchmarking• V8 JavaScript Tips and Tricks• Inside Crankshaft• Profiling Node
  3. 3. From 365 to 5,000 requests per second.
  4. 4. This shit works. Swears it.
  5. 5. Note: This is complex, but you’ll sleep better at night.
  6. 6. If you can sleep at all.
  7. 7. This is about speed.
  8. 8. Speed is addictive.
  9. 9. Benchmarking
  10. 10. HTTP
  11. 11. $ ab -n1000 –c100 –k http://127.0.0.1/
  12. 12. $ siege –r100 –c100 -b http://127.0.0.1/
  13. 13. JMeter
  14. 14. ab outputConcurrency 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)
  15. 15. Non-HTTP
  16. 16. 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’
  17. 17. Faster!
  18. 18. Hidden Classesfunction Point(x, y) { this.x = x; this.y = y;}var p1 = new Point(1, 2);var p2 = new Point(3, 4);
  19. 19. Hidden Classesfunction 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!!
  20. 20. Dictionary Modefunction takeOrder(row) { var order = { food: row.get(‘food’), quantity: row.get(‘qty’); }; process(order);}
  21. 21. Dictionary Modefunction 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]);}
  22. 22. Try-Catchtry { // hot code} catch(e) { console.log(e);}
  23. 23. Try-Catchtry { process();} catch(e) { console.log(e);}function process() { // hot code}
  24. 24. 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.
  25. 25. V8’s Crankshaft is Awesome
  26. 26. Base Compiler
  27. 27. Runtime Profiler
  28. 28. Optimizing Compiler
  29. 29. Deoptimization Support
  30. 30. JavaScript ->Hydrogen ->Lithium ->(Native)
  31. 31. Profiling Node
  32. 32. $ node --prof server.js
  33. 33. Note: Your benchmark will be slower.
  34. 34. $ 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
  35. 35. [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%
  36. 36. c4milo/node-webkit-agent
  37. 37. $ 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
  38. 38. More Profiling Options
  39. 39. --trace_opt• Shows code optimizations.• Good for spotting hot code.
  40. 40. --trace_deopt• Shows code de-optimizations.• Good for spotting type information changes in hot code.
  41. 41. “If you ain’t first, you’re last!”
  42. 42. The plumbing should work like greased lightning.
  43. 43. You can’t control everything.
  44. 44. Fail with grace.
  45. 45. Benchmark regularly.
  46. 46. Additional Resources• Understanding V8• Breaking the JavaScript Speed Limit• V8 Internals• Profiling Node Programs on Mac OS X• Node Webkit Agent
  47. 47. Adios!@kevinswiber kswiber@gmail.com

×