fstests: add fio perf results support
[xfstests-dev.git] / src / perf / ResultData.py
1 import sqlite3
2
3 def _dict_factory(cursor, row):
4     d = {}
5     for idx,col in enumerate(cursor.description):
6         d[col[0]] = row[idx]
7     return d
8
9 class ResultData:
10     def __init__(self, filename):
11         self.db = sqlite3.connect(filename)
12         self.db.row_factory = _dict_factory
13
14     def load_last(self, testname, config):
15         d = {}
16         cur = self.db.cursor()
17         cur.execute("SELECT * FROM fio_runs WHERE config = ? AND name = ?ORDER BY time DESC LIMIT 1",
18                     (config,testname))
19         d['global'] = cur.fetchone()
20         if d['global'] is None:
21             return None
22         cur.execute("SELECT * FROM fio_jobs WHERE run_id = ?",
23                     (d['global']['id'],))
24         d['jobs'] = cur.fetchall()
25         return d
26
27     def _insert_obj(self, tablename, obj):
28         keys = obj.keys()
29         values = obj.values()
30         cur = self.db.cursor()
31         cmd = "INSERT INTO {} ({}) VALUES ({}".format(tablename,
32                                                        ",".join(keys),
33                                                        '?,' * len(values))
34         cmd = cmd[:-1] + ')'
35         cur.execute(cmd, tuple(values))
36         self.db.commit()
37         return cur.lastrowid
38
39     def insert_result(self, result):
40         row_id = self._insert_obj('fio_runs', result['global'])
41         for job in result['jobs']:
42             job['run_id'] = row_id
43             self._insert_obj('fio_jobs', job)