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 |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 5 | import os |
| 6 | # from utils import ssize_to_kb |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 7 | |
| 8 | |
| 9 | def connect(url): |
| 10 | return sqlite3.connect(url) |
| 11 | |
| 12 | |
| 13 | create_db_sql_templ = """ |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 14 | # build - param_0 |
| 15 | # lab - param_1 |
| 16 | # operation - param_2 |
| 17 | # sync_type - param_3 (sync, direct, sync+direct, free) |
| 18 | # block_size - param_4 |
| 19 | # concurrence - param_5 |
| 20 | # .... |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 21 | |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 22 | 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] | 23 | |
| 24 | CREATE TABLE param (id integer primary key, name text, type text, descr text); |
| 25 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 26 | CREATE TABLE result (build_id integer, |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 27 | time datetime, |
| 28 | lab_id integer, |
| 29 | params_combination_id integer, |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 30 | bandwith float, |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 31 | meta text); |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 32 | """ |
| 33 | |
| 34 | |
| 35 | PARAM_COUNT = 20 |
| 36 | |
| 37 | |
| 38 | def get_all_tables(conn): |
| 39 | cursor = conn.cursor() |
| 40 | cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 41 | return cursor.fetchall() |
| 42 | |
| 43 | |
| 44 | def drop_database(conn): |
| 45 | cursor = conn.cursor() |
| 46 | cursor.execute("drop table result") |
| 47 | cursor.execute("drop table params_combination") |
| 48 | cursor.execute("drop table build") |
| 49 | cursor.execute("drop table param") |
| 50 | |
| 51 | |
| 52 | def init_database(conn): |
| 53 | cursor = conn.cursor() |
| 54 | |
| 55 | params = ["param_{0} text".format(i) for i in range(PARAM_COUNT)] |
| 56 | create_db_sql = create_db_sql_templ.format(params=",".join(params)) |
| 57 | |
| 58 | for sql in create_db_sql.split(";"): |
| 59 | cursor.execute(sql) |
| 60 | |
| 61 | |
| 62 | def insert_io_params(conn): |
| 63 | sql = """insert into param (name, type) values ('operation', |
| 64 | '{write,randwrite,read,randread}'); |
| 65 | insert into param (name, type) values ('sync', '{a,s}'); |
| 66 | insert into param (name, type) values ('block_size', 'size_kmg'); |
| 67 | """ |
| 68 | |
| 69 | for insert in sql.split(";"): |
| 70 | conn.execute(insert) |
| 71 | |
| 72 | |
| 73 | def insert_build(cursor, build_id, build_type, iso_md5): |
| 74 | cursor.execute("insert into build (build, type, md5) values (?, ?, ?)", |
| 75 | (build_id, build_type, iso_md5)) |
| 76 | return cursor.lastrowid |
| 77 | |
| 78 | |
| 79 | def insert_params(cursor, *param_vals): |
| 80 | param_vals = param_vals + ("",) * (PARAM_COUNT - len(param_vals)) |
| 81 | |
| 82 | params = ",".join(['?'] * PARAM_COUNT) |
| 83 | select_templ = "select id from params_combination where {params_where}" |
| 84 | |
| 85 | params_where = ["param_{0}=?".format(i) for i in range(PARAM_COUNT)] |
| 86 | req = select_templ.format(params_where=" AND ".join(params_where)) |
| 87 | cursor.execute(req, param_vals) |
| 88 | res = cursor.fetchall() |
| 89 | if [] != res: |
| 90 | return res[0][0] |
| 91 | |
| 92 | params = ",".join(['?'] * PARAM_COUNT) |
| 93 | param_insert_templ = "insert into params_combination ({0}) values ({1})" |
| 94 | param_names = ",".join("param_{0}".format(i) for i in range(PARAM_COUNT)) |
| 95 | req = param_insert_templ.format(param_names, params) |
| 96 | cursor.execute(req, param_vals) |
| 97 | return cursor.lastrowid |
| 98 | |
| 99 | |
| 100 | def insert_results(cursor, build_id, params_id, bw, dev): |
| 101 | req = "insert into result values (?, ?, ?, ?)" |
| 102 | cursor.execute(req, (build_id, params_id, bw, dev)) |
| 103 | |
| 104 | |
| 105 | @contextlib.contextmanager |
| 106 | def transaction(conn): |
| 107 | try: |
| 108 | cursor = conn.cursor() |
| 109 | yield cursor |
| 110 | except: |
| 111 | conn.rollback() |
| 112 | raise |
| 113 | else: |
| 114 | conn.commit() |
| 115 | |
| 116 | |
| 117 | def json_to_db(json_data, conn): |
| 118 | data = json.loads(json_data) |
| 119 | with transaction(conn) as cursor: |
| 120 | for build_data in data: |
| 121 | build_id = insert_build(cursor, |
| 122 | build_data.pop("build_id"), |
| 123 | build_data.pop("type"), |
| 124 | build_data.pop("iso_md5")) |
| 125 | |
| 126 | for params, (bw, dev) in build_data.items(): |
| 127 | param_id = insert_params(cursor, *params.split(" ")) |
| 128 | insert_results(cursor, build_id, param_id, bw, dev) |
| 129 | |
| 130 | |
| 131 | def to_db(): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 132 | conn = sqlite3.connect(os.path.abspath("test.db")) |
| 133 | # json_data = open(sys.argv[2]).read() |
| 134 | json_data = '[{\ |
| 135 | "randwrite a 256k": [16885, 1869],\ |
| 136 | "randwrite s 4k": [79, 2],\ |
| 137 | "read a 64k": [74398, 11618],\ |
| 138 | "write s 1024k": [7490, 193],\ |
| 139 | "randwrite a 64k": [14167, 4665],\ |
| 140 | "build_id": "1",\ |
| 141 | "randread a 1024k": [68683, 8604],\ |
| 142 | "randwrite s 256k": [3277, 146],\ |
| 143 | "write a 1024k": [24069, 660],\ |
| 144 | "type": "GA",\ |
| 145 | "write a 64k": [24555, 1006],\ |
| 146 | "write s 64k": [1285, 57],\ |
| 147 | "write a 256k": [24928, 503],\ |
| 148 | "write s 256k": [4029, 192],\ |
| 149 | "randwrite a 1024k": [23980, 1897],\ |
| 150 | "randread a 64k": [27257, 17268],\ |
| 151 | "randwrite s 1024k": [8504, 238],\ |
| 152 | "randread a 256k": [60868, 2637],\ |
| 153 | "randread a 4k": [3612, 1355],\ |
| 154 | "read a 1024k": [71122, 9217],\ |
| 155 | "date": "Thu Feb 12 19:11:56 2015",\ |
| 156 | "write s 4k": [87, 3],\ |
| 157 | "read a 4k": [88367, 6471],\ |
| 158 | "read a 256k": [80904, 8930],\ |
| 159 | "name": "GA - 6.0 GA",\ |
| 160 | "randwrite s 1k": [20, 0],\ |
| 161 | "randwrite s 64k": [1029, 34],\ |
| 162 | "write s 1k": [21, 0],\ |
| 163 | "iso_md5": "bla bla"\ |
| 164 | }]' |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 165 | |
| 166 | if len(get_all_tables(conn)) == 0: |
| 167 | init_database(conn) |
| 168 | |
| 169 | json_to_db(json_data, conn) |
| 170 | |
| 171 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 172 | def load_slice(cursor, build_id, y_param, **params): |
| 173 | params_id = {} |
| 174 | for param in list(params) + [y_param]: |
| 175 | cursor.execute("select id from param where name=?", (param,)) |
| 176 | params_id[param] = cursor.fetchone() |
| 177 | |
| 178 | sql = """select params_combination.param_{0}, result.bandwith |
| 179 | from params_combination, result |
| 180 | where result.build_id=?""".format(params_id[y_param]) |
| 181 | |
| 182 | for param, val in params.items(): |
| 183 | pid = params_id[param] |
| 184 | sql += " and params_combination.param_{0}='{1}'".format(pid, val) |
| 185 | |
| 186 | cursor.execute(sql) |
| 187 | |
| 188 | |
| 189 | def from_db(): |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 190 | conn = sqlite3.connect(os.path.abspath("test.db")) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 191 | # sql = sys.argv[2] |
| 192 | cursor = conn.cursor() |
| 193 | |
| 194 | sql = """select params_combination.param_2, result.bandwith |
| 195 | from params_combination, result |
| 196 | where params_combination.param_0="write" |
| 197 | and params_combination.param_1="s" |
| 198 | and params_combination.id=result.params_combination |
| 199 | and result.build_id=60""" |
| 200 | |
| 201 | cursor.execute(sql) |
| 202 | data = [] |
| 203 | |
| 204 | for (sz, bw) in cursor.fetchall(): |
| 205 | data.append((ssize_to_kb(sz), sz, bw)) |
| 206 | |
| 207 | data.sort() |
| 208 | |
| 209 | import matplotlib.pyplot as plt |
| 210 | xvals = range(len(data)) |
| 211 | plt.plot(xvals, [dt[2] for dt in data]) |
| 212 | plt.ylabel('bandwith') |
| 213 | plt.xlabel('block size') |
| 214 | plt.xticks(xvals, [dt[1] for dt in data]) |
| 215 | plt.show() |
stgleb | 75c7041 | 2015-02-17 02:52:00 +0200 | [diff] [blame] | 216 | # |
| 217 | init_database() |
| 218 | # from_db() |