Skip to content

Instantly share code, notes, and snippets.

@gleicon
Created July 26, 2012 02:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gleicon/3179988 to your computer and use it in GitHub Desktop.
Save gleicon/3179988 to your computer and use it in GitHub Desktop.
python, phantomjs and yslow
import sys, os, logging, subprocess, json
import gevent
logging.basicConfig(level=logging.DEBUG)
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
YSLOW = os.path.join(APP_ROOT, 'js/yslow.js')
PHANTOMJS = "/usr/local/bin/phantomjs"
def yslow(url):
try:
f = subprocess.Popen([PHANTOMJS, YSLOW, "--info grade", url], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=False)
out = f.communicate()[0]
return json.loads(out)
except Exception, e:
logging.fatal("Error parsing message: %s" % e)
def main():
if len(sys.argv) < 2:
print "needs url"
sys.exit(-1)
url = sys.argv[1]
d = yslow(url)
scores = d['g'].keys()
print "Load time: %s" % d['lt']
print "Size: %s" % d['w']
print "Overall score: %s" % d['o']
for k in scores:
print "%s : %s" % (k, d['g'][k].get(u'score', None))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment