Your SlideShare is downloading. ×
Run Node Run
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Run Node Run

774
views

Published on

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

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
774
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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