40
Owning Web Performance With PhantomJS 2 @wesleyhales

Owning Web Performance with PhantomJS 2 - Fluent 2016

Embed Size (px)

Citation preview

Page 1: Owning Web Performance with PhantomJS 2 - Fluent 2016

Owning Web Performance With PhantomJS 2

@wesleyhales

Page 2: Owning Web Performance with PhantomJS 2 - Fluent 2016

How long does it take the page to load?

@wesleyhales

Page 3: Owning Web Performance with PhantomJS 2 - Fluent 2016

1999 20152008

HTTP/1.1

WebSocketSSE

HTTP/22006 2009

SPDY

2005

AJAX

2000

REST

Delivering web resources to the browser

@wesleyhales

Page 4: Owning Web Performance with PhantomJS 2 - Fluent 2016

@wesleyhales

Page 5: Owning Web Performance with PhantomJS 2 - Fluent 2016

Loading everything and presenting something to the user

@wesleyhales

Page 6: Owning Web Performance with PhantomJS 2 - Fluent 2016

Web applications have become more complex…

@wesleyhales

Page 7: Owning Web Performance with PhantomJS 2 - Fluent 2016

Like this…

@wesleyhales

Page 8: Owning Web Performance with PhantomJS 2 - Fluent 2016

Understanding the basics

@wesleyhales

Page 9: Owning Web Performance with PhantomJS 2 - Fluent 2016

@wesleyhales

Page 10: Owning Web Performance with PhantomJS 2 - Fluent 2016

Fast is good… Faster is better

Google Dev Summit

@wesleyhales

Page 11: Owning Web Performance with PhantomJS 2 - Fluent 2016

Performance Reality

@wesleyhales

Page 12: Owning Web Performance with PhantomJS 2 - Fluent 2016

@wesleyhales

Page 13: Owning Web Performance with PhantomJS 2 - Fluent 2016

How do we measure web performance today?

@wesleyhales

Page 14: Owning Web Performance with PhantomJS 2 - Fluent 2016

Date.now() || Date.getTime()

• When did JavaScript execution begin? • <HEAD> vs. </BODY> • Get the current time • Profit • Example: Simple.html

@wesleyhales

Page 15: Owning Web Performance with PhantomJS 2 - Fluent 2016

However… • JavaScript time is skewed by adjustments to

the system clock

• NTP adjustments, leap seconds, user configuration changes, and so on can cause time inaccuracies.

• It can’t provide any data regarding the server, network, and so on.

@wesleyhales

Page 16: Owning Web Performance with PhantomJS 2 - Fluent 2016

Demo: Basic Loading and Blockingall-old.html

@wesleyhales

Page 17: Owning Web Performance with PhantomJS 2 - Fluent 2016

DomContentLoaded

• Document has been completely loaded and parsed.

• Stylesheets, images, and subframes have not finished loading

document.addEventListener('DOMContentLoaded', function () { console.log('DOMContentLoaded', new Date().getTime()); });

@wesleyhales

Page 18: Owning Web Performance with PhantomJS 2 - Fluent 2016

load || unload

• Every modern framework and app uses it.

• The load event is fired when a resource and its dependent resources have finished loading.

• The load event is easy to measure. Unfortunately, it isn't a very good indicator of the actual end-user experience.

