Statistics
| Branch: | Revision:

nepatest_peerstreamer / src / peerstreamer.py @ 97381a53

History | View | Annotate | Download (12.8 KB)

1
import sys
2
from time import *
3
import csv
4
import numpy as np
5
sys.path.append('../')
6
from network_builder import *
7
from os import kill, path, makedirs
8
from matplotlib.pyplot import ion
9
from random import sample, randint
10

    
11
from test_generic import *
12

    
13
class PSTest(MininetTest):
14
    def __init__(self, mininet, conf_args):
15
        if "duration" in conf_args:
16
            duration = int(conf_args["duration"])
17
        super(PSTest,self).__init__(mininet, path, duration)
18
        self.source = None
19
        self.hosts = []
20
        self.peer_opt_params = {}
21
        self.source_opt_params = {}
22
        self.set_opt_params(conf_args)
23
        self.test_label = "normal"
24
        self.cmd = "./streamer"
25

    
26
    def setTestLabel(self,s):
27
        self.test_label = s
28

    
29
    def launchPS(self,host,params,stdout,stderr):
30
        #cmd = "valgrind ./streamer-debug"
31
        params.update(self.peer_opt_params)
32
        return self.bgCmd(host,True,self.cmd,*reduce(lambda x, y: x + y, params.items()) + ('>',stdout,'2>',stderr))
33

    
34
    def launchPeer(self,host,source):
35
        idps = randint(0,100)
36
        logfile = self.prefix+host.name.split('_')[0]+"-"+str(idps)+"_peerstreamer_"+self.test_label+"_$(date +%s).log"
37
        params = {}
38
        params['-i'] = source.defaultIntf().ip
39
        params['-I'] = host.defaultIntf().name
40
        params['-p'] = self.source_opt_params['-P']
41
        params['-P'] = str(randint(4000,8000))
42
        return self.launchPS(host,params,'/dev/null',logfile)
43

    
44
    def launchSource(self,host):
45
        idps = randint(0,100)
46
        logfile = self.prefix+host.name.split('_')[0]+"-"+str(idps)+"_source_"+self.test_label+"_$(date +%s).log"
47
        params = {}
48
        params['-I'] = host.defaultIntf().name
49
        params.update(self.source_opt_params)
50
        return self.launchPS(host,params,'/dev/null',logfile)
51

    
52
    def set_opt_params(self,conf_args):
53
        if "video_file" in conf_args:
54
            self.source_opt_params['-f'] = conf_args["video_file"] + ",loop=1"
55
        else:
56
            self.source_opt_params['-f'] = "bunny.ts,loop=1"
57
        if "streamer_cmd" in conf_args:
58
            self.cmd = conf_args["streamer_cmd"]
59

    
60
        if "aframe_per_chunk" in conf_args:
61
            self.source_opt_params['-f'] += ",aframe=" + conf_args["aframe_per_chunk"]
62
        if "source_chunk_multiplicity" in conf_args:
63
            self.source_opt_params['-m'] = conf_args["source_chunk_multiplicity"]
64
        if "source_port" in conf_args:
65
            self.source_opt_params['-P'] = conf_args["source_port"]
66
        else:
67
            self.source_opt_params['-P'] = str(7000)
68
        if "push_strategy" in conf_args:
69
            if conf_args["push_strategy"] != "0":
70
                self.peer_opt_params['--push_strategy'] = ""
71

    
72

    
73
        if "chunks_per_second" in conf_args:
74
            self.peer_opt_params['-c'] = conf_args["chunks_per_second"]
75
        if "topology_config" in conf_args:
76
            self.peer_opt_params['-t'] = conf_args["topology_config"]
77
        if "neigh_size" in conf_args:
78
            self.peer_opt_params['-M'] = conf_args["neigh_size"]
79
        if "chunks_per_offer" in conf_args:
80
            self.peer_opt_params['-O'] = conf_args["chunks_per_offer"]
