-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_load_curl_hop.py
149 lines (130 loc) · 4.59 KB
/
exp_load_curl_hop.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
from time import time,sleep
from subprocess import check_output, PIPE, Popen
import csv
import simpleflock
import shlex
import os
import string
import random
num=''
FILE = "load_exp_ttfb"
VIV_FILE = "vivaldi_ttfb"
EXP_FILE = "my_proxy"
USER = 'david.pinilla'
PASS = r'|Jn 5DJ\\7inbNniK|m@^ja&>C'
PROXY = "10.228.0.83"
URL = "www.google.com"
def get_cmd2(proxy=PROXY):
hash = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(50))
url = URL+'/'+hash
cmd='curl -x '+proxy+':3128 -U '+USER+':\"'+PASS+'\" -m 180 -w %{time_starttransfer} '+url+' -o /dev/null -s'
#cmd='curl -x '+proxy+':3128 -U '+USER+':\"'+PASS+'\" -m 180 -w %{time_starttransfer} '+url+' -s'
return cmd
def getVivaldiProxy():
with simpleflock.SimpleFlock('/tmp/foolock1'):
with open('proxy_route_table','r') as f:
global PROXY, ema025,temp_ema025,ema005,temp_ema005,ema075,temp_ema075
r = csv.reader(f,delimiter='\t')
new_proxy = None
for row in r:
if row[5] == 'True':
new_proxy = row[0]
break
if new_proxy != PROXY:
PROXY = new_proxy
ema025[PROXY] = EMA(0.25)
temp_ema025[PROXY] = EMA(0.25)
ema005[PROXY] = EMA(0.05)
temp_ema005[PROXY] = EMA(0.05)
ema075[PROXY] = EMA(0.75)
temp_ema075[PROXY] = EMA(0.75)
def getVivaldiDistance(proxy):
result = 0.0
with simpleflock.SimpleFlock('/tmp/foolock1'):
with open('proxy_route_table','r') as f:
r = csv.reader(f,delimiter='\t')
for row in r:
if row[0]==proxy:
result = float(row[1])/1000
return result
class EMA(object):
def __init__(self, a):
self.a = a
self.last = 0
self.count = 0
def compute(self, value):
#data is list of ordered value wich is already clean and numerical
if self.count == 0 :
self.last = float(value)
else:
self.last = self.a *float(value) + (1-self.a)*float(self.last)
self.count = self.count+1
return self.last
with open(FILE,"wb") as f:
f.write("time,TTFB"+num+",TTFB_ema_0.25"+num+",TTFB_temp_ema_0.25"+num+",TTFB_ema_0.05"+num+",TTFB_temp_ema_0.05"+num+",TTFB_ema_0.75"+num+",TTFB_temp_ema_0.75"+num+",type"+num+",vivaldi_time"+num+"\n")
#print("time,TTFB,TTFB_ema_0.25,TTFB_temp_ema_0.25,TTFB_ema_0.5,TTFB_temp_ema_0.5,TTFB_ema_0.75,TTFB_temp_ema_0.75,type,vivaldi_time\n")
with open(EXP_FILE,"wb") as f:
f.write('time,proxy\n')
VIVALDI_PERIOD = 10
last_vivaldi = 0
t=0
START = time()
for i in range(0,15):
sleep(1);
t = time() - START
with open(FILE,'a') as f:
f.write("{0:.1f},{1},{2},{3},{4},{5},{6},{7},{8},{9}\n".format(t,0,0,0,0,0,0,0,'est',0))
with open(EXP_FILE,'a') as f:
f.write("{0:.1f},{1}\n".format(t,'None'))
ema025 = {PROXY:EMA(0.25)}
temp_ema025 = {PROXY:EMA(0.25)}
ema005 = {PROXY:EMA(0.05)}
temp_ema005 = {PROXY:EMA(0.05)}
ema075 = {PROXY:EMA(0.75)}
temp_ema075 = {PROXY:EMA(0.75)}
#getVivaldiProxy()
vivaldi_time = getVivaldiDistance(PROXY)
command = Popen(shlex.split(get_cmd2(PROXY)),stdout=PIPE, stderr=PIPE)
last_value = 0
type = 'meas'
last_time = START
out = 0
while True:
sleep(0.9945)
code = command.poll()
if code is not None:
if code==0 :
out, err = command.communicate()
type = 'meas'
final_out = float(out)-vivaldi_time
last_value = final_out
last_time = time()
command = Popen(shlex.split(get_cmd2(PROXY)),stdout=PIPE, stderr=PIPE)
ema025[PROXY].compute(final_out)
temp_ema025[PROXY].last = ema025[PROXY].last
ema005[PROXY].compute(final_out)
temp_ema005[PROXY].last = ema005[PROXY].last
ema075[PROXY].compute(final_out)
temp_ema075[PROXY].last = ema075[PROXY].last
else:
temp_time = time()
if (temp_time-last_time)>last_value:
last_value = temp_time-last_time
#out = last_value
temp_ema025[PROXY].compute(float(last_value))
temp_ema005[PROXY].compute(float(last_value))
temp_ema075[PROXY].compute(float(last_value))
type = 'est'
t = time()-START
if (t-last_vivaldi) > VIVALDI_PERIOD:
vivaldi_time = getVivaldiDistance(PROXY)
last_vivaldi = t
with open(FILE,'a') as f:
f.write("{0:.1f},{1},{2},{3},{4},{5},{6},{7},{8},{9}\n".format(t,out,ema025[PROXY].last,temp_ema025[PROXY].last,ema005[PROXY].last,temp_ema005[PROXY].last,ema075[PROXY].last,temp_ema075[PROXY].last,type,vivaldi_time))
with open(EXP_FILE,'a') as f:
f.write("{0:.1f},{1}\n".format(t,PROXY))
with simpleflock.SimpleFlock('/tmp/foolock'):
with open(VIV_FILE,'wb') as viv:
viv.write("{},{}\n".format(PROXY,temp_ema005[PROXY].last))
#getVivaldiProxy()
#print("{0:.1f},{1},{2},{3},{4},{5},{6},{7},{8},{9}\n".format(t,out,ema025.last,temp_ema025.last,ema005.last,temp_ema005.last,ema075.last,temp_ema075.last,type,vivaldi_time))