parse_logic.py 3.66 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 74 75 76 77 78 79 80 81 82 83 84
def gen_graph_chunks(bp_data, bp_labels, size):
    l = len(bp_data)
    for i in range(0, (l//size) * size, size):
        chunk = bp_data[i:i+size]
        labels = bp_labels[i:i+size]
        plt.boxplot(chunk, labels=labels)
        plt.xticks(rotation=90)
        plt.show()
    # Let's also plot the leftover
    rest = l % size
    if rest != 0:
        plt.boxplot(bp_data[(rest-2*rest):])
        plt.show()

85 86 87
def main():
    print('THE LWC BENCHMARK SPLITTER')
    print('powered by Deutsche Bahn')
Sebastian Renner committed
88
    build_dir = 'build/new/'
89 90
    bp_data = []
    bp_labels = []
91
    for d in os.listdir(build_dir):
92 93 94 95 96 97 98
        #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()
     
99 100
        bp_data.append(list(enc_values))
        bp_labels.append(dicts[0])
101

102 103 104 105 106 107 108 109
        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])
110

111 112
        if dec_len != enc_len:
            raise Exception("#Encryptions (%d) does not match #decryptions (%d)" % (enc_len, dec_len))
113

114 115
        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))
116 117 118 119 120

    gen_graph_chunks(bp_data, bp_labels, 5) 
    #plt.boxplot(bp_data, labels=bp_labels)
    #plt.xticks(rotation=90)
    #plt.show()
121 122 123
if __name__ == "__main__":
    main()