81
        if "chunk_buffer_size" in conf_args:
82
            self.peer_opt_params['-b'] = conf_args["chunk_buffer_size"]
83
        if "log_chunks" in conf_args:
84
            if conf_args["log_chunks"] != "0":
85
                self.peer_opt_params['--chunk_log'] = ""
86
        if "log_neighbourhood" in conf_args:
87
            if conf_args["log_neighbourhood"] != "0":
88
                self.peer_opt_params['--neighbourhood_log'] = ""
89
        if "log_signals" in conf_args:
90
            if conf_args["log_signals"] != "0":
91
                self.peer_opt_params['--signal_log'] = ""
92
        if "distoptimization" in conf_args:
93
            if conf_args["distoptimization"] != "0":
94
                self.peer_opt_params['--distoptimization'] = ""
95
        if "xloptimization" in conf_args:
96
            if conf_args["xloptimization"] != "0":
97
                self.peer_opt_params['--xloptimization'] =  conf_args["xloptimization"]
98

    
99

    
100
    def runTest(self):
101
        info("*** Launching PeerStreamer test\n")
102
        info("Data folder: "+self.prefix+"\n")
103
        if self.source:
104
            self.launchSource(self.source)
105

    
106
        for h in self.hosts:
107
            self.launchPeer(h,self.source)
108
        info("\nWaiting completion...\n")
109
        self.wait(self.duration, log_resources={'cpu': 'cpu_usage',
110
                                                'mem': 'mem_usage',
111
                                                'net': 'net_usage'})
112

    
113
        self.killAll()
114

    
115
class PSHostsTest(PSTest):
116
    def __init__(self, mininet, name, args):
117
        source_name = args["source_name"]
118
        peer_names = args["peer_names"]
119
        super(PSHostsTest, self).__init__(mininet, args)
120
        self.source = mininet.get(source_name)
121
        for n in peer_names.split():
122
            self.hosts.append(mininet.get(n))
123
        self.setPrefix(name)
124

    
125
class PSRandomTest(PSTest):
126
    def __init__(self, mininet, name, args):
127
        num_peers = int(args["num_peers"])
128
        super(PSRandomTest,self).__init__(mininet,args)
129
        self.hosts = self.getHostSample(num_peers)
130
        if len(self.hosts) > 0:
131
            self.source = self.hosts.pop()
132
        self.setPrefix(name)
133

    
134

    
135
class PSRandomNTest(PSRandomTest):
136
    def __init__(self, mininet, name, args):
137
        args["num_peers"] = "0"
138
        super(PSRandomNTest,self).__init__(mininet,name,args)
139
        self.min_nodes = int(args["min_nodes_num"])
140
        self.max_nodes = int(args["max_nodes_num"])
141
        self.nodes_inc = int(args["nodes_num_inc"])
142

    
143
    def setTestLabel(self,s):
144
        self.test_label = s + "-"+str(len(self.hosts)+1)+"peers"
145

    
146
    def runTest(self):
147
        for n in range(self.min_nodes,self.max_nodes + 1,self.nodes_inc):
148
            self.hosts = self.getHostSample(n)
149
            self.source = self.hosts.pop()
150
            self.setTestLabel("normal")
151
            super(PSRandomTest,self).runTest()
152

    
153

    
154
class PSXLOptimizationTest(PSRandomTest):
155
    def __init__(self, mininet, name, args):
156
        super(PSXLOptimizationTest,self).__init__(mininet,name,args)
157
        self.paths_file = self.prefix+"shortest_"+str(int(time()))+".paths"
158
        self.dumpShortestPaths()
159
        self.resetNetStatistics()
160
        fp = open(self.prefix+"PSTestNET_"+str(int(time()))+".info",'w')
161
        if self.hosts and self.source:
162
            wr = csv.writer(fp,quoting=csv.QUOTE_NONE)
163
            wr.writerow([host.name for host in self.hosts]+[self.source.name])
164
            fp.close()
