summaryrefslogtreecommitdiff
path: root/validation/test/av_home/av_home.py
blob: 6768a801d7b9d664d357e2d5b392df5aa191f5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf-8 -*-

"""Module allowing to generate P2P report in field test."""

import re, os, sys
import ConfigParser
import av_home_attr
import av_mme
import shlex
import subprocess

def master_script():
    print ("Executing master script")
    test_init ()
    print ("Executing ping on slave PC ("+ av_home_attr.addr +")...")
    av_mme.ping_peer ()
    print ("Sending MME for environment discovery to " + av_home_attr.plug_mac +
    " through " + av_home_attr.iface + " ...")
    av_mme.get_network_env("master")
    raw_input ("Press enter when slave script has been launched")
    print ("Executing upload iperf test on " + av_home_attr.addr + " ...")
    iperf_client_run ()
    server = iperf_server_spawn ()
    raw_input ("Press enter when slave script has finished upload iperf")
    iperf_server_stop (server)
    prepare_points ("master")
    get_general_result ("master")
    prepare_archive ("master")

def slave_script():
    print ("Executing slave script")
    test_init ()
    print ("Executing ping on master PC ("+ av_home_attr.addr +")...")
    av_mme.ping_peer ()
    print ("Sending MME for environment discovery to " + av_home_attr.plug_mac +
    " through " + av_home_attr.iface + " ...")
    av_mme.get_network_env("slave")
    print ("Starting iperf server...")
    server = iperf_server_spawn ()
    raw_input ("Press enter when master script has finished upload iperf test")
    iperf_server_stop (server)
    print ("Executing upload iperf test...")
    iperf_client_run ()
    prepare_points ("slave")
    get_general_result ("slave")
    prepare_archive ("slave")

def config_get_item (config, section, key):
    """Get the value of the key in the section from the config parser"""
    try:
        return config.get (section, key)
    except:
        return None

def parse_config(file):
    config = ConfigParser.RawConfigParser ()
    config.read (file)
    av_home_attr.plug_mac = config_get_item (config, 'Test', 'plug_mac')
    av_home_attr.peer_mac = config_get_item (config, 'Test', 'peer_mac')
    av_home_attr.source_mac = config_get_item (config, 'Test', 'source_mac')
    av_home_attr.iface = config_get_item (config, 'Test', 'iface')
    av_home_attr.addr = config_get_item (config, 'Test', 'addr')
    av_home_attr.output_dir = config_get_item (config, 'Test', 'output_dir')
    av_home_attr.scammer_path = config_get_item (config, 'Test', 'scammer_path')

def set_opts(options):
    if options.plug_mac:
        av_home_attr.plug_mac = options.plug_mac
    if options.peer_mac:
        av_home_attr.peer_mac = options.peer_mac
    if options.source_mac:
        av_home_attr.source_mac = options.source_mac
    if options.addr:
        av_home_attr.addr = options.addr
    if options.output_dir:
        av_home_attr.output_dir = options.output_dir
    if options.iface:
        av_home_attr.iface = options.iface
    if options.scammer_path:
        av_home_attr.scammer_path = options.scammer_path

def check_params():
    errorstr = ""
    error = 0
    mac_re = re.compile (r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$")
    if not re.match (mac_re, av_home_attr.plug_mac) \
            or not re.match (mac_re, av_home_attr.peer_mac) \
            or not re.match (mac_re, av_home_attr.source_mac):
                print "MAC address invalid"
                sys.exit (1)
    if not re.match (r"((((25[0-5])|(2[0-4][0-9])|(1?[0-9]{1,2})).){3})((25[0-5])|(2[0-4][0-9])|(1?[0-9]{1,2}))$",
            av_home_attr.addr):
        print "IP address invalid"
        sys.exit (1)
    if not av_home_attr.scammer_path:
        print "You must set the path to scammer"
        sys.exit (1)

def test_init():
    if not av_home_attr.output_dir:
        av_home_attr.output_dir = "test_home-default"
    else:
        av_home_attr.output_dir = "test_home-%s" % av_home_attr.output_dir
    if not os.path.exists(av_home_attr.output_dir):
        os.mkdir (av_home_attr.output_dir)
    else:
        print "Test directory already exists, choose another name"
        sys.exit (1)

def iperf_server_spawn ():
    args = shlex.split ("iperf -s -u -i 1")
    with open (os.path.join (av_home_attr.output_dir, "server_log"), "w") as f:
        server = subprocess.Popen(args, stdout=f)
    return server

def iperf_server_stop (server):
    server.terminate ()

def iperf_client_run ():
    args = shlex.split ("iperf -c " + av_home_attr.addr + " -t 60 -u -b 100m -i 1")
    with open (os.path.join (av_home_attr.output_dir, "client_log"), "w") as f:
        client = subprocess.Popen(args, stdout=f)
    client.wait ()

def prepare_points (mode):
    # Get min and max value from server logs
    f = open (os.path.join (av_home_attr.output_dir, "server_log"), "r")
    lines = f.readlines ()
    minimum = 0
    maximum = 0
    id = []
    id_re = re.compile (r"^\[ *([1-9][0-9]*)\] .* connected .*$")
    for l in lines:
        result = id_re.match (l)
        if result:
            id.append (result.group(1))
    f.close ()
    f = open (os.path.join (av_home_attr.output_dir, "%s_plot" % mode), "w")
    for i in id:
        res = []
        pattern = r"^\[ *%s\] *[0-9]+\.0- *[0-9]+\.0.*([0-9]+\.?[0-9]+) [KM]Bytes *([0-9]+\.[0-9]+).*" % i
        val_re = re.compile (pattern)
        for l in lines:
            result = val_re.match (l)
            if result:
                value = float (result.group (2))
                if not maximum:
                    maximum = value
                if not minimum:
                    minimum = value
                if value < minimum:
                    minimum = value
                if value > maximum:
                    maximum = value
                res.append (value)
        s = str (minimum) + ";" + str (maximum)
        for v in res:
            s += ";" + str(v)
        print >> f, s
    f.close ()
    # Prepare tonemap points
    f = open (os.path.join (av_home_attr.output_dir, "tonemap"), "r")
    lines = f.readlines ()[1:]
    s = ""
    for l in lines:
        for c in l:
            if ord (c) <= ord('7') and ord (c) >= ord ('0'):
                s += c
    f.close ()
    f = open (os.path.join (av_home_attr.output_dir, "tonemap-%s_plot" % mode), "w")
    print >> f, s,
    f.close ()

def get_general_result (mode):
    # Get average up and down throughput
    f = open (os.path.join (av_home_attr.output_dir, "server_log"), "r")
    lines = f.readlines ()
    result_re = re.compile (r"^.* 0\.0-[0-9]+\.[0-9].* ([0-9]+\.[0-9]+) Mbits/sec")
    avg = ""
    for l in lines:
        result = result_re.match (l)
        if result:
            avg = result.group (1)
            break
    if not avg:
        print "Some error occured during the download test, check peer plug for assert traces."
    else:
        print "Average download throughput: %s Mbps" % avg
    f.close ()
    f = open (os.path.join (av_home_attr.output_dir, "client_log"), "r")
    lines = f.readlines ()
    avg = ""
    for l in lines:
        result = result_re.match (l)
        if result:
            avg = result.groups (1)
            break
    if not avg:
        print "Some error occured during the upload test, check plug for assert traces."
    else:
        print "Average upload throughput: %s Mbps" % avg
    f.close ()
    # Get tonemap stats
    f = open ("tonemask", "r")
    line = f.readlines ()[0]
    available_carrier = 0
    for c in line:
        if c == '0':
            available_carrier += 1
    f.close ()
    f = open (os.path.join (av_home_attr.output_dir, "tonemap-%s_plot" % mode), "r")
    line = f.readlines ()[0]
    mod_list = [0, 0, 0, 0, 0, 0, 0, 0]
    for c in line:
        mod_list[int(c)] += 1
    nb_carrier = sum (mod_list[1:])
    mod = { 1: "BPSK",
            2: "QPSK",
            3: "8-QAM",
            4: "16-QAM",
            5: "64-QAM",
            6: "256-QAM",
            7: "1024-QAM" }
    print "Using %d carriers on %d (%d%%)" % (nb_carrier, available_carrier, (nb_carrier * 100) / available_carrier)
    for i in mod:
        print "With %s modulation: %d (%d%%)" % (mod[i], mod_list[i], (mod_list[i] * 100) / nb_carrier)
    f.close ()

def prepare_archive (mode):
    # Create the archive
    args = shlex.split ("tar cf test-%s.tar soft_version-%s" % (mode, mode))
    p = subprocess.Popen (args, shell=False)
    p.wait ()
    directories = os.listdir (".")
    dir_re = re.compile (r"^test_home-.*$")
    for d in directories:
        if dir_re.match (d):
            args = shlex.split ("tar rf test-%s.tar %s" % (mode, d))
            p = subprocess.Popen (args, shell=False)
            p.wait ()
    args = shlex.split ("bzip2 -zf test-%s.tar" % mode)
    p = subprocess.Popen (args, shell=False)
    p.wait ()