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