165

    
166
    def dumpShortestPaths(self):
167
        assert(self.net.gg)
168
        sp = open(self.paths_file,'w')
169
        paths = nx.all_pairs_dijkstra_path(self.net.gg, weight='weight')
170
        nodes = paths.keys()
171
        while len(nodes):
172
            node1 = nodes.pop()
173
            for node2 in nodes:
174
                path = paths[node1][node2]
175
                old_n = None
176
                for n in path:
177
                    if old_n is not None:
178
                        w = int(round(self.net.gg[old_n][n]['weight']))
179
                        # sp.write(str(w))
180
                        sp.write(str(1))
181
                        sp.write(",")
182

    
183
                    old_n = n
184
                    h = self.net.get(n)
185
                    sp.write(h.defaultIntf().ip)
186
                    if n != path[len(path)-1]:
187
                        sp.write(",")
188
                    else:
189
                        sp.write("\n")
190
        sp.close()
191

    
192
    def resetNetStatistics(self):
193
        self.sent_packets, self.sent_bytes = self.net.sentPackets() # base value
194
        self.links = {}
195
        for l in self.net.getLinks():
196
          self.links[l] = self.net.linkSentBytes(l) # base value
197

    
198
    def jain_fairness(self,values):
199
        n = len(values)
200
        den = float(n*sum([x**2 for x in values]))
201
        if den >0:
202
            return (sum(values)**2)/den
203
        else:
204
            return 0
205

    
206
    def get_stats(self):
207
        links = self.bytes_per_link()
208

    
209
        logfile = self.prefix+"linkdistribution_"+self.test_label+\
210
                "_"+str(int(time()))+".csv"
211
        fp = open(logfile,'w')
212
        for v in links.values():
213
            fp.write(str(v) + "\n")
214
        fp.close()
215

    
216
        return (sum(links.values()),self.jain_fairness(links.values()))
217

    
218

    
219
    def bytes_per_link(self):
220
        linkos = {}
221
        linkos.update(self.links)
222
        for l in linkos.keys():
223
            linkos[l] = self.net.linkSentBytes(l) - self.links[l]
224
        return linkos
225

    
226
    def sentPackets(self):
227
        sp,sb = self.net.sentPackets()
228
        return sp - self.sent_packets
229

    
230
    def runTest(self):
231
        self.setTestLabel("random")
232
        if '--xloptimization' in self.peer_opt_params.keys():
233
            self.peer_opt_params.pop('--xloptimization')
234
        self.resetNetStatistics()
235
        super(PSXLOptimizationTest,self).runTest()
236
        self.rand_res = self.get_stats()
237

    
238
        self.setTestLabel("optimized")
239
        self.peer_opt_params['--xloptimization'] = self.paths_file
240
        self.resetNetStatistics()
241
        super(PSXLOptimizationTest,self).runTest()
242
        self.optim_res = self.get_stats()
243

    
244
        self.setTestLabel("hopcount")
245
        self.peer_opt_params['--xloptimization'] = self.paths_file + ",hopcount=1"
246
        self.resetNetStatistics()
247
        super(PSXLOptimizationTest,self).runTest()
248
        self.hopcount_res = self.get_stats()
249

    
250
#        self.setTestLabel("scaled")
251
#        if '-M' in self.peer_opt_params.keys():
252
#            self.peer_opt_params['--xloptimization'] = self.paths_file + ",expected_degree="+str(self.peer_opt_params["-M"])
253
#        else:
254
#            self.peer_opt_params['--xloptimization'] = self.paths_file + ",expected_degree=30"
255
#        self.resetNetStatistics()
256
#        super(PSXLOptimizationTest,self).runTest()
257
#        self.scaled_res = self.get_stats()
258

    
259
        print "Normal case: " + str(self.rand_res)
260
        print "Optimized case: " + str(self.optim_res)
261
        print "Hopcount case: " + str(self.hopcount_res)
