-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_peer_contribution.py
More file actions
executable file
·64 lines (53 loc) · 2.04 KB
/
Copy pathplot_peer_contribution.py
File metadata and controls
executable file
·64 lines (53 loc) · 2.04 KB
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
#!/usr/bin/python
from tr_log_reader import *
from argparse import ArgumentParser
import colors
from PIL import Image
sys.path.insert(0, "visual-experiments")
from gatherer import Gatherer
parser = ArgumentParser()
parser.add_argument("sessiondir", type=str)
parser.add_argument("-t", "--torrent", dest="torrentname", default="")
parser.add_argument("-n", "--filenum", dest="filenum", type=int)
parser.add_argument("-width", type=int, default=1000)
parser.add_argument("-height", type=int, default=500)
parser.add_argument("-output", default="peer_contribution.png")
options = parser.parse_args()
logfilename = "%s/session.log" % options.sessiondir
log = TrLogReader(logfilename, options.torrentname, options.filenum).get_log()
print >> sys.stderr, "found %d chunks" % len(log.chunks)
total_size = max([chunk["end"] for chunk in log.chunks])
def time_to_x(t): return int(t / log.lastchunktime() * options.width)
def byte_to_y(byte_pos): return min(int(float(byte_pos) / total_size * options.height), options.height-1)
class Piece:
def __init__(self, begin, end):
self.begin = begin
self.end = end
peer_colors = [(int(r*255), int(g*255), int(b*255))
for (r,g,b,a) in colors.colors(len(log.peers))]
peers = {}
image = Image.new("RGB", (options.width, options.height), "white")
pixels = image.load()
def render_slice(x):
for (gatherer, color) in zip(peers.values(), peer_colors):
for piece in gatherer.pieces():
y1 = byte_to_y(piece.begin)
y2 = byte_to_y(piece.end)
for y in range(y1, y2):
pixels[x, y] = color
previous_x = None
for chunk in log.chunks:
peer_id = log.peeraddr_to_id[chunk["peeraddr"]]
try:
peer = peers[peer_id]
except KeyError:
peer = peers[peer_id] = Gatherer()
peer.add(Piece(chunk["begin"], chunk["end"]))
new_x = time_to_x(chunk["t"])
if previous_x is None:
render_slice(new_x)
else:
for x in range(previous_x, new_x):
render_slice(x)
previous_x = new_x
image.save(options.output)