koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame^] | 1 | import sys |
| 2 | import json |
| 3 | import sqlite3 |
| 4 | import contextlib |
| 5 | |
| 6 | |
| 7 | def connect(url): |
| 8 | return sqlite3.connect(url) |
| 9 | |
| 10 | |
| 11 | create_db_sql_templ = """ |
| 12 | CREATE TABLE build (id integer primary key, |
| 13 | build text, |
| 14 | type text, |
| 15 | md5 text); |
| 16 | |
| 17 | CREATE TABLE params_combination (id integer primary key, {params}); |
| 18 | CREATE TABLE param (id integer primary key, name text, type text); |
| 19 | |
| 20 | CREATE TABLE result (build_id integer, |
| 21 | params_combination integer, |
| 22 | bandwith float, |
| 23 | deviation float); |
| 24 | """ |
| 25 | |
| 26 | |
| 27 | PARAM_COUNT = 20 |
| 28 | |
| 29 | |
| 30 | def get_all_tables(conn): |
| 31 | cursor = conn.cursor() |
| 32 | cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 33 | return cursor.fetchall() |
| 34 | |
| 35 | |
| 36 | def drop_database(conn): |
| 37 | cursor = conn.cursor() |
| 38 | cursor.execute("drop table result") |
| 39 | cursor.execute("drop table params_combination") |
| 40 | cursor.execute("drop table build") |
| 41 | cursor.execute("drop table param") |
| 42 | |
| 43 | |
| 44 | def init_database(conn): |
| 45 | cursor = conn.cursor() |
| 46 | |
| 47 | params = ["param_{0} text".format(i) for i in range(PARAM_COUNT)] |
| 48 | create_db_sql = create_db_sql_templ.format(params=",".join(params)) |
| 49 | |
| 50 | for sql in create_db_sql.split(";"): |
| 51 | cursor.execute(sql) |
| 52 | |
| 53 | |
| 54 | def insert_io_params(conn): |
| 55 | sql = """insert into param (name, type) values ('operation', |
| 56 | '{write,randwrite,read,randread}'); |
| 57 | insert into param (name, type) values ('sync', '{a,s}'); |
| 58 | insert into param (name, type) values ('block_size', 'size_kmg'); |
| 59 | """ |
| 60 | |
| 61 | for insert in sql.split(";"): |
| 62 | conn.execute(insert) |
| 63 | |
| 64 | |
| 65 | def insert_build(cursor, build_id, build_type, iso_md5): |
| 66 | cursor.execute("insert into build (build, type, md5) values (?, ?, ?)", |
| 67 | (build_id, build_type, iso_md5)) |
| 68 | return cursor.lastrowid |
| 69 | |
| 70 | |
| 71 | def insert_params(cursor, *param_vals): |
| 72 | param_vals = param_vals + ("",) * (PARAM_COUNT - len(param_vals)) |
| 73 | |
| 74 | params = ",".join(['?'] * PARAM_COUNT) |
| 75 | select_templ = "select id from params_combination where {params_where}" |
| 76 | |
| 77 | params_where = ["param_{0}=?".format(i) for i in range(PARAM_COUNT)] |
| 78 | req = select_templ.format(params_where=" AND ".join(params_where)) |
| 79 | cursor.execute(req, param_vals) |
| 80 | res = cursor.fetchall() |
| 81 | if [] != res: |
| 82 | return res[0][0] |
| 83 | |
| 84 | params = ",".join(['?'] * PARAM_COUNT) |
| 85 | param_insert_templ = "insert into params_combination ({0}) values ({1})" |
| 86 | param_names = ",".join("param_{0}".format(i) for i in range(PARAM_COUNT)) |
| 87 | req = param_insert_templ.format(param_names, params) |
| 88 | cursor.execute(req, param_vals) |
| 89 | return cursor.lastrowid |
| 90 | |
| 91 | |
| 92 | def insert_results(cursor, build_id, params_id, bw, dev): |
| 93 | req = "insert into result values (?, ?, ?, ?)" |
| 94 | cursor.execute(req, (build_id, params_id, bw, dev)) |
| 95 | |
| 96 | |
| 97 | @contextlib.contextmanager |
| 98 | def transaction(conn): |
| 99 | try: |
| 100 | cursor = conn.cursor() |
| 101 | yield cursor |
| 102 | except: |
| 103 | conn.rollback() |
| 104 | raise |
| 105 | else: |
| 106 | conn.commit() |
| 107 | |
| 108 | |
| 109 | def json_to_db(json_data, conn): |
| 110 | data = json.loads(json_data) |
| 111 | with transaction(conn) as cursor: |
| 112 | for build_data in data: |
| 113 | build_id = insert_build(cursor, |
| 114 | build_data.pop("build_id"), |
| 115 | build_data.pop("type"), |
| 116 | build_data.pop("iso_md5")) |
| 117 | |
| 118 | for params, (bw, dev) in build_data.items(): |
| 119 | param_id = insert_params(cursor, *params.split(" ")) |
| 120 | insert_results(cursor, build_id, param_id, bw, dev) |
| 121 | |
| 122 | |
| 123 | def to_db(): |
| 124 | conn = sqlite3.connect(sys.argv[1]) |
| 125 | json_data = open(sys.argv[2]).read() |
| 126 | |
| 127 | if len(get_all_tables(conn)) == 0: |
| 128 | init_database(conn) |
| 129 | |
| 130 | json_to_db(json_data, conn) |
| 131 | |
| 132 | |
| 133 | def ssize_to_kb(ssize): |
| 134 | try: |
| 135 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 136 | for ext, coef in smap.items(): |
| 137 | if ssize.endswith(ext): |
| 138 | return int(ssize[:-1]) * coef |
| 139 | |
| 140 | if int(ssize) % 1024 != 0: |
| 141 | raise ValueError() |
| 142 | |
| 143 | return int(ssize) / 1024 |
| 144 | |
| 145 | except (ValueError, TypeError, AttributeError): |
| 146 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 147 | raise ValueError(tmpl.format(ssize)) |
| 148 | |
| 149 | |
| 150 | def load_slice(cursor, build_id, y_param, **params): |
| 151 | params_id = {} |
| 152 | for param in list(params) + [y_param]: |
| 153 | cursor.execute("select id from param where name=?", (param,)) |
| 154 | params_id[param] = cursor.fetchone() |
| 155 | |
| 156 | sql = """select params_combination.param_{0}, result.bandwith |
| 157 | from params_combination, result |
| 158 | where result.build_id=?""".format(params_id[y_param]) |
| 159 | |
| 160 | for param, val in params.items(): |
| 161 | pid = params_id[param] |
| 162 | sql += " and params_combination.param_{0}='{1}'".format(pid, val) |
| 163 | |
| 164 | cursor.execute(sql) |
| 165 | |
| 166 | |
| 167 | def from_db(): |
| 168 | conn = sqlite3.connect(sys.argv[1]) |
| 169 | # sql = sys.argv[2] |
| 170 | cursor = conn.cursor() |
| 171 | |
| 172 | sql = """select params_combination.param_2, result.bandwith |
| 173 | from params_combination, result |
| 174 | where params_combination.param_0="write" |
| 175 | and params_combination.param_1="s" |
| 176 | and params_combination.id=result.params_combination |
| 177 | and result.build_id=60""" |
| 178 | |
| 179 | cursor.execute(sql) |
| 180 | data = [] |
| 181 | |
| 182 | for (sz, bw) in cursor.fetchall(): |
| 183 | data.append((ssize_to_kb(sz), sz, bw)) |
| 184 | |
| 185 | data.sort() |
| 186 | |
| 187 | import matplotlib.pyplot as plt |
| 188 | xvals = range(len(data)) |
| 189 | plt.plot(xvals, [dt[2] for dt in data]) |
| 190 | plt.ylabel('bandwith') |
| 191 | plt.xlabel('block size') |
| 192 | plt.xticks(xvals, [dt[1] for dt in data]) |
| 193 | plt.show() |
| 194 | |
| 195 | |
| 196 | from_db() |