+ All Categories
Home > Technology > Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Date post: 18-May-2015
Category:
Upload: philip-tellis
View: 1,428 times
Download: 1 times
Share this document with a friend
Description:
While building boomerang, we developed many interesting methods to measure network performance characteristics using JavaScript running in the browser. While the W3C's NavigationTiming API provides access to many performance metrics, there's far more you can get at with some creative tweaking and analysis of how the browser reacts to certain requests. In this talk, I'll go into the details of how boomerang works to measure network throughput, latency, TCP connect time, DNS time and IPv6 connectivity. I'll also touch upon some of the other performance related browser APIs we use to gather useful information. http://www.nywebperformance.org/events/78566362/
Popular Tags:
59
Philip Tellis .com [email protected] @bluesmoon geek paranoid speedfreak http://bluesmoon.info/ NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 1
Transcript
Page 1: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

• Philip Tellis

• .com• [email protected]

• @bluesmoon• geek paranoid speedfreak• http://bluesmoon.info/

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 1

Page 2: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

I <3 JavaScript

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 2

Page 3: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

So much that I wore Mustache socks to my wedding

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 3

Page 4: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

I’m a Speedfreak (the network kind)

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 4

Page 5: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

We measure real user website performance

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 5

Page 6: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

This talk is (mostly) about how we abuse JavaScript to do it

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 6

Page 7: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Abusing JavaScript to Measure WebPerformance

Philip Tellis / [email protected]

NY #WebPerf Meetup / 2012-09-13

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 7

Page 8: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

First, a note about the code

Note that in the code that follows,

+ new Date

is equivalent to

new Date().getTime()

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 8

Page 9: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1Latency

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 9

Page 10: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Arrange the following in order of increasing signal latency overa fixed distance:• Electricity through copper• Smoke Signals• Light through Fibre• Pulsar

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 10

Page 11: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1 Blinking Lights

It takes about 16ms for light to get from SF to NYC(24ms through fibre)

. . .

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 11

Page 12: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1 Blinking Lights

It takes about 16ms for light to get from SF to NYC(24ms through fibre)

Though it takes about 100ms to ping... why?

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 11

Page 13: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1 HTTP

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 12

Page 14: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

So to measure latency, we need to send 1 packet each way, andtime it

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 13

Page 15: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1 Network latency in JavaScript

var ts, rtt, img = new Image;img.onload=function() { rtt=(+new Date - ts) };ts = +new Date;img.src="/1x1.gif";

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 14

Page 16: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

1 Notes

• 1x1 gif is 35 bytes• including HTTP headers, is smaller than a TCP packet• Fires onload on all browsers• 0 byte image fires onerror• which is indistinguishable from network error

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 15

Page 17: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

2TCP handshake

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 16

Page 18: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

2 ACK-ACK-ACK

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 17

Page 19: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

2 Connection: keep-alive

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 18

Page 20: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

2 Network latency & TCP handshake in JavaScript

var t=[], tcp, rtt;var ld = function() {

t.push(+new Date);if(t.length > 2) // run 2 times

done();else {var img = new Image;img.onload = ld;img.src="/1x1.gif?" + Math.random()

+ ’=’ + new Date;}

};var done = function() {

rtt=t[2]-t[1];tcp=t[1]-t[0]-rtt;

};ld();

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 19

Page 21: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Notice that we’ve ignored DNS lookup time here... how wouldyou measure it?

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 20

Page 22: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3Network Throughput

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 21

Page 23: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

data_lengthdownload_time

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 22

Page 24: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Should you fly a 747 or a 737?

• A 747 seats 400+ passengers• A 737 seats about 150• Both take about the same time to fly from SFO to BOS• A 747 takes longer to load and unload than 3 737s in

parallel

The best selling aircraft to date is the 737

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 23

Page 25: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Network Throughput in JavaScript

// Assume global object// image={ url: ..., size: ... }var ts, rtt, bw, img = new Image;img.onload=function() {

rtt=(+new Date - ts);bw = image.size*1000/rtt; // rtt is in ms

};ts = +new Date;img.src=image.url;

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 24

Page 26: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

If it were that simple, I wouldn’t be doing this talk.

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 25

Page 27: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 TCP Slow Start

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 26

Page 28: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

So to make the best use of bandwidth, we need resources that fitin a TCP window

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 27

Page 29: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 There is no single size that will tax all available networks

http://www.yuiblog.com/blog/2010/04/08/analyzing-bandwidth-and-latency/

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 28

Page 30: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Network Throughput in JavaScript – Take 2

// image object is now an array of multiple imagesvar i=0;var ld = function() {

if(i>0)image[i-1].end = +new Date;

if(i >= image.length)done();

else {var img = new Image;img.onload = ld;image[i].start = +new Date;img.src=image[i].url;

}i++;

};

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 29

Page 31: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

Slow network connection, meet really huge image

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 30

Page 32: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Network Throughput in JavaScript – Take 3

var img = new Image;img.onload = ld;image[i].start = +new Date;image[i].timer =

setTimeout(function() {image[i].expired=true

},image[i].timeout);

img.src=image[i].url;

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 31

Page 33: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Network Throughput in JavaScript – Take 3

if(i>0) {image[i-1].end = +new Date;clearTimeout(image[i-1].timer);

}

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 32

Page 34: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Network Throughput in JavaScript – Take 3

if(i >= image.length|| (i > 0 && image[i-1].expired)) {

done();}

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 33

Page 35: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

Are we done yet?

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 34

Page 36: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

Are we done yet?sure...

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 34

Page 37: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

Except network throughput is different every time you test it

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 35

Page 38: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Statistics to the Rescue

flickr/sophistechate/4264466015/

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 36

Page 39: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

3 Measuring Network Throughput

The code for that is NOT gonna fit on a slide

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 37

Page 40: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

But this is sort of what we see world-wide

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 38

Page 41: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

And it’s different for different countries

This is India

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 39

Page 42: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4DNS

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 40

Page 43: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS

time_with_dns − time_without_dns

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 41

Page 44: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS in JavaScript

var t=[], dns, ip, hosts=[’http://hostname.com/’,’http://ip.ad.dr.ess/’];

var ld = function() {t.push(+new Date);if(t.length > hosts.length)done();

else {var img = new Image;img.onload = ld;img.src=hosts[t.length-1] + "/1x1.gif";

}};var done = function() {

ip=t[2]-t[1];dns=t[1]-t[0]-ip;

};ld();

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 42

Page 45: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS

• What if the IP changes?• What if DNS is cached?• What if you map DNS based on geo location?

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 43

Page 46: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Wildcard DNS Entries

*.foo.com→ IP address

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 44

Page 47: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS in JavaScript – take 2

var base_url="http://*.foo.com/",timers = {}, gen_url="";

function start() {var random = Math.random().toString(36),

cache_bust = Math.random(),img = new Image();

gen_url = base_url.replace(/\*/, random);

img.onload = A_loaded;

timers.start = +new Date;img.src = gen_url + "image-l.gif?t=" + cache_bust;

}

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 45

Page 48: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS in JavaScript – take 2

function A_loaded() {var cache_bust = Math.random(),

img = new Image();

img.onload = B_loaded;

timers.a_loaded = +new Date;img.src = gen_url + "image-l.gif?t=" + cache_bust;

}

I’ll let you figure out B_loaded

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 46

Page 49: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

4 Measuring DNS

Full code in boomerang’s DNS plugin

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 47

Page 50: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

5IPv6

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 48

Page 51: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

5 Measuring IPv6 support and latency

1 Try to load image from IPv6 host• If timeout or error, then no IPv6 support• If successful, then calculate latency and proceed

2 Try to load image from hostname that resolves only to IPv6host

• If timeout or error, then DNS server doesn’t support IPv6• If successful, calculate latency

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 49

Page 52: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

5 Measuring IPv6 support and latency

Full code in boomerang’s IPv6 pluginNote, only run this if you know IPv6 is supported by the client

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 50

Page 53: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

6Other Stuff

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 51

Page 54: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

6 Other Stuff We Measure

• NavTiming – navtiming.js

• navigation.connection.type – mobile.js

• window.performance.memory – memory.js –Chrome 22 reporting now.

• Number of DOM nodes and byte size of HTML –memory.js

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 52

Page 55: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

And we try to do it fast

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 53

Page 56: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

–.done()

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 54

Page 57: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Code/References

• http://lognormal.github.com/boomerang/doc/(BSD Licensed)

• www.lognormal.com

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 55

Page 58: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

• Philip Tellis

• .com• [email protected]

• @bluesmoon• geek paranoid speedfreak• http://bluesmoon.info/

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 56

Page 59: Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"

Thank youAsk me about a discount code

NY #WebPerf Meetup / 2012-09-13 Abusing JavaScript to Measure Web Performance 57


Recommended