blob: a1af6a241dd221f1ed9e5eb3f3d12c3a36ddc90e [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Tool for seeing the real world impact of a patch.
# Layout Tests can tell you whether something has changed, but this can help
# you determine whether a subtle/controversial change is beneficial or not.
# It dumps the rendering of a large number of sites, both with and without a
# patch being evaluated, then sorts them by greatest difference in rendering,
# such that a human reviewer can quickly review the most impacted sites,
# rather than having to manually try sites to see if anything changes.
# In future it might be possible to extend this to other kinds of differences,
# e.g. page load times.
from __future__ import print_function
import argparse
from argparse import RawTextHelpFormatter
from contextlib import closing
import datetime
import errno
from distutils.spawn import find_executable
from operator import itemgetter
import multiprocessing
import os
import re
from cStringIO import StringIO
import subprocess
import sys
import textwrap
import time
from urllib2 import urlopen
from urlparse import urlparse
import webbrowser
from zipfile import ZipFile
from nsfw_urls import nsfw_urls
action = None
allow_js = False
additional_content_shell_flags = ""
chromium_src_root = ""
chromium_out_dir = ""
image_diff = ""
content_shell = ""
output_dir = ""
num_sites = 100
urls = []
print_lock = multiprocessing.Lock()
def MakeDirsIfNotExist(dir):
except OSError as e:
if e.errno != errno.EEXIST:
def SetupPathsAndOut():
global chromium_src_root, chromium_out_dir, output_dir
global image_diff, content_shell
chromium_src_root = os.path.abspath(os.path.join(os.path.dirname(__file__),
# Find out directory (might be out_linux for users of cr).
for out_suffix in ["_linux", ""]:
out_dir = os.path.join(chromium_src_root, "out" + out_suffix)
if os.path.exists(out_dir):
chromium_out_dir = out_dir
if not chromium_out_dir:
return False
this_script_name = "real_world_impact"
output_dir = os.path.join(chromium_out_dir,
image_diff = os.path.join(chromium_out_dir, "Release", "image_diff")
if sys.platform == 'darwin':
content_shell = os.path.join(chromium_out_dir, "Release",
"Content Shell")
elif sys.platform.startswith('linux'):
content_shell = os.path.join(chromium_out_dir, "Release",
elif sys.platform.startswith('win'):
content_shell = os.path.join(chromium_out_dir, "Release",
return True
def CheckPrerequisites():
if not find_executable("wget"):
print("wget not found! Install wget and re-run this.")
return False
if not os.path.exists(image_diff):
print("image_diff not found (%s)!" % image_diff)
print("Build the image_diff target and re-run this.")
return False
if not os.path.exists(content_shell):
print("Content shell not found (%s)!" % content_shell)
print("Build Release/content_shell and re-run this.")
return False
return True
def PickSampleUrls():
global urls
data_dir = os.path.join(output_dir, "data")
# Download Alexa top 1,000,000 sites
# TODO(johnme): Should probably update this when it gets too stale...
csv_path = os.path.join(data_dir, "top-1m.csv")
if not os.path.exists(csv_path):
print("Downloading list of top 1,000,000 sites from Alexa...")
csv_url = ""
with closing(urlopen(csv_url)) as stream:
ZipFile(StringIO("top-1m.csv", data_dir)
bad_urls_path = os.path.join(data_dir, "bad_urls.txt")
if os.path.exists(bad_urls_path):
with open(bad_urls_path) as f:
bad_urls = set(
bad_urls = set()
# See if we've already selected a sample of size num_sites (this way, if you
# call this script with arguments "before N" then "after N", where N is the
# same number, we'll use the same sample, as expected!).
urls_path = os.path.join(data_dir, "%06d_urls.txt" % num_sites)
if not os.path.exists(urls_path):
if action == 'compare':
print("Error: you must run 'before %d' and 'after %d' before "
"running 'compare %d'" % (num_sites, num_sites, num_sites))
return False
print("Picking %d sample urls..." % num_sites)
# TODO(johnme): For now this just gets the top num_sites entries. In future
# this should pick a weighted random sample. For example, it could fit a
# power-law distribution, which is a good model of website popularity
# (
urls = []
remaining_num_sites = num_sites
with open(csv_path) as f:
for entry in f:
if remaining_num_sites <= 0:
remaining_num_sites -= 1
hostname = entry.strip().split(',')[1]
if not '/' in hostname: # Skip Alexa 1,000,000 entries that have paths.
url = "http://%s/" % hostname
if not url in bad_urls:
# Don't write these to disk yet; we'll do that in SaveWorkingUrls below
# once we have tried to download them and seen which ones fail.
with open(urls_path) as f:
urls = [u for u in if not u in bad_urls]
return True
def SaveWorkingUrls():
# TODO(johnme): Update the list if a url that used to work goes offline.
urls_path = os.path.join(output_dir, "data", "%06d_urls.txt" % num_sites)
if not os.path.exists(urls_path):
with open(urls_path, 'w') as f:
f.writelines(u + '\n' for u in urls)
def PrintElapsedTime(elapsed, detail=""):
elapsed = round(elapsed * 10) / 10.0
m = elapsed / 60
s = elapsed % 60
print("Took %dm%.1fs" % (m, s), detail)
def DownloadStaticCopyTask(url):
url_parts = urlparse(url)
host_dir = os.path.join(output_dir, "data", url_parts.hostname)
# Use wget for now, as does a reasonable job of spidering page dependencies
# (e.g. CSS, JS, images).
success = True
"--execute", "robots=off",
("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS "
"X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) C"
"hrome/32.0.1700.14 Safari/537.36"),
"--directory-prefix=" + host_dir,
"--timeout=5", # 5s timeout
except KeyboardInterrupt:
success = False
except subprocess.CalledProcessError:
# Ignoring these for now, as some sites have issues with their subresources
# yet still produce a renderable index.html
pass #success = False
if success:
download_path = os.path.join(host_dir, url_parts.hostname, "index.html")
if not os.path.exists(download_path):
success = False
with print_lock:
print("Downloaded:", url)
if not success:
with print_lock:
print("Failed to download:", url)
return False
return True
def DownloadStaticCopies():
global urls
new_urls = []
for url in urls:
url_parts = urlparse(url)
host_dir = os.path.join(output_dir, "data", url_parts.hostname)
download_path = os.path.join(host_dir, url_parts.hostname, "index.html")
if not os.path.exists(download_path):
if new_urls:
print("Downloading static copies of %d sites..." % len(new_urls))
start_time = time.time()
results = multiprocessing.Pool(20).map(DownloadStaticCopyTask, new_urls)
failed_urls = [new_urls[i] for i,ret in enumerate(results) if not ret]
if failed_urls:
bad_urls_path = os.path.join(output_dir, "data", "bad_urls.txt")
with open(bad_urls_path, 'a') as f:
f.writelines(u + '\n' for u in failed_urls)
failed_urls_set = set(failed_urls)
urls = [u for u in urls if u not in failed_urls_set]
PrintElapsedTime(time.time() - start_time)
def RunDrtTask(url):
url_parts = urlparse(url)
host_dir = os.path.join(output_dir, "data", url_parts.hostname)
html_path = os.path.join(host_dir, url_parts.hostname, "index.html")
if not allow_js:
nojs_path = os.path.join(host_dir, url_parts.hostname, "index-nojs.html")
if not os.path.exists(nojs_path):
with open(html_path) as f:
html =
if not html:
return False
# These aren't intended to be XSS safe :)
block_tags = (r'<\s*(script|object|video|audio|iframe|frameset|frame)'
block_attrs = r'\s(onload|onerror)\s*=\s*(\'[^\']*\'|"[^"]*|\S*)'
html = re.sub(block_tags, '', html, flags=re.I|re.S)
html = re.sub(block_attrs, '', html, flags=re.I)
with open(nojs_path, 'w') as f:
html_path = nojs_path
start_time = time.time()
with open(os.devnull, "w") as fnull:
p = subprocess.Popen([content_shell,
result =
PNG_START = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
PNG_END = b"\x49\x45\x4E\x44\xAE\x42\x60\x82"
start = result.index(PNG_START)
end = result.rindex(PNG_END) + 8
except ValueError:
return False
png_path = os.path.join(output_dir, action, url_parts.hostname + ".png")
with open(png_path, 'wb') as f:
elapsed_time = (time.time() - start_time, url)
return elapsed_time
def RunDrt():
print("Taking screenshots of %d pages..." % len(urls))
start_time = time.time()
results = multiprocessing.Pool().map(RunDrtTask, urls, 1)
max_time, url = max(t for t in results if t)
elapsed_detail = "(slowest: %.2fs on %s)" % (max_time, url)
PrintElapsedTime(time.time() - start_time, elapsed_detail)
def CompareResultsTask(url):
url_parts = urlparse(url)
before_path = os.path.join(output_dir, "before", url_parts.hostname + ".png")
after_path = os.path.join(output_dir, "after", url_parts.hostname + ".png")
diff_path = os.path.join(output_dir, "diff", url_parts.hostname + ".png")
MakeDirsIfNotExist(os.path.join(output_dir, "diff"))
# TODO(johnme): Don't hardcode "real_world_impact".
red_path = ("data:image/gif;base64,R0lGODlhAQABAPAAAP8AAP///yH5BAAAAAAALAAAAA"
before_exists = os.path.exists(before_path)
after_exists = os.path.exists(after_path)
if not before_exists and not after_exists:
# TODO(johnme): Make this more informative.
return (-100, url, red_path)
if before_exists != after_exists:
# TODO(johnme): Make this more informative.
return (200, url, red_path)
# Get percentage difference.
p = subprocess.Popen([image_diff, "--histogram",
before_path, after_path],
output,_ = p.communicate()
if p.returncode == 0:
return (0, url, before_path)
diff_match = re.match(r'histogram diff: (\d+\.\d{2})% (?:passed|failed)\n'
'exact diff: (\d+\.\d{2})% (?:passed|failed)', output)
if not diff_match:
raise Exception("image_diff output format changed")
histogram_diff = float(
exact_diff = float(
combined_diff = max(histogram_diff + exact_diff / 8, 0.001)
# Produce diff PNG.[image_diff, "--diff", before_path, after_path, diff_path])
return (combined_diff, url, diff_path)
def CompareResults():
print("Running image_diff on %d pages..." % len(urls))
start_time = time.time()
results = multiprocessing.Pool().map(CompareResultsTask, urls)
results.sort(key=itemgetter(0), reverse=True)
PrintElapsedTime(time.time() - start_time)
now ="%a %Y-%m-%d %H:%M")
html_start = textwrap.dedent("""\
<!DOCTYPE html>
<title>Real World Impact report %s</title>
var togglingImg = null;
var toggleTimer = null;
var before = true;
function toggle() {
var newFolder = before ? "before" : "after";
togglingImg.src = togglingImg.src.replace(/before|after|diff/, newFolder);
before = !before;
toggleTimer = setTimeout(toggle, 300);
function startToggle(img) {
before = true;
togglingImg = img;
if (!img.origSrc)
img.origSrc = img.src;
function stopToggle(img) {
img.src = img.origSrc;
document.onkeydown = function(e) {
e = e || window.event;
var keyCode = e.keyCode || e.which;
var newFolder;
switch (keyCode) {
case 49: //'1'
newFolder = "before"; break;
case 50: //'2'
newFolder = "after"; break;
case 51: //'3'
newFolder = "diff"; break;
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
imgs[i].src = imgs[i].src.replace(/before|after|diff/, newFolder);
h1 {
font-family: sans;
h2 {
font-family: monospace;
white-space: pre;
.nsfw-spacer {
height: 50vh;
.nsfw-warning {
background: yellow;
border: 10px solid red;
.info {
font-size: 1.2em;
font-style: italic;
body:not(.details-supported) details {
display: none;
if ('open' in document.createElement('details'))
document.body.className = "details-supported";
<!--<div class="nsfw-spacer"></div>-->
<p class="nsfw-warning">Warning: sites below are taken from the Alexa top %d
and may be NSFW.</p>
<!--<div class="nsfw-spacer"></div>-->
<h1>Real World Impact report %s</h1>
<p class="info">Press 1, 2 and 3 to switch between before, after and diff
screenshots respectively; or hover over the images to rapidly alternate
between before and after.</p>
""" % (now, num_sites, now))
html_same_row = """\
<h2>No difference on <a href="%s">%s</a>.</h2>
html_diff_row = """\
<h2>%7.3f%% difference on <a href="%s">%s</a>:</h2>
<img src="%s" width="800" height="600"
onmouseover="startToggle(this)" onmouseout="stopToggle(this)">
html_nsfw_diff_row = """\
<h2>%7.3f%% difference on <a href="%s">%s</a>:</h2>
<summary>This site may be NSFW. Click to expand/collapse.</summary>
<img src="%s" width="800" height="600"
onmouseover="startToggle(this)" onmouseout="stopToggle(this)">
html_end = textwrap.dedent("""\
html_path = os.path.join(output_dir, "diff.html")
with open(html_path, 'w') as f:
for (diff_float, url, diff_path) in results:
diff_path = os.path.relpath(diff_path, output_dir)
if diff_float == 0:
f.write(html_same_row % (url, url))
elif url in nsfw_urls:
f.write(html_nsfw_diff_row % (diff_float, url, url, diff_path))
f.write(html_diff_row % (diff_float, url, url, diff_path))
webbrowser.open_new_tab("file://" + html_path)
def main(argv):
global num_sites, action, allow_js, additional_content_shell_flags
parser = argparse.ArgumentParser(
description="Compare the real world impact of a content shell change.",
Example usage:
1. Build content_shell in out/Release without any changes.
2. Run: %s before [num sites to test (default %d)].
3. Either:
a. Apply your controversial patch and rebuild content_shell.
b. Pass --additional_flags="--enable_your_flag" in step 4.
4. Run: %s after [num sites to test (default %d)].
5. Run: %s compare [num sites to test (default %d)].
This will open the results in your web browser.
""" % (argv[0], num_sites, argv[0], num_sites, argv[0], num_sites)))
parser.add_argument("--allow_js", help="Don't disable Javascript",
help="Additional flags to pass to content shell")
Action to perform.
download - Just download the sites.
before - Run content shell and record 'before' result.
after - Run content shell and record 'after' result.
compare - Compare before and after results.
choices=["download", "before", "after", "compare"])
help="Number of sites (default %s)" % num_sites,
type=int, default=num_sites, nargs='?')
args = parser.parse_args()
action = args.action
if (args.num_sites):
num_sites = args.num_sites
if (args.allow_js):
allow_js = args.allow_js
if (args.additional_flags):
additional_content_shell_flags = args.additional_flags
if not SetupPathsAndOut() or not CheckPrerequisites() or not PickSampleUrls():
return 1
if action == 'compare':
if action != 'download':
return 0
if __name__ == '__main__':