From d676d0a80e1fc16b45142dff8d556ef97d6e48d0 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Tue, 17 Apr 2018 10:15:08 +0300
Subject: [PATCH] Added Lighthouse and Sitespeed.io tests (Fabric tasks).

---
 README.md  |  5 +++++
 fabfile.py | 37 ++++++++++++++++++++++++++++++++++---
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 538c53b..8d1d174 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,9 @@
 
 - Python 3.6
 - [Pipenv](https://pipenv.org)
+- [Docker](https://www.docker.com/) (Only needed for running the lighthouse and
+  sitespeed tests)
+- [direnv](http://direnv.net/) (Just for ease of use)
 
 ## Usage
 
@@ -17,10 +20,12 @@ Available commands:
     build       Build local version of site
     clean       Remove generated files
     dev         Auto-regenerate files and serve at http://localhost:8080/
+    lighthouse  Run Chrome's Lighthouse report against the local dev server
     preview     Build production version of site
     publish     Publish to production via rsync
     regenerate  Automatically regenerate site upon file modification
     serve       Serve site at http://localhost:8080/
+    sitespeed   Run sitespeed test against the local dev server
 ```
 
 
diff --git a/fabfile.py b/fabfile.py
index fc2c611..e5ebef5 100644
--- a/fabfile.py
+++ b/fabfile.py
@@ -1,7 +1,9 @@
-from fabric.api import lcd, env, local, hosts
+from fabric.api import lcd, env, local, hosts, warn_only
 import fabric.contrib.project as project
 import multiprocessing
+import os
 
+env.use_ssh_config = True
 # Local path configuration (can be absolute or relative to fabfile)
 env.deploy_path = 'output'
 DEPLOY_PATH = env.deploy_path
@@ -10,10 +12,12 @@ DEPLOY_PATH = env.deploy_path
 production = 'www.shore.co.il'
 dest_path = '/var/www/htdocs/www.shore.co.il/blog/'
 
+UID = os.getuid()
+
 
 def clean():
     """Remove generated files"""
-    local(f'rm -r __pycache__/ {DEPLOY_PATH}/*')
+    local(f'rm -rf __pycache__/ {DEPLOY_PATH}/* sitespeed-result/ lighthouse-result/')
 
 
 def build():
@@ -34,9 +38,11 @@ def serve():
 
 def dev():
     """Auto-regenerate files and serve at http://localhost:8080/"""
-    server_process = multiprocessing.Process(target=serve)
+    server_process = multiprocessing.Process(target=serve, daemon=True)
     server_process.start()
     regenerate()
+    server_process.terminate()
+    server_process.join(timeout=3)
 
 
 def preview():
@@ -55,3 +61,28 @@ def publish():
         delete=True,
         extra_opts='-c',
     )
+
+
+def sitespeed():
+    """Run sitespeed test against the local dev server"""
+    build()
+    with warn_only():
+        local('docker run --rm --privileged --net=host gliderlabs/hostlocal')
+    server_process = multiprocessing.Process(target=serve, daemon=True)
+    server_process.start()
+    local(f'docker run --rm --shm-size=1g -u {UID} -v "$PWD:/sitespeed.io" sitespeedio/sitespeed.io http://169.254.255.254:8080/')
+    server_process.terminate()
+    server_process.join(timeout=3)
+
+
+def lighthouse():
+    """Run Chrome's Lighthouse report against the local dev server"""
+    build()
+    with warn_only():
+        local('docker run --rm --privileged --net=host gliderlabs/hostlocal')
+    server_process = multiprocessing.Process(target=serve, daemon=True)
+    server_process.start()
+    local('mkdir -p lighthouse-result')
+    local('docker run --rm -u {UID} -v "$PWD/lighthouse-result:/home/chrome/reports" --cap-add=SYS_ADMIN --user=1000 justinribeiro/lighthouse lighthouse --chrome-flags="--headless --no-sandbox --disable-gpu" http://169.254.255.254:8080/')
+    server_process.terminate()
+    server_process.join(timeout=3)
-- 
GitLab