Commit 2c34b5e5 authored by Joanne Hugé's avatar Joanne Hugé

Start script to import measures and generate graphs and markdown tables

parent c09e2e8d
#!/usr/bin/env python3
import statistics
import json
import markdown_table
import argparse
import os
class MeasureSetHandler:
measures_dir = "measures"
measure_sets_file_name = "measure_sets.json"
measure_sets_path = measures_dir + "/" + measure_sets_file_name
def __init__(self):
with open(MeasureSetHandler.measure_sets_path) as measure_sets_file:
self.measure_sets = json.load(measure_sets_file)
def __str__(self):
for measure_set in self.measure_sets:
print("List of measures:\n\n")
print(" " + measure_set)
def save(self):
with open(MeasureSetHandler.measure_sets_path, 'w') as measure_sets_file:
json.dump(self.measure_sets, measure_sets_file)
def add_measure_set(self, measure_set):
mtype = measure_set.measure_type
if mtype not in self.measure_sets:
self.measure_sets[mtype] = {'ids': [], 'next_id': 0}
next_id = self.measure_sets[mtype]['next_id']
measure_file_name = MeasureSetHandler.measures_dir + "/" + mtype + str(next_id)
measure_set.export_to_json(measure_file_name)
self.measure_sets[mtype]['ids'].append(next_id)
self.measure_sets[mtype]['next_id'] += 1
class Measures:
self.save()
def __init__(self, board, linux_version, boot_params):
def remove_measure_set(self, mtype, mid):
if mtype in self.measure_sets and len(self.measure_sets[mtype]['ids']) > 0:
self.measure_sets[mtype]['ids'].remove(mid)
measure_file_name = MeasureSetHandler.measures_dir + "/" + mtype + str(mid)
os.remove(measure_file_name)
self.save()
def remove_all(self):
for mtype in self.measure_sets:
for mid in self.measure_sets[mtype]['ids']:
self.remove_measure_set(mtype, mid)
class MeasureSet:
def __init__(self):
self.board = "Emerald"
self.linux_version = "4.19"
self.boot_params = "isolcpus"
self.cols = {}
self.interval = 0
self.units = []
def __str__(self):
return "Cols: " + str(self.cols) + "\nInterval: " + str(self.interval)
def set_meta_data(self, board, linux_version, boot_params):
self.board = board
self.linux_version = linux_version
self.boot_params = boot_params
def add_results(self, cols, interval):
def add_results(self, measure_type, interval, col_names, units, cols):
self.measure_type = measure_type
self.cols = cols
self.col_names = col_names
self.interval = interval
self.units = units
self.max = [max(col) for col in cols]
self.min = [min(col) for col in cols]
self.avg = [statistics.mean(col) for col in cols]
self.var = [statistics.variance(col) for col in cols]
def export_to_json(self, path):
with open(path, 'w') as outfile:
json.dump({'measure_type': self.measure_type, 'interval': self.interval, 'col_names': self.col_names, 'units': self.units, 'cols': self.cols}, outfile)
def import_from_json(self, path, flat):
with open(path) as infile:
data = json.load(infile)
measure_type = data['measure_type']
interval = data['interval']
col_names = data['col_names']
values = data['values']
units = data['units']
if flat:
nb_cols = len(col_names)
cols = [[] for c in range(nb_cols)]
for i,value in enumerate(values):
cols[i % nb_cols].append(value)
else:
cols = data['cols']
self.add_results(measure_type, interval, col_names, units, cols)
def generate_graph(self, path):
pass
def generate_table(self):
m = [self.min,
self.max,
self.avg,
self.var]
headers = ["Name", "Min", "Max", "Avg", "Var"]
values = [[ (format(m[j][i], '.4f') + self.units[i]) for j in range(len(m))] for i in range(len(m[0]))]
table = [headers]
table += [([("*" + self.col_names[i] + "*")] + values[i]) for i in range(len(values))]
col_lens = [max([len(table[i][j]) for i in range(len(table))]) for j in range(len(table[0]))]
table = [[ table[i][j].ljust(col_lens[j]) for j in range(len(table[0]))] for i in range(len(table))]
table_str = " | ".join(table[0]) + "\n"
table_str += " | ".join([ ("-" * col_lens[i]) for i in range(len(col_lens)) ]) + "\n"
table_str += "\n".join([" | ".join(line) for line in table[1:]])
return table_str
def parse():
parser = argparse
parser = argparse.ArgumentParser(description='Measure analysis')
parser.add_argument('-i', nargs=1, required=False, help='import file')
parser.add_argument('--remove_all', action='store_true', help='remove all measure sets')
parser.add_argument('-t', action='store_true', help='generate table')
args = parser.parse_args()
ms_handler = MeasureSetHandler()
self.max = {name: max(cols[name]) for name in cols}
self.min = {name: min(cols[name]) for name in cols}
self.avg = {name: statistics.mean(cols[name]) for name in cols}
self.var = {name: statistics.variance(cols[name]) for name in cols}
if args.remove_all:
confirm = input("Are you sure all measure sets should be removed ? [Yes] / [No]: ")
if confirm == "Yes":
ms_handler.remove_all()
if args.i is not None:
measure_set = MeasureSet()
measure_set.import_from_json(args.i[0], True)
ms_handler.add_measure_set(measure_set)
parse()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment