Demo entry 6177048

Test Data Parser

   

Submitted by Jamie Getty on Oct 10, 2016 at 16:06
Language: Python 3. Code size: 8.2 kB.

import re
import os
import numpy
import matplotlib.pyplot as plt
from pylab import show, savefig

#Parameters that will alter the test output. TODO: Turn into command line args

#Toggles printed output of the program on and off
DEBUG = True

#If True, then the channel tests will be analysed. If False, the MAC tests
CHANNEL_TEST = False

#table: an empty array. Used to create a 2D array
#rows : the number of rows the 2D table will have
def table_init(table, rows):
    for i in range(rows):
        table.append([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

#Print the contents of a 2D array
def print_table(table, rows):
    print("-"*40)
    for i in range(rows):
        print(table[i])
    print("-"*40)

#Turns a table where each row is an experiment, into a column of averaged output
def average_table(table, rows):
    for i in range(rows):
        table[i] = float(sum(table[i])) / float(10)


print("Let's begin")   

#Debug messages we want to search through out text files for. Uses regular expressions
macfinder = re.compile("LogMAC[^0]")
transfinder = re.compile("LogTransceiver0")
transmittersfinder = re.compile("Total Nodes = [\d]+")
simulationfinder = re.compile("Run Number = [\d]+")
radiusfinder = re.compile("Radius = [\d]+")

packetfinder = re.compile("Message [\d]+")
bufferdropfinder = re.compile("Buffer is full, discarding message")

unverifiedmessagefinder = re.compile("Signal start message received from channel")
verifiedmessagefinder = re.compile("Packet was not erroneous, so was forwarded to MAC layer")

backofffinder = re.compile("Maximum number of backoffs reached")
created_packets = 0
misses = 0

#filenames = os.listdir( "/home/jamie/omnetpp-5.0/workspace/git/COSC418/COSC418/COSC418/complete")
filenames = os.listdir("E:\Temporary\\")

channel_test_table = []
table_init(channel_test_table, 40)
    
mac_test_table_buffer = []
table_init(mac_test_table_buffer, 10)

mac_test_table_channel = []
table_init(mac_test_table_channel, 10)

mac_test_table_backoff = []
table_init(mac_test_table_backoff, 10)


for filename in filenames:

    if (not filename.endswith(".swp")) and (filename.endswith(".txt")):
        
        f = open(filename, 'r')

        #In this case, we have found any number of MACs belonging to transmitters. Used in one experiment
        if macfinder.findall(filename):

            #Used in the "at the buffer" test in the MAC test
            if not CHANNEL_TEST:

                if DEBUG:
                    print("Found a transmiter's MAC: ", filename)
                    
                sim = int(simulationfinder.findall(filename)[0].strip("Run Number = "))
                
                if transmittersfinder.findall(filename):
                    
                    transmitters = int(transmittersfinder.findall(filename)[0].strip("Total Nodes = ")) - 1
                    created_packets = 0
                    misses = 0
                    backoffs = 0

                    if DEBUG:
                        print("Transmitters: ", transmitters)
       
                    for line in f.readlines():
                        if packetfinder.findall(line):
                            created_packets = int(packetfinder.findall(line)[0].strip("Message "))
                        elif bufferdropfinder.findall(line):
                            misses = misses + 1
                        elif backofffinder.findall(line):
                            backoffs += 1

                    if DEBUG:
                        print("Number of created packets is ", created_packets)
                        print("Number of misses is ", misses)
                        print("Number of backoffs is",backoffs)
                        print("Your packet loss rate in the buffer is ", float(misses) / float(created_packets))
                        print("Your packet loss rate due to backoffs is ", float(backoffs) / float(created_packets))

                    mac_test_table_buffer[int(transmitters/2) - 1][sim - 1] = float(misses) / float(created_packets)
                    mac_test_table_backoff[int(transmitters/2) - 1][sim - 1] = float(backoffs) / float(created_packets)
                
            
        #In this case, we have found the receiver's transceiver. Used in both experiments
        elif transfinder.findall(filename):

            if DEBUG:
                print("Found the reveicer's transceiver: ", filename)
                
            sim = int(simulationfinder.findall(filename)[0].strip("Run Number = "))

            #Used in the channel test
            if CHANNEL_TEST:
                
                if radiusfinder.findall(filename):
                    radius = int(radiusfinder.findall(filename)[0].strip("Radius = "))

                    if DEBUG:
                        print("The radius is ", radius, ", and sim number is ", sim)

                    transmitted_packets = 0
                    received_packets = 0

                    for line in f.readlines():
                        if unverifiedmessagefinder.findall(line):
                            transmitted_packets += 1
                        elif verifiedmessagefinder.findall(line):
                            received_packets += 1

                    if DEBUG:
                        print("Number of sent packets is ", transmitted_packets)
                        print("Number of received packets is ", received_packets)
                        print("Your packet loss rate in the channel is ", 1 - float(received_packets) / float(transmitted_packets))

                    channel_test_table[radius - 1][sim - 1] = 1 - float(received_packets) / float(transmitted_packets)

            #Used in the "in the channel" test in the MAC test
            else:

                if transmittersfinder.findall(filename):

                    transmitters = int(transmittersfinder.findall(filename)[0].strip("Total Nodes = ")) - 1

                    created_packets = 0

                    if DEBUG:
                        print("Transmitters: ", transmitters)

                    transmitted_packets = 0
                    received_packets = 0

                    for line in f.readlines():
                        if unverifiedmessagefinder.findall(line):
                            transmitted_packets += 1
                        elif verifiedmessagefinder.findall(line):
                            received_packets += 1

                    if DEBUG:
                        print("Number of sent packets is ", transmitted_packets)
                        print("Number of received packets is ", received_packets)
                        print("Your packet loss rate in the channel is ", 1 - float(received_packets) / float(transmitted_packets))

                    mac_test_table_channel[int(transmitters/2) - 1][sim - 1] = 1 - float(received_packets) / float(transmitted_packets)

        f.close()

if CHANNEL_TEST:
    
    average_table(channel_test_table, 40)

    if DEBUG:
        print_table(channel_test_table, 40)

    radii = range(1,41)

    plt.figure()
    plt.plot(radii, channel_test_table)
    plt.grid()
    plt.title("Channel Packet Loss Rate vs Distance")
    plt.xlabel("Distance from Base Station (m)")
    plt.ylabel("Packet Loss Rate")

    show()

else:

    if DEBUG:
        print_table(mac_test_table_buffer, 10)
        print_table(mac_test_table_channel, 10)
        print_table(mac_test_table_backoff, 10)

    average_table(mac_test_table_buffer, 10)
    average_table(mac_test_table_channel, 10)
    average_table(mac_test_table_backoff, 10)

    if DEBUG:
        print_table(mac_test_table_buffer, 10)
        print_table(mac_test_table_channel, 10)
        print_table(mac_test_table_backoff, 10)

    numTransmitters = [2,4,6,8,10,12,14,16,18,20]

    plt.figure()
    plt.plot(numTransmitters, mac_test_table_buffer, 'r', numTransmitters, mac_test_table_channel, 'b', numTransmitters, mac_test_table_backoff, 'g')
    plt.legend(["At the buffer", "In the channel", "Due to Backoffs"], loc='lower right')
    plt.grid()
    plt.title("MAC packet loss rates")
    plt.xlabel("Number of transmitters in the network")
    plt.ylabel("Packet Loss Rate")

    show()

print("Done")

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).