window.addEventListener('load', function (event) {

Page 19: Owning Web Performance with PhantomJS 2 - Fluent 2016

readyState • "loading" while the

document is loading

• "interactive" once it is finished parsing (but still loading sub-resources)

• "complete" once it has loaded

document.onreadystatechange = function () { if (document.readyState == "interactive") { … }else if (document.readyState == "loading") { … }else if (document.readyState == "complete") { … } };

Page 20: Owning Web Performance with PhantomJS 2 - Fluent 2016

readyState == “complete” • This will

usually include any activity that is triggered by javascript after the main page loads.

document.onreadystatechange = function () { if (document.readyState == "interactive") { … }else if (document.readyState == "loading") { … }else if (document.readyState == "complete") { … } };

all-old.html (again)@wesleyhales

Page 21: Owning Web Performance with PhantomJS 2 - Fluent 2016

Loadreport.js (2012-2015)

Page 22: Owning Web Performance with PhantomJS 2 - Fluent 2016

Remember this…

@wesleyhales

Page 23: Owning Web Performance with PhantomJS 2 - Fluent 2016

Enter Navigation Timing API (The starting point of Web

Performance APIs)

@wesleyhales

Page 24: Owning Web Performance with PhantomJS 2 - Fluent 2016

Navigation is about how user agents convert the requested

HTML, CSS, and JavaScript into rendered pixels, which is one of the most critical steps

for users to navigate a document.

@wesleyhales

Page 25: Owning Web Performance with PhantomJS 2 - Fluent 2016

@wesleyhales

Page 26: Owning Web Performance with PhantomJS 2 - Fluent 2016

window.performance.timing process model

@wesleyhales

Page 27: Owning Web Performance with PhantomJS 2 - Fluent 2016

http://www.w3.org/2015/10/webperf-all.html

@wesleyhales

Page 28: Owning Web Performance with PhantomJS 2 - Fluent 2016

HRT

• All measurements are at microsecond precision.

• Does not depend on system clock

• The idea of High Resolution Time is to provide a monotonic, uniformly increasing timestamp suitable for interval measurements

• Example: perf.now.html

performance.now()

@wesleyhales

Page 29: Owning Web Performance with PhantomJS 2 - Fluent 2016

Demo: window.performancesimple-new.html

@wesleyhales

Page 30: Owning Web Performance with PhantomJS 2 - Fluent 2016

• Released Early 2015

• Headless Web Browser

• Based on WebKit

PhantomJS 2

@wesleyhales

Page 31: Owning Web Performance with PhantomJS 2 - Fluent 2016

PhantomJS 2 Feature Detect

Page 32: Owning Web Performance with PhantomJS 2 - Fluent 2016

Demo: Basic PhantomJS Scripts

http://phantomjs.org/examples/

@wesleyhales

Page 33: Owning Web Performance with PhantomJS 2 - Fluent 2016

@wesleyhales

Page 34: Owning Web Performance with PhantomJS 2 - Fluent 2016

• Rewrite of loadreport.js

• Leverages all implemented PhantomJS 2 Navigation Timing APIs

• (shims resource timing)

Speedgun.js

Page 35: Owning Web Performance with PhantomJS 2 - Fluent 2016

Demo: Speedgun.js

@wesleyhales

Page 36: Owning Web Performance with PhantomJS 2 - Fluent 2016

speedgun.io

@wesleyhales

Page 37: Owning Web Performance with PhantomJS 2 - Fluent 2016

Synthetic RUM • Use Speedgun.io as

centralized server • All docker nodes send

beacon with: • Current container CPU

and memory usage • Yes, another demo...

http://wesleyhales.com/blog/2015/04/24/Speedgun/

Page 38: Owning Web Performance with PhantomJS 2 - Fluent 2016

Synthetic RUM • The ability to execute a script (PhantomJS in this case) in a

controlled, one at a time manner.

• A REST api that allows master/slave communication and is publicly accessible.

• A beacon that sends it’s availability to a centralized(parent) server.

• Data storage of reports in a db that treats JSON as a first class citizen.

http://wesleyhales.com/blog/2015/04/24/Speedgun/

Page 39: Owning Web Performance with PhantomJS 2 - Fluent 2016

http://w3c.github.io/perf-timing-primer/

W3C Performance Specs

Page 40: Owning Web Performance with PhantomJS 2 - Fluent 2016

Thanks!!

• speedgun.io (github) • Navigation Timing API • Navigation Timing 2 • Resource Timing API

@wesleyhales