262
#        print "Scaled case: " + str(self.scaled_res)
263

    
264
        fp = open(self.prefix+'NetLoad.csv','a')
265
        fp.write("RAND," + str(self.rand_res[0]) + "," + str(self.rand_res[1]))
266
        fp.write("\n")
267
        fp.write("OPTIM," + str(self.optim_res[0]) + "," + str(self.optim_res[1]))
268
        fp.write("\n")
269
        fp.write("HOPCOUNT," + str(self.hopcount_res[0]) + "," + str(self.hopcount_res[1]))
270
        fp.write("\n")
271
#        fp.write("SCALED," + str(self.scaled_res[0]) + "," + str(self.scaled_res[1]))
272
#        fp.write("\n")
273
        fp.close()
274

    
275

    
276
class PSXLOptimizationNTest(PSXLOptimizationTest):
277
    def __init__(self, mininet, name, args):
278
        args["num_peers"] = "0"
279
        super(PSXLOptimizationNTest,self).__init__(mininet,name,args)
280
        self.min_nodes = int(args["min_nodes_num"])
281
        self.max_nodes = int(args["max_nodes_num"])
282
        self.nodes_inc = int(args["nodes_num_inc"])
283

    
284
    def setTestLabel(self,s):
285
        self.test_label = s + "-"+str(len(self.hosts)+1)+"peers"
286

    
287
    def runTest(self):
288
        fp = open(self.prefix+"NTestResults_"+str(int(time()))+".csv",'w')
289
        fp.write("NeighSize,pkts_normal,netimpact_normal,pkts_hopcount,netimpact_hopcount,pkts_optimized,netimpact_optimized\n")
290
        for n in range(self.min_nodes,self.max_nodes + 1,self.nodes_inc):
291
            self.hosts = self.getHostSample(n)
292
            self.source = self.hosts.pop()
293
            super(PSXLOptimizationNTest,self).runTest()
294
            fp.write(str(n)+",")
295
            fp.write(str(self.norm_res[0])+","+str(self.norm_res[1])+",")
296
            fp.write(str(self.hopcount_res[0])+","+str(self.hopcount_res[1])+",")
297
            fp.write(str(self.optim_res[0])+","+str(self.optim_res[1])+"\n")
298
        fp.close()
299

    
300
class PSXLOptimizationNeighTest(PSXLOptimizationTest):
301
    def __init__(self, mininet, name, args):
302
        args["neigh_size"] = "0"
303
        super(PSXLOptimizationNeighTest,self).__init__(mininet,name,args)
304
        self.min_neigh = int(args["min_neigh_num"])
305
        self.max_neigh = int(args["max_neigh_num"])
306
        self.neigh_inc = int(args["neigh_num_inc"])
307

    
308
    def setTestLabel(self,s):
309
        self.test_label = s + "-"+self.peer_opt_params['-M']+"neigh"
310

    
311
    def runTest(self):
312
        fp = open(self.prefix+"NeighTestResults_"+str(int(time()))+".csv",'w')
313
        for n in range(self.min_neigh,self.max_neigh + 1,self.neigh_inc):
314
            self.peer_opt_params['-M'] = str(n)
315
            super(PSXLOptimizationNeighTest,self).runTest()
316
            fp.write(str(n)+",")
317
            fp.write(str(self.norm_res[0])+","+str(self.norm_res[1])+",")
318
            fp.write(str(self.optim_res[0])+","+str(self.optim_res[1])+"\n")
319
        fp.close()
320

    
321
class PSXLOptimizationFullTest(PSTest):
322
    def __init__(self,mininet,name,args):
323
        super(PSXLOptimizationFullTest,self).__init__(mininet,args)
324
        self.neigh_test = PSXLOptimizationNeighTest(mininet,name,args)
325
        self.nodes_test = PSXLOptimizationNTest(mininet,name,args)
326

    
327
    def runTest(self):
328
        self.neigh_test.runTest()
329
        self.nodes_test.runTest()
330