-
-
Save tonywangcn/42824ec4f5e3795a7c7329f1ad5e99a6 to your computer and use it in GitHub Desktop.
python, phantomjs and yslow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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