-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.py
executable file
·410 lines (350 loc) · 13.3 KB
/
experiment.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python
from __future__ import print_function, division, unicode_literals
import argparse
import os
import sys
import signal
import subprocess
import platform
import tarfile
import shutil
import time
import json
from collections import defaultdict
from random import random
if sys.version_info.major == 3:
from urllib.request import urlretrieve
else:
from urllib import urlretrieve
PING_COMMAND = "ping -c 10 %s | tail -1 | awk '{print $4}'"
CURL_COMMAND = "curl -o /dev/null -w '{\\n\"time_namelookup\": %{time_namelookup},\\n\"time_connect\": %{time_connect},\\n\"time_appconnect\": %{time_appconnect},\\n\"time_pretransfer\": %{time_pretransfer},\\n\"time_redirect\": %{time_redirect},\\n\"time_starttransfer\": %{time_starttransfer},\\n\"total\": %{time_total}\\n}\\n' "
######
# Ping
######
def get_pings(hosts):
print("Pinging hosts...")
results = {}
for host in hosts:
out = subprocess.check_output(PING_COMMAND % host, shell=True)
try:
ping_min, ping_avg, ping_max, ping_stddev = [float(s) for s in out.decode("utf8").strip().split("/")]
except:
print("ERROR: Failed to ping {}. Please report this error.".format(host))
else:
results[host] = {'min': ping_min, 'avg': ping_avg, 'max': ping_max, 'stddev': ping_stddev}
return results
def curl_stats(url):
try:
out = subprocess.check_output(CURL_COMMAND + url, shell=True).decode("utf8")
return json.loads(out)
except:
print("ERROR: Please ensure you are connected to the internet and rerun the script.")
print("If this persists, please contact the sender.")
def average_by_keys(l):
by_key = defaultdict(list)
for d in l:
for k,v in d.items():
by_key[k].append(v)
return {
"samples": dict(by_key),
"average": {str(k): sum(v) / len(v) for k, v in by_key.items()}
}
def curl_and_average(url, samples=10):
print("Gathering curl stats...")
curls = []
for i in range(samples):
time.sleep(2*random())
print("Attempt {} of {}".format(i+1, samples))
curls.append(curl_stats(url))
return average_by_keys(curls)
class IPFSNode:
def __init__(self, id, address):
assert id in address
self.id = id
self.address = address
class IPFSFile:
def __init__(self, url, hash):
self.url = url
self.hash = hash
HOSTS = {
"npfoss.mit.edu": IPFSNode(
"QmSzpPdWxHMmPDPy8A4igWWwTVPcBHZXueYLzWQsBwHFuU",
"/ip4/18.18.96.12/tcp/14001/p2p/QmSzpPdWxHMmPDPy8A4igWWwTVPcBHZXueYLzWQsBwHFuU"
),
"p1.mit.edu": IPFSNode(
"QmX5Q5cF1F1yvcS2QWMXPu1GsP5kVajH6UbLrTBXWHx8HM",
"/ip4/18.18.248.83/tcp/4001/ipfs/QmX5Q5cF1F1yvcS2QWMXPu1GsP5kVajH6UbLrTBXWHx8HM"
),
}
RESIDENTIAL_HOSTS = {
"lobster.moinnadeem.com": IPFSNode(
"QmeTPkjcFsgBvAm3K18JbC3kDGNbDZ9cs4fibnDb2Lr8je",
"/ip4/66.31.16.203/tcp/9701/ipfs/QmeTPkjcFsgBvAm3K18JbC3kDGNbDZ9cs4fibnDb2Lr8je"
),
}
FILES = {
"ipad": IPFSFile(
"https://www.apple.com/105/media/us/ipad-pro/2020/7be9ce7b-fa4e-4f54-968b-4a7687066ed8/films/feature/ipad-pro-feature-tpl-cc-us-2020_1280x720h.mp4",
"QmfBsQa4iZRsKkELk7QGP4acN4sX6WfvBvVWT4Tz5yuE24"
),
"iphone": IPFSFile(
"https://www.apple.com/v/home/f/images/heroes/iphone-se/hero__dvsxv8smkkgi_large.jpg",
"Qmay7eKcsxZ5UkraAucEGtTsv6LzA5hn3P8JnQQcWaVwcN"
),
}
class IPFSClient:
def __init__(self, ipfs_binary, ipfs_path):
self.ipfs_binary = ipfs_binary
self.ipfs_path = ipfs_path
self.devnull = open(os.devnull, "w")
self._ensure_path()
def teardown(self):
self.kill_daemon()
self._unset_path()
def launch_daemon(self):
if self.daemon_available():
return
elif self.daemon_running():
self.kill_daemon()
print("Launching daemon...")
while not self.daemon_running():
try:
subprocess.Popen(
self._get_command("daemon &"), shell=True, stdout=self.devnull)
except:
self.kill_daemon()
while not self.daemon_available():
print("Waiting for daemon... [may take a min, do not quit]")
time.sleep(1)
print("Success.")
def daemon_available(self):
try:
subprocess.check_call(
self._get_command("stats bitswap"),
shell=True,
stdout=self.devnull,
stderr=self.devnull,
)
return True
except:
return False
def daemon_running(self):
try:
return int(subprocess.check_output("pgrep ipfs", shell=True).strip())
except:
return False
def kill_daemon(self):
pid = self.daemon_running()
while self.daemon_running():
print("Killing daemon...")
try:
os.kill(pid, signal.SIGKILL)
except:
return
def init(self):
try:
subprocess.check_call(
self._get_command("init"),
shell=True,
stdout=self.devnull,
stderr=self.devnull,
)
except:
pass
def is_connected(self, ipfs_node):
addrs = self.check_output("swarm addrs").decode("utf8")
return ipfs_node.id in addrs
def ensure_connected(self, ipfs_node):
if not self.is_connected(ipfs_node):
self.check_output("swarm connect {}".format(ipfs_node.address))
def ensure_disconnected(self, ipfs_node):
if self.is_connected(ipfs_node):
self.check_output("swarm disconnect {}".format(ipfs_node.address))
def call(self, command):
return subprocess.call(self._get_command(command), shell=True)
def time_get(self, hash):
return float(self.time("get {}".format(hash)).strip())
def time(self, command):
# TIMEFORMAT=%R
if os.environ.get("TIMEFORMAT") != "%R":
os.environ["TIMEFORMAT"] = "%R"
try:
return subprocess.check_output("(time " + self._get_command(command) + "&> /dev/null ) 2>&1", shell=True, executable='bash').decode("utf8")
except:
print("ERROR: Failed to run ipfs command: {}. Please report this error".format(command))
exit()
os.environ.pop("TIMEFORMAT", None)
os.unsetenv("TIMEFORMAT")
def check_output(self, command):
try:
return subprocess.check_output(self._get_command(command), shell=True)
except:
print("ERROR: Failed to run ipfs command: {}. Please report this error".format(command))
exit()
def _ensure_path(self):
if os.environ.get("IPFS_PATH") != self.ipfs_path:
os.environ["IPFS_PATH"] = self.ipfs_path
def _unset_path(self):
os.environ.pop("IPFS_PATH", None)
os.unsetenv("IPFS_PATH")
def _get_command(self, command):
return "{} {}".format(self.ipfs_binary, command)
def get_stats(self, file, hosts, samples=10):
print("Collecting ipfs stats...")
gets = []
tries = 0
while len(gets) < samples and tries < samples*2:
tries += 1
print("Attempt {} out of (min: {}, max: {})".format(tries, samples, samples*2))
for h in hosts:
self.ensure_connected(h)
self.check_output("repo gc")
if os.path.exists(file.hash):
os.remove(file.hash)
t = self.time_get(file.hash)
if all(self.is_connected(h) for h in hosts):
gets.append(t)
if os.path.exists(file.hash):
os.remove(file.hash)
return {"tries": tries, "gets": gets, "average": sum(gets) / len(gets)}
class IPFSDownloader:
BASE_URL = "https://dist.ipfs.io/go-ipfs/v0.5.0/go-ipfs_v0.5.0_"
FOLDER_NAME = "go-ipfs"
PATH_TO_FOLDER = os.path.join(
os.path.dirname(os.path.realpath(__file__)), FOLDER_NAME)
@classmethod
def run(cls):
if os.path.exists(cls.PATH_TO_FOLDER):
return cls.PATH_TO_FOLDER
cls.download_and_extract(cls.get_ipfs_download_link())
return cls.PATH_TO_FOLDER
@classmethod
def delete(cls):
print("Deleting folder...")
if os.path.exists(cls.PATH_TO_FOLDER):
shutil.rmtree(cls.PATH_TO_FOLDER)
@classmethod
def get_ipfs_download_link(cls):
return cls.BASE_URL + cls.get_ipfs_download_postfix()
@staticmethod
def get_ipfs_download_postfix():
is_64bits = sys.maxsize > 2 ** 32
is_arm = "arm" in platform.machine()
platform_name = sys.platform
if platform_name == "linux" or platform_name == "linux2":
# linux
if is_arm:
if is_64bits:
return "linux-arm64.tar.gz"
return "linux-arm.tar.gz"
if is_64bits:
return "linux-amd64.tar.gz"
return "linux-386.tar.gz"
elif platform_name == "darwin":
# OS X
if is_64bits:
return "darwin-amd64.tar.gz"
return "darwin-386.tar.gz"
elif platform_name == "win32":
# Windows...
sys.exit("Windows is not supported")
@staticmethod
def download_and_extract(url):
print("Downloading...")
file_tmp = urlretrieve(url, filename=None)[0]
tar = tarfile.open(file_tmp)
print("Extracting...")
tar.extractall()
def main():
parser = argparse.ArgumentParser(
description="Experiments. Please contact the sender for questions.")
parser.add_argument("--host",
help="Host a long-running node for the experiment in the background",
action="store_true")
parser.add_argument("--kill",
help="Kill the background node",
action="store_true")
parser.add_argument("-d", "--dotipfs",
help="/path/to/.ipfs/ (default: .ipfs)")
parser.add_argument("-a", "--all",
help="Test both large and small files",
action="store_true")
args = parser.parse_args()
if sys.platform == "win32":
sys.exit("Windows is not supported")
ipfs_folder = IPFSDownloader.run()
if args.dotipfs:
dotipfs = args.dotipfs
else:
dotipfs = os.path.join(ipfs_folder, ".ipfs/")
ipfs = IPFSClient(os.path.join(ipfs_folder, "ipfs"), dotipfs)
ipfs.init()
if args.host:
print(
"""
Thank you for volunteering! You've been selected to host a node in
our experiment.
We expect your computer to remain powered on and connected to the
internet for an extended period of time while we conduct the
experiment in the background.
Please do not tamper with the ./go-ipfs directory or the running ipfs
daemon process.
When you ultimately wish to turn off the experiment, you may run:
./experiment.py --kill
""")
ipfs.launch_daemon()
print("Connecting to server...")
# ipfs.check_output("swarm connect {}".format(HOSTS["npfoss.mit.edu"].address))
# pin the files
for f in FILES.values():
print("pinning {}...".format(f.hash))
ipfs.check_output("pin add {}".format(f.hash))
print("\nIMPORTANT: Please copy and send the following back to the experiment leader:")
ipfs.call("id")
print("Host is running! Pleae do not close this shell.")
while True:
ipfs.launch_daemon()
time.sleep(60)
elif args.kill:
ipfs.kill_daemon()
else:
RESULTS = {'VERSION': 'v0.4.0'}
print("""Thank you for participating! Please remain connected to the
internet, and refrain from streaming videos.""")
RESULTS['pings'] = {
'mit': get_pings(HOSTS),
'residential': get_pings(RESIDENTIAL_HOSTS)
}
RESULTS['curl'] = {
'iphone': curl_and_average(FILES["iphone"].url, samples=5),
}
ipfs.launch_daemon()
for h in HOSTS.values():
ipfs.ensure_disconnected(h)
RESULTS['ipfs_residential'] = {
'iphone': ipfs.get_stats(FILES["iphone"], RESIDENTIAL_HOSTS.values(), samples=10),
}
for h in RESIDENTIAL_HOSTS.values():
ipfs.ensure_disconnected(h)
RESULTS['ipfs_mit'] = {
'iphone': ipfs.get_stats(FILES["iphone"], HOSTS.values(), samples=10),
}
if args.all:
print("Running slow tests...")
RESULTS['curl']['ipad'] = curl_and_average(FILES["ipad"].url, samples=3)
ipfs.launch_daemon()
for h in HOSTS.values():
ipfs.ensure_disconnected(h)
RESULTS['ipfs_residential']['ipad'] = ipfs.get_stats(FILES["ipad"], RESIDENTIAL_HOSTS.values(), samples=3)
for h in RESIDENTIAL_HOSTS.values():
ipfs.ensure_disconnected(h)
RESULTS['ipfs_mit']['ipad'] = ipfs.get_stats(FILES["ipad"], HOSTS.values(), samples=3)
print("Writing output...")
with open('out.json', 'w') as f:
json.dump(RESULTS, f, indent=4, sort_keys=True)
print("\nSUCCESS! Please send out.json back to the sender :)")
ipfs.kill_daemon()
ipfs.teardown()
IPFSDownloader.delete()
if __name__ == "__main__":
main()