| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import json |
| 12 |
import math |
| 13 |
import statistics |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
import click |
| 17 |
|
| 18 |
|
| 19 |
def plot_vega(vega_spec, data, *, to, parameters=None): |
| 20 |
import vl_convert |
| 21 |
|
| 22 |
parameters = parameters or {} |
| 23 |
|
| 24 |
spec = json.loads(vega_spec.read_text()) |
| 25 |
spec["data"].insert(0, {"name": "source", "values": data}) |
| 26 |
if "signals" not in spec: |
| 27 |
spec["signals"] = [] |
| 28 |
|
| 29 |
for key, value in parameters.items(): |
| 30 |
spec["signals"].append({"name": key, "value": value}) |
| 31 |
|
| 32 |
with open(to, "wb") as f: |
| 33 |
# default ppi is 72, which is somewhat blurry. |
| 34 |
f.write(vl_convert.vega_to_png(spec, ppi=200)) |
| 35 |
|
| 36 |
|
| 37 |
def _mean_difference_ci(n1, n2, *, confidence): |
| 38 |
from scipy import stats |
| 39 |
|
| 40 |
var1 = statistics.variance(n1) |
| 41 |
var2 = statistics.variance(n2) |
| 42 |
df = len(n1) + len(n2) - 2 |
| 43 |
# this assumes equal variances between the populations of n1 and n2. This |
| 44 |
# is not necessarily true (new might be more consistent than old), but it's |
| 45 |
# good enough. |
| 46 |
pooled_std = math.sqrt(((len(n1) - 1) * var1 + (len(n2) - 1) * var2) / df) |
| 47 |
se = pooled_std * math.sqrt(1 / len(n1) + 1 / len(n2)) |
| 48 |
t_crit = stats.t.ppf((1 + confidence) / 2, df) |
| 49 |
return t_crit * se |
| 50 |
|
| 51 |
|
| 52 |
def _process_benchmark_data(data): |
| 53 |
assert set(data) == {"old", "new"} |
| 54 |
old_calls = data["old"]["calls"] |
| 55 |
new_calls = data["new"]["calls"] |
| 56 |
assert set(old_calls) == set(new_calls), set(old_calls).symmetric_difference( |
| 57 |
set(new_calls) |
| 58 |
) |
| 59 |
|
| 60 |
graph_data = [] |
| 61 |
|
| 62 |
def _diff_times(old, new): |
| 63 |
if old == 0 and new == 0: |
| 64 |
return 0 |
| 65 |
if old == 0: |
| 66 |
# there aren't any great options here, but 0 is more reasonable than inf. |
| 67 |
return 0 |
| 68 |
v = (old - new) / old |
| 69 |
if 0 < v < 1: |
| 70 |
v = (1 / (1 - v)) - 1 |
| 71 |
return v |
| 72 |
|
| 73 |
sums = {"old": 0, "new": 0} |
| 74 |
for node_id in old_calls: |
| 75 |
old = old_calls[node_id] |
| 76 |
new = new_calls[node_id] |
| 77 |
if ( |
| 78 |
set(old) | set(new) == {0} |
| 79 |
or len(old) != len(new) |
| 80 |
or len(old) == len(new) == 0 |
| 81 |
): |
| 82 |
print(f"skipping {node_id}") |
| 83 |
continue |
| 84 |
|
| 85 |
sums["old"] += statistics.mean(old) |
| 86 |
sums["new"] += statistics.mean(new) |
| 87 |
diffs = [n_old - n_new for n_old, n_new in zip(old, new, strict=True)] |
| 88 |
diffs_times = [ |
| 89 |
_diff_times(n_old, n_new) for n_old, n_new in zip(old, new, strict=True) |
| 90 |
] |
| 91 |
ci_shrink = ( |
| 92 |
_mean_difference_ci(old, new, confidence=0.95) if len(old) > 1 else 0 |
| 93 |
) |
| 94 |
|
| 95 |
graph_data.append( |
| 96 |
{ |
| 97 |
"node_id": node_id, |
| 98 |
"absolute": statistics.mean(diffs), |
| 99 |
"absolute_ci_lower": ci_shrink, |
| 100 |
"absolute_ci_upper": ci_shrink, |
| 101 |
"nx": statistics.mean(diffs_times), |
| 102 |
"nx_ci_lower": 0, |
| 103 |
"nx_ci_upper": 0, |
| 104 |
} |
| 105 |
) |
| 106 |
|
| 107 |
graph_data = sorted(graph_data, key=lambda d: d["absolute"]) |
| 108 |
return graph_data, sums |
| 109 |
|
| 110 |
|
| 111 |
@click.command() |
| 112 |
@click.argument("data", type=click.Path(exists=True, path_type=Path)) |
| 113 |
@click.argument("out", type=click.Path(path_type=Path)) |
| 114 |
def plot(data, out): |
| 115 |
data = json.loads(data.read_text()) |
| 116 |
data, sums = _process_benchmark_data(data) |
| 117 |
plot_vega( |
| 118 |
Path(__file__).parent / "spec.json", |
| 119 |
data=data, |
| 120 |
to=out, |
| 121 |
parameters={ |
| 122 |
"title": "Shrinking benchmark (calls)", |
| 123 |
"sum_old": sums["old"], |
| 124 |
"sum_new": sums["new"], |
| 125 |
"absolute_axis_title": ("shrink call change (old - new, larger is good)"), |
| 126 |
}, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == "__main__": |
| 131 |
plot() |