Slide 1

Slide 1 text

MILLISECONDS TO MILLIONS! Benchmarking & Optimization for fun and profit!!!

Slide 2

Slide 2 text

HELLO WORLD! SYRIAN-CANADIAN, ENTREPRENEUR, HACKER, DEVELOPER, OPEN SOURCE ADVOCATE & DOG LOVER @AhmadNassri http://ahmad.codes Principal Architect at TELUS Digital, Founder at Bench CI, Founder at Tech Masters, Mentor at Node School Toronto, Board Member at Full Stack Toronto, Organizer at Functions Conf, Editor at The RESTful Web. Tech Outlaw, Wanted by a third-world dictator (true story)

Slide 3

Slide 3 text

AGENDA What is benchmarking? Why should you care? When & Where to start How!? Q&A OUTCOME Get excited about performance & optimization Learn about the tools, tricks and Gotchas! Benchmark all the things!!! Save your company $$$

Slide 4

Slide 4 text

THE WHAT

Slide 5

Slide 5 text

PERFORMANCE! ... MAYBE?

Slide 6

Slide 6 text

PERFORMANCE ON THE FRONT END RAIL: A User-Centric Model For Performance

Slide 7

Slide 7 text

PERFORMANCE ENGINEERING Response time The amount of time that it takes for a server to respond to a request Throughput The number of requests that can be served by your application per unit time Resource utilization CPU, memory, disk I/O, and network I/O Workload The total number of users and concurrent active users, data volumes, and transaction volumes

Slide 8

Slide 8 text

WHY SHOULD YOU CARE?

Slide 9

Slide 9 text

BANK SCENARIO Consider a system that processes CAD GBP transactions Throughput of 1 Billion CAD GBP worth of transactions per minute You are the best coder in the world, but you're not perfect Your error rate is 0.1% A single bug could cost the company millions, but it's detectable Performance degradation could cost billions! Not easily detectable

Slide 10

Slide 10 text

WHY SHOULD YOU CARE?

Slide 11

Slide 11 text

WHY SHOULD YOU CARE?

Slide 12

Slide 12 text

WHY SHOULD YOU CARE? MEASURE PERFORMANCE Code quality is not enough, keep track of performance and efficient resource usage. REDUCE RESOURCE CONSUMPTION Memory capacity & CPU cycles are precious in resource constrained environments SHIP WITH CONFIDENCE Ensure your software operates on and within any hardware & resource constraints.

Slide 13

Slide 13 text

WHEN & WHERE

Slide 14

Slide 14 text

WHEN & WHERE TO MEASURE PERFORMANCE?

Slide 15

Slide 15 text

MOST COMMON...

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

BENCHMARKING & PROFILING DO IT AS EARLY AS POSSIBLE

Slide 18

Slide 18 text

OK, HOW? FINALLY!

Slide 19

Slide 19 text

BENCHMARKING IT'S EASY! Discover optimizations that could impact the largest number of users

Slide 20

Slide 20 text

$ time for a in {1..10}; do node index.js; done real 0m13.042s user 0m0.021s sys 0m0.044s IT'S SUPER EASY! real: time from start to finish. (all elapsed time including time slices used by other processes + process block) user: actual CPU time used in executing the process sys: CPU time spent in system calls (within the kernel)

Slide 21

Slide 21 text

FUN IN THE BROWSER

Slide 22

Slide 22 text

LET'S GO DEEPER! BENCHMARK.JS https://benchmarkjs.com

Slide 23

Slide 23 text

new Benchmark.Suite() .add({ name: 'JSON.parse', fn: () => JSON.parse(json) }) .add({ name: 'jju', fn: () => jju.parse(json) }) ... .add({ name: 'vuvuzela', fn: () => vuvuzela.parse(json) }) .run() BENCHMARK.JS clarinet x 2,636 ops/sec ±2.13% (78 runs sampled) JSON.parse x 80,639 ops/sec ±2.14% (88 runs sampled) jju x 7,343 ops/sec ±3.12% (84 runs sampled) jsonparse x 10,719 ops/sec ±4.54% (45 runs sampled) json-parse-stream x 2,039 ops/sec ±6.39% (69 runs sampled) stream-json x 1,775 ops/sec ±1.14% (82 runs sampled) vuvuzela x 15,644 ops/sec ±4.07% (87 runs sampled)

Slide 24

Slide 24 text

PROFILING! OKAY, THIS ONE IS NOT AS EASY

Slide 25

Slide 25 text

BROWSER: BUILT IN PROFILER!

Slide 26

Slide 26 text

app.get('/auth', function (req, res) { const username = req.query.username || '' const password = req.query.password || '' username = username.replace(/[!@#$%^&*]/g, '') if (!username || !password || !users[username]) { return res.sendStatus(400) } const hash = crypto.pbkdf2Sync(password, users[username].salt, 10000, 512) if (users[username].hash.toString() === hash.toString()) { res.sendStatus(200) } else { res.sendStatus(401) } }) NODE.JS PROFILER

Slide 27

Slide 27 text

[Summary]: ticks total nonlib name 79 0.2% 0.2% JavaScript 36703 97.2% 99.2% C++ 7 0.0% 0.0% GC 767 2.0% Shared libraries 215 0.6% Unaccounted NODE.JS PROFILER $ node --prof ./app.js $ node --prof-process ./log-file [C++]: ticks total nonlib name 19557 51.8% 52.9% node::crypto::PBKDF2(v8::FunctionCallbackInfo const&) 4510 11.9% 12.2% _sha1_block_data_order 3165 8.4% 8.6% _malloc_zone_malloc

Slide 28

Slide 28 text

[Bottom up (heavy) profile] ticks parent name 19557 51.8% node::crypto::PBKDF2(v8::FunctionCallbackInfo const&) 19557 100.0% v8::internal::Builtins::~Builtins() 19557 100.0% LazyCompile: ~pbkdf2 crypto.js:557:16 4510 11.9% _sha1_block_data_order 4510 100.0% LazyCompile: *pbkdf2 crypto.js:557:16 4510 100.0% LazyCompile: *exports.pbkdf2Sync crypto.js:552:30 3165 8.4% _malloc_zone_malloc 3161 99.9% LazyCompile: *pbkdf2 crypto.js:557:16 3161 100.0% LazyCompile: *exports.pbkdf2Sync crypto.js:552:30 NODE.JS PROFILER

Slide 29

Slide 29 text

NODE.JS PROFILER app.get('/auth', function (req, res) { const username = req.query.username || '' const password = req.query.password || '' username = username.replace(/[!@#$%^&*]/g, '') if (!username || !password || !users[username]) { return res.sendStatus(400); } crypto.pbkdf2(password, users[username].salt, 10000, 512, function (err, hash) { if (users[username].hash.toString() === hash.toString()) { res.sendStatus(200) } else { res.sendStatus(401) } }) })

Slide 30

Slide 30 text

PRO MODE++

Slide 31

Slide 31 text

GOTCHAS

Slide 32

Slide 32 text

TRANSPILERS!

Slide 33

Slide 33 text

BENCHMARKING != MONITORING

Slide 34

Slide 34 text

SYSTEM CALLS!

Slide 35

Slide 35 text

DEPENDENCIES

Slide 36

Slide 36 text

END-2-END

Slide 37

Slide 37 text

THANK YOU! @AhmadNassri http://ahmad.codes Slides & Links: ahmad.codes/talks TechMasters.chat Functions.world FSTO.co