Skip to content

Commit

Permalink
Design and add testing for the new API version
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr committed Feb 18, 2024
1 parent d964faf commit 89a6b91
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
44 changes: 44 additions & 0 deletions rebench/model/data_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,49 @@ def measurements_as_dict(self, criteria):
'm': data
}

def add_measurements_api_v20(self, criteria, data):
num_measurements = 0
iteration = -1
invocation = -1

# data contains a list of hashes
# with {in: n, m: [[], [], ...]}
# where m is a list of values for each criterion

ms = None

for m in self._measurements:
if iteration == -1:
iteration = m.iteration
invocation = m.invocation
for d in data:
if d['in'] == invocation:
ms = d['m']
break
if ms is None:
ms = []
data.append({'in': invocation, 'm': ms})
for _ in criteria:
ms.append([])
else:
assert iteration == m.iteration
assert invocation == m.invocation
assert ms is not None
criterion = (m.criterion, m.unit)
if criterion not in criteria:
criteria[criterion] = len(criteria)

c_idx = criteria[criterion]
if len(ms) <= c_idx:
ms.append([])

while len(ms[c_idx]) + 1 < iteration:
ms[c_idx].append(None)
ms[c_idx].append(m.value)
num_measurements += 1

assert self.invocation == invocation
return num_measurements

def __repr__(self):
return "DataPoint(" + str(self.run_id) + ", " + str(self._measurements) + ")"
19 changes: 19 additions & 0 deletions rebench/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,25 @@ def convert_data_to_api_format(self, data):

return all_data, criteria_index, num_measurements

def convert_data_to_api_20_format(self, data):
num_measurements = 0
all_data = []
criteria = {}
for run_id, data_points in data.items():
dp_data = []
for dp in data_points:
num_measurements += dp.add_measurements_api_v20(criteria, dp_data)
all_data.append({
'runId': run_id.as_dict(),
'd': dp_data
})

criteria_index = []
for c, idx in criteria.items():
criteria_index.append({'c': c[0], 'u': c[1], 'i': idx})

return all_data, criteria_index, num_measurements

def _send_data(self, cache):
self.ui.debug_output_info("ReBenchDB: Prepare data for sending\n")
all_data, criteria_index, num_measurements = self.convert_data_to_api_format(cache)
Expand Down
47 changes: 47 additions & 0 deletions rebench/tests/persistency_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,50 @@ def test_data_conversion_to_rebench_db_api(self):
'{"in":1,"it":9,"m":[{"v":8.8,"c":0},{"v":8.8,"c":2}]},' +
'{"in":1,"it":10,"m":[{"v":9.9,"c":1},{"v":9.9,"c":3},{"v":9.9,"c":2}]}]',
rdb.convert_data_to_json(data))

def _assert_data_point_structure_v20(self, data):
self.assertEqual(1, len(data))
in1 = data[0]
self.assertEqual(1, in1['in'])
self.assertEqual(4, len(in1['m'])) # 4 criteria

ms = in1['m']
for i in range(0, 10):
if i % 2 == 0:
self.assertEqual(i, int(ms[0][i]))
elif len(ms[0]) > i:
self.assertIsNone(ms[0][i])

if i % 3 == 0:
self.assertEqual(i, int(ms[1][i]))
elif len(ms[1]) > i:
self.assertIsNone(ms[1][i])

self.assertEqual(i, int(ms[2][i]))

if i % 2 == 1:
self.assertEqual(i, int(ms[3][i]))
elif len(ms[3]) > i:
self.assertIsNone(ms[3][i])

def test_data_conversion_to_rebench_db_api_v20(self):
cache, run_id_obj = self._run_exp_to_get_data_points_with_inconsistent_set_of_criteria()
rebench_db = self._create_dummy_rebench_db_persistence()
all_data, criteria_index, num_measurements = rebench_db.convert_data_to_api_20_format(cache)

self.assertEqual(24, num_measurements)

run_id = all_data[0]['runId']
data = all_data[0]['d']

self._assert_criteria_index_structure(criteria_index)
self._assert_run_id_structure(run_id, run_id_obj)
self._assert_benchmark_structure(run_id, run_id_obj)
self._assert_data_point_structure_v20(data)

rdb = self._create_dummy_rebench_db_adapter()
self.assertEqual('[{"in":1,"m":[[0.0,null,2.2,null,4.4,null,6.6,null,8.8],' +
'[0.0,null,null,3.3,null,null,6.6,null,null,9.9],' +
'[0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9],' +
'[null,1.1,null,3.3,null,5.5,null,7.7,null,9.9]]}]',
rdb.convert_data_to_json(data))

0 comments on commit 89a6b91

Please sign in to comment.