+ All Categories

Download - Run Node Run

Transcript
Page 1: Run Node Run

Run Node Run

Fine-tuning JavaScript forthe V8 Runtime

@kevinswiber [email protected]

Page 2: Run Node Run

Agenda

• Benchmarking • V8 JavaScript Tips and Tricks• Inside Crankshaft• Profiling Node

Page 3: Run Node Run

From 365 to 5,000 requests per second.

Page 4: Run Node Run

This shit works. Swears it.

Page 5: Run Node Run

Note: This is complex, but you’ll sleep better at night.

Page 6: Run Node Run

If you can sleep at all.

Page 7: Run Node Run

This is about speed.

Page 8: Run Node Run

Speed is addictive.

Page 9: Run Node Run

Benchmarking

Page 10: Run Node Run

HTTP

Page 11: Run Node Run

$ ab -n1000 –c100 –k http://127.0.0.1/

Page 12: Run Node Run

$ siege –r100 –c100 -b http://127.0.0.1/

Page 13: Run Node Run

JMeter

Page 14: Run Node Run

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

Non-HTTP

Page 16: Run Node Run

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

Faster!

Page 18: Run Node Run

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

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

Dictionary Mode

function takeOrder(row) { var order = { food: row.get(‘food’), quantity: row.get(‘qty’); }; process(order);}

Page 21: Run Node Run

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

Try-Catch

try { // hot code} catch(e) { console.log(e);}

Page 23: Run Node Run

Try-Catch

try { process();} catch(e) { console.log(e);}

function process() { // hot code}

Page 24: Run Node Run

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

V8’s Crankshaft is Awesome

Page 26: Run Node Run

Base Compiler

Page 27: Run Node Run

Runtime Profiler

Page 28: Run Node Run

Optimizing Compiler

Page 29: Run Node Run

Deoptimization Support

Page 30: Run Node Run

JavaScript ->Hydrogen ->Lithium ->(Native)

Page 31: Run Node Run

Profiling Node

Page 32: Run Node Run

$ node --prof server.js

Page 33: Run Node Run

Note: Your benchmark will be slower.

Page 34: Run Node Run

$ 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

[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

c4milo/node-webkit-agent

Page 37: Run Node Run

$ 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

More Profiling Options

Page 39: Run Node Run

--trace_opt

• Shows code optimizations.• Good for spotting hot code.

Page 40: Run Node Run

--trace_deopt

• Shows code de-optimizations.• Good for spotting type information changes in

hot code.

Page 41: Run Node Run

“If you ain’t first, you’re last!”

Page 42: Run Node Run

The plumbing should work like greased lightning.

Page 43: Run Node Run

You can’t control everything.

Page 44: Run Node Run

Fail with grace.

Page 45: Run Node Run

Benchmark regularly.

Page 47: Run Node Run

Adios!

@kevinswiber [email protected]


Top Related