Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 1 | import argparse |
| 2 | from collections import OrderedDict |
| 3 | import sys |
| 4 | |
| 5 | import charts |
| 6 | import storage_api |
| 7 | |
| 8 | |
Yulia Portnova | 042344d | 2015-02-10 17:38:33 +0200 | [diff] [blame] | 9 | OPERATIONS = ('sync', ('randwrite a', 'randread a', 'write a', 'read a'), |
| 10 | 'async', ('randwrite s', 'randread s', 'write s', 'read s')) |
| 11 | |
| 12 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 13 | def ssize_to_kb(ssize): |
| 14 | try: |
| 15 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 16 | for ext, coef in smap.items(): |
| 17 | if ssize.endswith(ext): |
| 18 | return int(ssize[:-1]) * coef |
| 19 | |
| 20 | if int(ssize) % 1024 != 0: |
| 21 | raise ValueError() |
| 22 | |
| 23 | return int(ssize) / 1024 |
| 24 | |
| 25 | except (ValueError, TypeError, AttributeError): |
| 26 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 27 | raise ValueError(tmpl.format(ssize)) |
| 28 | |
| 29 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 30 | def parse_args(argv): |
| 31 | parser = argparse.ArgumentParser() |
| 32 | parser.add_argument('-s', '--storage', help='storage location', dest="url") |
| 33 | parser.add_argument('-e', '--email', help='user email', |
| 34 | default="aaa@gmail.com") |
| 35 | parser.add_argument('-p', '--password', help='user password', |
| 36 | default="1234") |
| 37 | return parser.parse_args(argv) |
| 38 | |
| 39 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 40 | def build_vertical_bar(results): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 41 | data = {} |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 42 | charts_url = [] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 43 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 44 | for build, results in results.items(): |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 45 | for key, value in results.results.items(): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 46 | keys = key.split(' ') |
| 47 | if not data.get(keys[2]): |
| 48 | data[keys[2]] = {} |
| 49 | if not data[keys[2]].get(build): |
| 50 | data[keys[2]][build] = {} |
| 51 | data[keys[2]][build][' '.join([keys[0], keys[1]])] = value |
| 52 | |
| 53 | for name, value in data.items(): |
Yulia Portnova | 042344d | 2015-02-10 17:38:33 +0200 | [diff] [blame] | 54 | for op_type, operations in OPERATIONS: |
| 55 | title = name |
| 56 | legend = [] |
| 57 | dataset = [] |
Yulia Portnova | e440802 | 2015-02-10 15:39:25 +0200 | [diff] [blame] | 58 | |
Yulia Portnova | 042344d | 2015-02-10 17:38:33 +0200 | [diff] [blame] | 59 | scale_x = [] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 60 | |
Yulia Portnova | 042344d | 2015-02-10 17:38:33 +0200 | [diff] [blame] | 61 | for build_id, build_results in value.items(): |
| 62 | vals = [] |
Yulia Portnova | e440802 | 2015-02-10 15:39:25 +0200 | [diff] [blame] | 63 | |
Yulia Portnova | 042344d | 2015-02-10 17:38:33 +0200 | [diff] [blame] | 64 | for key in operations: |
| 65 | res = build_results.get(key) |
| 66 | if res: |
| 67 | vals.append(res) |
| 68 | scale_x.append(key) |
| 69 | if vals: |
| 70 | dataset.append(vals) |
| 71 | legend.append(build_id) |
| 72 | |
| 73 | if dataset: |
| 74 | charts_url.append(str(charts.render_vertical_bar |
| 75 | (title, legend, dataset, scale_x=scale_x))) |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 76 | return charts_url |
| 77 | |
| 78 | |
| 79 | def build_lines_chart(results): |
| 80 | data = {} |
| 81 | charts_url = [] |
| 82 | |
| 83 | for build, results in results.items(): |
| 84 | for key, value in results.results.items(): |
| 85 | keys = key.split(' ') |
| 86 | if not data.get(' '.join([keys[0], keys[1]])): |
| 87 | data[' '.join([keys[0], keys[1]])] = {} |
| 88 | if not data[' '.join([keys[0], keys[1]])].get(build): |
| 89 | data[' '.join([keys[0], keys[1]])][build] = {} |
| 90 | data[' '.join([keys[0], keys[1]])][build][keys[2]] = value |
| 91 | |
| 92 | for name, value in data.items(): |
| 93 | title = name |
| 94 | legend = [] |
| 95 | dataset = [] |
| 96 | scale_x = [] |
| 97 | for build_id, build_results in value.items(): |
| 98 | legend.append(build_id) |
| 99 | ordered_build_results = OrderedDict(sorted(build_results.items(), |
| 100 | key=lambda t: ssize_to_kb(t[0]))) |
| 101 | if not scale_x: |
| 102 | scale_x = ordered_build_results.keys() |
| 103 | dataset.append(zip(*ordered_build_results.values())[0]) |
| 104 | |
| 105 | chart = charts.render_lines(title, legend, dataset, scale_x) |
| 106 | charts_url.append(str(chart)) |
| 107 | |
| 108 | return charts_url |
| 109 | |
| 110 | |
| 111 | def render_html(charts_urls): |
| 112 | templ = open("report.html", 'r').read() |
| 113 | body = "<div><ol>%s</ol></div>" |
| 114 | li = "<li><img src='%s'></li>" |
| 115 | ol = [] |
| 116 | for chart in charts_urls: |
| 117 | ol.append(li % chart) |
| 118 | html = templ % {'body': body % '\n'.join(ol)} |
| 119 | open('results.html', 'w').write(html) |
| 120 | |
| 121 | |
| 122 | def report(url, email=None, password=None): |
| 123 | storage = storage_api.create_storage(url, email, password) |
| 124 | results = storage.recent_builds() |
| 125 | bars = build_vertical_bar(results) |
| 126 | lines = build_lines_chart(results) |
| 127 | |
| 128 | render_html(bars + lines) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def main(argv): |
| 132 | opts = parse_args(argv) |
| 133 | report(opts.url) |
| 134 | return 0 |
| 135 | |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | exit(main(sys.argv[1:])) |