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