def test_add_2_result_in_empty_file(fs): """ Use a CSVOutput instance to write 2 result in an empty csv file named 'toto.csv' Each result contains power consumption of two sockets Test if: - Before calling the save method, the csv file contains only the header - After calling the save method, the csv file contains 4 lines containing the result values """ output = CSVOutput('toto.csv') result_list = [ Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]), Result('titi', 0, 0.2, [0.5555, 0.6666], [0.7777, 0.8888]), ] for result in result_list: output.add(result) assert os.path.exists('toto.csv') csv_file = open('toto.csv', 'r') assert csv_file.readline() == 'label,timestamp,duration,pkg,dram,socket\n' output.save() for result in result_list: line1 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[0]},{result.dram[0]},0\n""" line2 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[1]},{result.dram[1]},1\n""" assert line1 == csv_file.readline() assert line2 == csv_file.readline() assert csv_file.readline() == ''
def test_add_2_result_in_empty_file_semicolon_separator(fs): """ Use a CSVOutput instance to write 2 result in an empty csv file named 'toto.csv' Each result contains power consumption of two sockets Each values must be separated with a semicolong Test if: - Before calling the save method, the csv file contains only the header - After calling the save method, the csv file contains 4 lines containing the result values separated with semicolon """ output = CSVOutput('toto.csv', separator=';') result_list = [ Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]), Result('titi', 0, 0.2, [0.5555, 0.6666], [0.7777, 0.8888]), ] for result in result_list: output.add(result) assert os.path.exists('toto.csv') csv_file = open('toto.csv', 'r') assert csv_file.readline() == 'label;timestamp;duration;pkg;dram;socket\n' output.save() for result in result_list: line1 = f"""{result.label};{result.timestamp};{result.duration};{result.pkg[0]};{result.dram[0]};0\n""" line2 = f"""{result.label};{result.timestamp};{result.duration};{result.pkg[1]};{result.dram[1]};1\n""" assert line1 == csv_file.readline() assert line2 == csv_file.readline() assert csv_file.readline() == ''
def test_fulldiv(): val = Result('toto', 0, 0.23456, [0.34567], [0.45678, 0.56789]) correct_value = Result('toto', 0, 0.023456, [0.034567], [0.045678, 0.056789]) result = val / 10 assert result.label == correct_value.label assert result.timestamp == correct_value.timestamp assert round(result.duration, 6) == correct_value.duration assert [round(x, 6) for x in result.pkg] == correct_value.pkg assert [round(x, 6) for x in result.dram] == correct_value.dram assert len(result.pkg) == 1 assert len(result.dram) == 2
def test_two_save_call(fs): """ Use a CSVOutput instance to write 1 result in an empty csv file named 'toto.csv' After saving the first result, write another result in the csv file Each result contains power consumption of two sockets Test if: - Before calling the fisrt save method, the csv file contains only the header and one line - After calling the fisrt save method, the csv file contains 2 lines containing the result values - After calling the second save method, the csv file contains 4 lines containing the results values """ result_list = [ Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]), Result('titi', 0, 0.2, [0.5555, 0.6666], [0.7777, 0.8888]), ] output = CSVOutput('toto.csv') assert os.path.exists('toto.csv') csv_file = open('toto.csv', 'r') assert csv_file.readline() == 'label,timestamp,duration,pkg,dram,socket\n' assert csv_file.readline() == '' # end of file result = result_list[0] output.add(result) output.save() line1 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[0]},{result.dram[0]},0\n""" line2 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[1]},{result.dram[1]},1\n""" assert line1 == csv_file.readline() assert line2 == csv_file.readline() assert csv_file.readline() == '' csv_file.close() output.add(result_list[1]) output.save() csv_file = open('toto.csv', 'r') assert csv_file.readline() == 'label,timestamp,duration,pkg,dram,socket\n' for result in result_list: line1 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[0]},{result.dram[0]},0\n""" line2 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[1]},{result.dram[1]},1\n""" assert line1 == csv_file.readline() assert line2 == csv_file.readline() assert csv_file.readline() == ''
def test_dataframe(): """ Add 2 result to a dataframe """ output = DataFrameOutput() result_list = [ Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]), Result('titi', 0, 0.2, [0.5555, 0.6666], [0.7777, 0.8888]), ] output.add(result_list[0]) assert_line_equal_result(output.data, 0, result_list[0]) output.add(result_list[1]) for i in range(2): assert_line_equal_result(output.data, 2 * i, result_list[i])
def test_add_2_result_in_non_empty_file_non_append(fs): """ Use a CSVOutput instance to overwrite 2 results on a non empty csv file named 'toto.csv' Each result contains power consumption of two sockets before adding result, the csv file contains a header an a line of result containing only 0 values Test if: - Before calling the save method, the csv file contains only the header and one line - After calling the save method, the csv file contains 2 lines containing the new results values """ fs.create_file( 'toto.csv', contents='label,timestamp,duration,pkg,dram,socket\n0,0,0,0,0,0\n') output = CSVOutput('toto.csv', append='') result_list = [ Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]), Result('titi', 0, 0.2, [0.5555, 0.6666], [0.7777, 0.8888]), ] for result in result_list: output.add(result) assert os.path.exists('toto.csv') csv_file = open('toto.csv', 'r') assert csv_file.readline() == 'label,timestamp,duration,pkg,dram,socket\n' output.save() for result in result_list: line1 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[0]},{result.dram[0]},0\n""" line2 = f"""{result.label},{result.timestamp},{result.duration},{result.pkg[1]},{result.dram[1]},1\n""" assert line1 == csv_file.readline() assert line2 == csv_file.readline() assert csv_file.readline() == ''
def end(self): """ End energy consumption recording """ ts_end = time_ns() energy_end = self._sensor.energy() delta = energy_end - self._energy_begin duration = ts_end - self._ts_begin pkg = delta[0::2] # get odd numbers pkg = pkg if empty_energy_result(pkg) else None # set result to None if its contains only -1 dram = delta[1::2] # get even numbers dram = dram if empty_energy_result(dram) else None # set result to None if its contains only -1 self._results = Result(self.label, self._ts_begin / 1000000000, duration / 1000, pkg, dram)
def test_save(_): output = MongoOutput(None, None, None) output._collection.insert_many = Mock() result = Result('toto', 0, 0.1, [0.1111, 0.2222], [0.3333, 0.4444]) output.add(result) output.save() args = output._collection.insert_many.call_args[0][0] for i in range(2): assert args[i]['label'] == result.label assert args[i]['timestamp'] == result.timestamp assert args[i]['duration'] == result.duration assert args[i]['pkg'] == result.pkg[i] assert args[i]['dram'] == result.dram[i] assert args[i]['socket'] == i
def test_non_raw_output(): """ run PrintOutput._format_output to print non raw result Test if: the returned string is correct """ result = Result('toto', 0, 0.23456, [0.34567], [0.45678, 0.56789]) correct_value = f"""Label : toto Begin : {time.ctime(result.timestamp)} Duration : 0.2346 us ------------------------------- PKG : \tsocket 0 : 0.3457 uJ ------------------------------- DRAM : \tsocket 0 : 0.4568 uJ \tsocket 1 : 0.5679 uJ -------------------------------""" output = PrintOutput() print(output._format_output(result)) assert output._format_output(result) == correct_value