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