aboutsummaryrefslogtreecommitdiff
path: root/libc/benchmarks/render.py3
blob: e790d18f84e7f7379a9929a98ccc0174ef52ccbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Reads JSON files produced by the benchmarking framework and renders them.

Installation:
> apt-get install python3-pip
> pip3 install matplotlib scipy numpy

Run:
> python3 render.py3 <files>

Rendering can occur on disk by specifying the --output option or on screen if
the --headless flag is not set.
"""

import argparse
import collections
import json
import math
import pprint
import sys
import matplotlib.pyplot as plt
from matplotlib.ticker import EngFormatter
import numpy as np
import scipy.stats


def format_freq(number):
    """Returns a human readable frequency."""
    magnitude = 0
    while math.fabs(number) >= 1000:
        number /= 1000.0
        magnitude += 1
    return "%g%sHz" % (number, ["", "k", "M", "G"][magnitude])


def format_size(number):
    """Returns number in human readable form."""
    magnitude = 0
    while number >= 1000 and number % 1000 == 0:
        number /= 1000
        magnitude += 1
    return "%g%s" % (number, ["", "K", "M", "G"][magnitude])


def mean_confidence_interval(dataset, confidence=0.95):
    """Returns the mean and half confidence interval for the dataset."""
    a = 1.0 * np.array(dataset)
    n = len(a)
    m, se = np.mean(a), scipy.stats.sem(a)
    h = se * scipy.stats.t.ppf((1 + confidence) / 2., n - 1)
    return m, h


def add_plot(function_name, points):
    """Plots measurements for a function."""
    n = len(points.keys())
    x = np.zeros(n)
    y = np.zeros(n)
    yerr = np.zeros(n)

    for i, key in enumerate(sorted(points.keys())):
        values = points[key]
        m, e = mean_confidence_interval(values)
        x[i] = key
        y[i] = m
        yerr[i] = e

    plt.plot(x, y, linewidth=1, label=function_name)
    plt.fill_between(x, y - yerr, y + yerr, alpha=0.5)


def get_title(host):
    """Formats the Host object into a title for the plot."""
    cpu_name = host["CpuName"]
    cpu_freq = format_freq(host["CpuFrequency"])
    cache_strings = []
    for cache in host["Caches"]:
        prefix = {
            "Instruction": "i",
            "Data": "d",
            "Unified": "u",
        }.get(cache["Type"])
        cache_strings.append(r"%sL_%d %s_{/%d}" %
                             (prefix, cache["Level"], format_size(
                                 cache["Size"]), cache["NumSharing"]))
    title = "%s (%s)" % (cpu_name, cpu_freq)
    subtitle = r"$" + ", ".join(sorted(cache_strings)) + r"$"
    return title + "\n" + subtitle


def get_host(jsons):
    """Returns the host of the different json objects iff they are all the same.
    """
    host = None
    for root in jsons:
        if host and host != root["Host"]:
            sys.exit("The datasets are not coming from the same Host")
        if not host:
            host = root["Host"]
    return host


def get_configuration(jsons):
    """Returns the configuration of the different json objects iff they are all
    the same.
    """
    config = None
    for root in jsons:
        if config and config != root["Configuration"]:
            return None
        if not config:
            config = root["Configuration"]
    return config


def setup_graphs(files):
    """Setups the graphs to render from the json files."""
    jsons = []
    for file in files:
        with open(file) as json_file:
            jsons.append(json.load(json_file))
    if not jsons:
        sys.exit("Nothing to process")

    for root in jsons:
        for function in root["Functions"]:
            function_name = function["Name"]
            sizes = function["Sizes"]
            runtimes = function["Runtimes"]
            assert len(sizes) == len(runtimes)
            values = collections.defaultdict(lambda: [])
            for i in range(len(sizes)):
              values[sizes[i]].append(runtimes[i])
            add_plot(function_name, values)

    config = get_configuration(jsons)
    if config:
        plt.figtext(
            0.95,
            0.15,
            pprint.pformat(config),
            verticalalignment="bottom",
            horizontalalignment="right",
            multialignment="left",
            fontsize="small",
            bbox=dict(boxstyle="round", facecolor="wheat"))

    axes = plt.gca()
    axes.set_title(get_title(get_host(jsons)))
    axes.set_ylim(bottom=0)
    axes.set_xlabel("Size")
    axes.set_ylabel("Time")
    axes.xaxis.set_major_formatter(EngFormatter(unit="B"))
    axes.yaxis.set_major_formatter(EngFormatter(unit="s"))
    plt.legend()
    plt.grid()


def main():
    parser = argparse.ArgumentParser(
        description="Process benchmark json files.")
    parser.add_argument("files", nargs="+", help="The json files to read from.")
    parser.add_argument("--output", help="The output file to write the graph.")
    parser.add_argument(
        "--headless",
        help="If set do not display the graph.",
        action="store_true")
    args = parser.parse_args()
    setup_graphs(args.files)
    if args.output:
        plt.savefig(args.output)
    if not args.headless:
        plt.show()

if __name__ == "__main__":
    main()