parse_logic.py 3.25 KB
Newer Older
1 2 3 4
#!/usr/bin/python3

import os
import sys
Sebastian Renner committed
5
import statistics
6
import matplotlib.pyplot as plt
7 8 9

def parse_capture(filename):
    f = open('measurements/' + filename)
Sebastian Renner committed
10 11
    # Skip the first two false positves (risky)
    counter = -1
12 13 14 15 16 17 18 19 20
    lets_use_a_flag = False
    six_ts = ''
    two_ts = ''
    enc_deltas = {}
    dec_deltas = {}
    for l in f.readlines():
        l_array = l.split(" ")
        bit_field = l_array[-1][:-1]
        # l_array? That's the best you came up with?
Sebastian Renner committed
21 22
        if bit_field == '2':
            two_ts = l_array[0][:-1]
23 24 25
            lets_use_a_flag = True
            continue
        if lets_use_a_flag:
Sebastian Renner committed
26 27 28 29 30 31 32
            if bit_field == '6':
                if counter <= 0:
                    counter = counter + 1
                    lets_use_a_flag = False
                    continue
                six_ts = l_array[0][:-1]
                delta = float(six_ts) - float(two_ts)
33 34 35 36 37 38 39 40
                if counter % 2 == 1:
                    enc_deltas[(counter+1)/2] = delta
                else:
                    dec_deltas[counter/2] = delta
                counter = counter + 1
            else:
                lets_use_a_flag = False
    f.close()
41
    return (enc_deltas, dec_deltas)
42 43 44 45 46

def read_log(d):
    # Soo readlines, much efficient
    f = open(d + '/test_stdout.log', 'r')
    content = f.readlines()
47

48 49 50 51
    are_we_happy = content[-1].split(' ')[-1]
    if are_we_happy != 'SUCCESSFUL\n':
        print ("Test unsuccesful or log file structure corrupted")
        return
52

53
    # I like to split it, split it 
54 55 56 57 58 59
    path = content[0].split(' ')[-1].split('/') 
    if path[-2] == 'ref':
        algorithm = path[-3]
    else:
        algorithm = path[-2]

60 61
    # Path to logic data is in the second to last line
    logic_file = content[-2].split('/')[-1][:-2]
62

63
    f. close()
64

Sebastian Renner committed
65
    print("Evaluating results for %s" % (algorithm))
66 67 68 69

    dicts = parse_capture(logic_file)

    return (algorithm, dicts)
70 71 72 73

def main():
    print('THE LWC BENCHMARK SPLITTER')
    print('powered by Deutsche Bahn')
Sebastian Renner committed
74
    build_dir = 'build/new/'
75 76
    box_plot_data = []
    box_plot_labels = []
77
    for d in os.listdir(build_dir):
78 79 80 81 82 83 84 85 86
        #dicts[0] --> algo
        #dicts[1][0] --> enc
        #dicts[1][1] --> dec
        dicts = read_log(os.path.join(build_dir + d))
        enc_values = dicts[1][0].values()
        dec_values = dicts[1][1].values()
     
        box_plot_data.append(list(enc_values))
        box_plot_labels.append(dicts[0])
87

88 89 90 91 92 93 94 95
        print("Average enc time[s] = %f" % (statistics.mean(enc_values)))
        print("Median enc time[s] = %f" % (statistics.median(enc_values)))
        print("Average dec time[s] = %f"  % (statistics.mean(dec_values)))
        print("Median dec time[s] = %f"  % (statistics.median(dec_values)))
        print()
        
        enc_len = len(dicts[1][0])
        dec_len = len(dicts[1][1])
96

97 98
        if dec_len != enc_len:
            raise Exception("#Encryptions (%d) does not match #decryptions (%d)" % (enc_len, dec_len))
99

100 101 102 103 104 105
        if dec_len != 1089 or enc_len != 1089:
            raise Exception("#Number of encrypted test vectors (%d)/ decrypted test vectors (%d) does not match guidelines (1089)" % (enc_len, dec_len))
    
    plt.boxplot(box_plot_data, labels=box_plot_labels)
    plt.xticks(rotation=90)
    plt.show()
106 107 108
if __name__ == "__main__":
    main()