Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automating Web Performance

Dean Hume
October 28, 2015

Automating Web Performance

This workshop was first presented at OSCON Conference on the 28th October 2015. It was a joint workshop with Dean Hume (http://deanhume.com/) and Robin Osborne (http://robinosborne.co.uk/)

http://conferences.oreilly.com/oscon/open-source-eu-2015

Presentation Details
----------------------------------

According to the HTTP Archive, the average web page consumes about 2 MB of total transfer size. This worrying trend is growing year on year, and as developers we need to ensure that our web pages are as mean and lean as possible. The easiest way to do this and ensure that our web pages forever stay lean is to automate our web performance workflow. As the famous meme goes – Automate all the things!

If you are lazy developers like us, then you will understand the need to automate as much of your development workflow as possible. Automation makes life easier and reduces the need to manually recreate your steps every time. However, when it comes to the different automation options out there – where do you even begin?

There are amazing open source tools that can make life a lot easier. In this talk we will run through various open source tools and libraries, and a step-by-step automation example covering web techniques, such as:

- Image compression and optimization
- Responsive images
- WebP images
- Removing unused CSS
- Critical path CSS
- Testing and benchmarking
- How to integrate this all in a continuous integration process
- Build, deployment, hosting, scaling

By the end of the session, we hope that developers will have the knowledge required to set up and automate the performance workflow of their websites. Engineers will look to attend this presentation because they can learn how to automate the performance of their websites; simple improvements can really go a long way toward improving performance.

For more information on the code repository used in this workshop, please checkout - https://github.com/rposbo/bulky-bricks-inc

Dean Hume

October 28, 2015
Tweet

More Decks by Dean Hume

Other Decks in Programming

Transcript

  1. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  2. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  3. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  4. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  5. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  6. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  7. RULES Make Fewer HTTP Requests Use a Content Delivery Network

    Add an Expires Header Gzip Components Put Stylesheets at the Top Put Scripts at the Bottom
  8. 53

  9. 764 15,758 145 1,283,067 1,691 Grunt Gulp 601 9,883 55

    1,356,374 4,403 Watchers Stars Contributors Downloads Plugins August 2015
  10. NPM

  11. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ cssmin: { dist:

    { files: [ { src: 'stylesheets/about.css', dest: 'stylesheets/about.min.css' }, { src: 'stylesheets/articles.css', dest: 'stylesheets/articles.min.css' }, ] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-cssmin'); // Default tasks. grunt.registerTask('default', ['cssmin']); };
  12. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ cssmin: { dist:

    { files: [ { src: 'stylesheets/about.css', dest: 'stylesheets/about.min.css' }, { src: 'stylesheets/articles.css', dest: 'stylesheets/articles.min.css' }, ] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-cssmin'); // Default tasks. grunt.registerTask('default', ['cssmin']); };
  13. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ cssmin: { dist:

    { files: [ { src: 'stylesheets/about.css', dest: 'stylesheets/about.min.css' }, { src: 'stylesheets/articles.css', dest: 'stylesheets/articles.min.css' }, ] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-cssmin'); // Default tasks. grunt.registerTask('default', ['cssmin']); };
  14. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ cssmin: { dist:

    { files: [ { src: 'stylesheets/about.css', dest: 'stylesheets/about.min.css' }, { src: 'stylesheets/articles.css', dest: 'stylesheets/articles.min.css' }, ] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-cssmin'); // Default tasks. grunt.registerTask('default', ['cssmin']); };
  15. Images are the biggest part of a web page Stylesheets,

    HTML, etc Images 60% JavaScript source: httparchive.org
  16. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ imagemin: { dist:

    { files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-imagemin'); // Default tasks. grunt.registerTask('default', ['imagemin']); };
  17. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ imagemin: { dist:

    { files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-imagemin'); // Default tasks. grunt.registerTask('default', ['imagemin']); };
  18. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ imagemin: { dist:

    { files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-imagemin'); // Default tasks. grunt.registerTask('default', ['imagemin']); };
  19. Gruntfile.js module.exports = function (grunt) { grunt.initConfig({ imagemin: { dist:

    { files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) // Load the plugins grunt.loadNpmTasks('grunt-contrib-imagemin'); // Default tasks. grunt.registerTask('default', ['imagemin']); };
  20. 1. npm install <imagemin plugin> 2. var plugin = require('<imagemin

    plugin>'); 3. options: {use: [plugin( {config} )]} grunt-contrib-imagemin
  21. var moz = require('imagemin-mozjpeg'); grunt.initConfig({ imagemin: { dist: { options:

    { use: [moz( {quality:80, quantTable: 3} )] }, files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) Gruntfile.js
  22. var zopfli = require('imagemin-zopfli'); grunt.initConfig({ imagemin: { dist: { options:

    { use: [zopfli( {more: true} )] }, files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) Gruntfile.js
  23. grunt.initConfig({ imagemin: { dist: { options: { optimizationLevel: 3, //

    default values, so is not even necessary }, files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif}'], dest: 'images/dist/' }] } } }) Gruntfile.js
  24. grunt.initConfig({ imagemin: { dist: { options: { svgoPlugins: [{ removeViewBox:

    true }] }, files: [{ expand: true, cwd: 'images/', src: ['*.{png,jpg,gif,svg}'], dest: 'images/dist/' }] } } }) Gruntfile.js
  25. 70%

  26. ?

  27. 86

  28. webp:{ png: { files:[{ expand: true, cwd: "images/", src: "*.png",

    dest: "images/dist/" }], options: { binpath: "tools/cwebp.exe", preset: 'picture', verbose: true, quality: 80, alphaQuality: 80, compressionMethod: 6, …….. } } Gruntfile.js
  29. 72%

  30. Gruntfile.js responsive_images: { images: { options: { engine: 'im', sizes:

    [ { name: 'medium', width: 300 },{ name: 'large', width: 500, }] }, files: { src: '../before/images/tie-fighter-large.jpg' , dest: 'images/tie-fighter.jpg'} } }
  31. Gruntfile.js responsive_images: { images: { options: { engine: 'im', sizes:

    [ { name: 'medium', width: 300 },{ name: 'large', width: 500, }] }, files: { 'images/tie-fighter.jpg': '../before/images/tie-fighter-large.jpg' } } }
  32. Gruntfile.js responsive_images: { images: { options: { engine: 'im', sizes:

    [ { name: 'medium', width: 300 },{ name: 'large', width: 500, }] }, files: { 'images/tie-fighter.jpg': '../before/images/tie-fighter-large.jpg' } } }
  33. 77%

  34. /* This is a comment */ h1 { font-size: 30px;

    line-height: 36px; } h1 small { font-size: 18px; /* Another comment */ } CSS
  35. Clean your CSS .foo { background: url('images/test.jpg'); width: 100px; }

    .bar { display: block; } .baz { background: url('images/test.jpg'); width: 110px; }
  36. 25%

  37. cssmin: { dist: { files: [{ expand: true, cwd: 'release/css',

    src: ['*.css', '!*.min.css'], dest: 'release/css', ext: '.min.css' }] } } Gruntfile.js
  38. uncss: { dist: { options: { ignore: ['#header', 'mdl-layout__header'] },

    files: { 'css/result.css': ['index.html'] } } } Gruntfile.js
  39. uncss: { dist: { options: { ignore: ['#header', 'mdl-layout__header'] },

    files: { 'css/result.css': ['index.html'] } } } Gruntfile.js
  40. <!doctype html> <head> <style> /* inlined critical CSS */ </style>

    <script> loadCSS('deferred.css'); </script> </head> <body> ...body goes here </body> </html>
  41. <!doctype html> <head> <style> /* inlined critical CSS */ </style>

    <script> loadCSS('deferred.css'); </script> </head> <body> ...body goes here </body> </html>
  42. critical: { dist: { options: { base: './', minify: true,

    dimensions: [ { width: 1300, height: 900 }, { width: 500, height: 900 }] }, files: { src: 'index.html', dest: ['index.html'] } } } Gruntfile.js
  43. Gruntfile • grunt-contrib-imagemin ◦ imagemin-mozjpeg ◦ imagemin-zopfli • grunt-webp •

    grunt-responsive-images • grunt-contrib-cssmin • grunt-critical
  44. ?

  45. 94

  46. /** * Upgrades a specific element rather than all in

    the DOM. * @param {HTMLElement} element The element we wish to upgrade. * @param {string} jsClass The name of the class we want to upgrade * the element to. */ function upgradeElementInternal(element, jsClass) { // Only upgrade elements that have not already been upgraded. var dataUpgraded = element.getAttribute('data-upgraded'); if (dataUpgraded === null || dataUpgraded.indexOf(jsClass) === -1) { // Upgrade element. if (dataUpgraded === null) { dataUpgraded = ''; } element.setAttribute('data-upgraded', dataUpgraded + ',' + jsClass); …. JS
  47. function upgradeElementInternal(a,b){var c=a.getAttribute("data- upgraded");if(null===c||-1===c.indexOf(b)){null===c&&(c=""),a. setAttribute("data-upgraded",c+","+b);var d=findRegisteredClass_(b);if(! d)throw"Unable to find a

    registered component for the given class.";var e=new d.classConstructor(a);e[componentConfigProperty_]=d, createdComponents_.push(e),d.callbacks.forEach(function(b){b(a)}),d. widget&&(a[b]=e);var f=document.createEvent("Events");f.initEvent ("mdl-componentupgraded",!0,!0),a.dispatchEvent(f)}}
  48. uglify: { target: { files: [{ expand: true, cwd: 'src',

    src: ['*.js'], dest: 'dest', ext: '.min.js' }] } } Gruntfile.js
  49. 40%

  50. <body> <!-- Always shows a header, even in smaller screens.

    --> <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header"> <header class="mdl-layout__header"> <div class="mdl-layout__header-row"> <!-- Title --> <span class="mdl-layout-title">Bulky Bricks</span> <!-- Add spacer, to align navigation to the right --> <div class="mdl-layout-spacer"></div> <!-- Navigation. We hide it in small screens. --> <nav class="mdl-navigation mdl-layout--large-screen-only"> <a class="mdl-navigation__link" href="index.html">Home</a> HTML
  51. <body>\n <!-- Always shows a header, even in smaller screens.

    --> <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">\n <header class="mdl-layout__header">\n <div class="mdl-layout__header-row">\n <!-- Title -->\n <span class="mdl-layout-title">Bulky Bricks</span>\n <!-- Add spacer, to align navigation to the right -->\n <div class="mdl-layout-spacer"></div>\n <!-- Navigation. We hide it in small screens. -->\n <nav class="mdl-navigation mdl-layout--large-screen-only">\n <a class="mdl-navigation__link" href="index.html">Home</a>\n HTML
  52. <body> <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header"> <header class=mdl-layout__header><div class=mdl-layout__header- row> <span

    class=mdl-layout-title>Bulky Bricks</span> <div class=mdl- layout-spacer></div> <nav class="mdl-navigation mdl-layout--large- screen-only"><a class=mdl-navigation__link href=index.html>Home</a> <a class=mdl-navigation__link href=products.html>Products</a> <a class=mdl-navigation__link href=about.html>About Us</a><a class=mdl-navigation__link href=contact.html>Contact Us</a></nav> </div> </header> <div class=mdl-layout__drawer><span class=mdl- layout-title>Bulky Bricks</span><nav class=mdl-navigation>
  53. htmlmin: { target: { options: { removeComments: true, collapseWhitespace: true,

    collapseBooleanAttributes: true, removeAttributeQuotes: true }, files: { dest: 'index.min.html', src: 'index.html' } } } Gruntfile.js
  54. 30%

  55. 95

  56. Gruntfile • grunt-contrib-imagemin ◦ imagemin-mozjpeg ◦ imagemin-zopfli • grunt-webp •

    grunt-responsive-images • grunt-contrib-cssmin • grunt-critical • grunt-contrib-uglify • grunt-contrib-htmlmin
  57. ION

  58. pagespeed: { options: { nokey: true }, prod: { options:

    { url: "http://rposbo.github.io/bulky-bricks-inc/after/index.html", locale: "en_GB", strategy: "desktop", threshold: 95 } } } Gruntfile.js
  59. language: node_js node_js: - "0.12" before_install: npm install -g grunt-cli

    install: npm install script: grunt test travis.yml