-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
executable file
·176 lines (146 loc) · 6.01 KB
/
script.py
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
#!/usr/bin/env python3
# coding: utf-8
"""
Script d'automatisation pour TGV-over-UDP
Liens utiles :
https://stackoverflow.com/questions/4256107/running-bash-commands-in-python
http://queirozf.com/entries/python-3-subprocess-examples
Gabriel Forien
INSA Lyon 4TC
"""
from subprocess import *
from time import sleep
from sys import argv
from socket import *
def main():
global ip, port, debugLevel, nEchantillons, enBoucle, typeClient, nClients, taille, bufferSize, timeout, cwnd, maxAckDuplique
ip = "134.214.202.221"
port = 10534
debugLevel = 4
nEchantillons = 10
enBoucle = "false"
typeClient = "client1"
nClients = 1
taille = 1
bufferSize = 1480
timeout = 2
cwnd = 1
maxAckDuplique = 3
# traitement des paramètres
assert 2<= len(argv) <=3
assert argv[1] in ["client", "serveur"]
if len(argv) == 3:
assert argv[2] in ["scen1", "scen2", "scen3"]
params = {"scen1": ("client1", "false"),
"scen2": ("client2", "false"),
"scen3": ("client1", "true")}
typeClient, enBoucle = params[argv[2] if len(argv)==3 else "scen1"]
eval(argv[1]+"()")
def serveur():
global ip, port, debugLevel, nEchantillons, enBoucle, typeClient, nClients, taille, bufferSize, timeout, cwnd, maxAckDuplique
# socket serveur
ss = socket(AF_INET, SOCK_STREAM)
ss.bind((ip, port))
ss.listen()
s = ss.accept()[0]
# on compile + chmod
check_call("make -sf src/Makefile".split(" "))
check_call("chmod a+x bin/client1 bin/client2".split(" "))
# test lambda
# debit = serveur_launch(s, 'client1', 1, 5, bufferSize, timeout, cwnd, maxAckDuplique)
# print("débit recu %.2f" % (debit))
# algorithme de recherche
precision = 1
parametres = ['cwnd', 'timeout', 'maxAckDuplique']
callback = lambda b_timeout, c_cwnd, d_maxAckDuplique: serveur_launch(s, 'client1', 1, taille, bufferSize, b_timeout, c_cwnd, d_maxAckDuplique)
minMax = { "bufferSize_min" : 100, "bufferSize_max" : 1490,
"cwnd_min" : 37, "cwnd_max" : 42,
"maxAckDuplique_min" : 1, "maxAckDuplique_max" : 3,
"timeout_min" : 1, "timeout_max" : 4}
for prec in range(1,100):
print("Precision = "+str(prec))
algo_recherche(prec, parametres, minMax, callback)
s.close()
ss.close()
def serveur_launch(s, typeClient, nClients, taille, bufferSize, timeout, cwnd, maxAckDuplique):
global ip, port, debugLevel, nEchantillons, enBoucle
# on tue le process et on le relance avec les bons paramètres
# if run(["pgrep", "java"], stdout=PIPE).returncode == 0:
# pid = run(["pgrep", "java"], stdout=PIPE, universal_newlines=True).stdout.replace("\n", "")
# print("Process déjà lancé: PID "+pid+" -> kill")
# check_call(["kill", pid])
# sleep(1)
# elif "java" in run(["ps","-ax"], stdout=PIPE, universal_newlines=True).stdout:
# print("ERREUR: Process déjà lancé et intuable")
# exit(1)
try:
Popen(["java", "-cp", "bin" , "com.ebgf.TGVOverUDP.Test",
ip, str(port), str(debugLevel), str(bufferSize), str(timeout), str(cwnd), str(maxAckDuplique), enBoucle])
## ATTENTION ## sleep indispensable
sleep(0.3)
##################################
except UnicodeDecodeError as e:
pass
instructions = {"typeClient": typeClient, "nClients": nClients, "fichier" : str(taille)+"Mo"}
s.send(str(instructions).encode())
return float(s.recv(4096).decode())
def client():
global ip, port
while True:
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((ip, port))
while True:
d = eval(s.recv(4096).decode())
# print("Lance: %dx %s pour %s" % (d["nClients"], d["typeClient"], d["fichier"]), end='')
client_cmd = ["time", "-f", "%e", "./bin/"+d["typeClient"], ip, str(port), d["fichier"], "0"]
process = Popen(client_cmd, universal_newlines=True, stdout=PIPE, stderr=PIPE)
out, err = process.communicate()
err = err.split("\n")
if len(err) > 2:
print("petite erreur... ", end='')
try:
debit = taille*8/float(err[len(err)-2])
# print(" -> %.2f Mb/s" % (debit))
s.send(str(debit).encode())
except ValueError:
s.send(str(-1).encode())
except (SyntaxError, ConnectionRefusedError):
# print("Connection lost, try again..")
s.close()
sleep(5)
def algo_recherche(n, dim, var, cb):
f = open("trace_"+str(n)+".log", "w", buffering=1)
for d in dim :
var[d+"_plage"] = var[d+"_max"] - var[d+"_min"]
## pour chaque dimension on initialise le delta
for d in dim:
delta = int(var[d+"_plage"]/2**(n-1))
var[d+"_delta"] = 1 if delta < 1 else delta
x = {}
## on boucle sur les blocs => un index par dimension, chacun variant de 0 à 2**(n-1)
idx = [0 for x in dim]
for i in range((2 **(n-1)) **len(dim)):
# print(str(idx))
for k in range(len(dim)):
var[dim[k]] = int(var[dim[k]+"_min"] + idx[k]*var[dim[k]+"_delta"] + (var[dim[k]+"_delta"]/2))
# print(str(var[dim[k]])+", ", end='')
# print()
cle = "\t".join([str(eval(str(var[x]))) for x in dim])
x[cle] = cb(*[eval(str(var[x])) for x in dim])
print("%s\t%s" % (cle, str(x[cle])), file=f)
## on met à jour les index
idx[0] += 1
for j in range(len(idx)):
if idx[j] == 2**(n-1) and j+1 != len(idx):
idx[j+1] += 1
idx[j] = 0
f.close()
if __name__ == '__main__':
try:
main()
except AssertionError:
print("usage: ./script.py [client|serveur] scen[1|2|3]")
except KeyboardInterrupt as e:
print()
exit(1)