swarm_speed

This commit is contained in:
Alon Zakai 2006-12-10 19:46:07 +00:00
parent 356009f5ee
commit 6511e33b3e
2 changed files with 86 additions and 21 deletions

View File

@ -468,31 +468,18 @@ class manager:
except AttributeError:
pass
# Advanced statistics
# Advanced statistics - these may be SLOW. The client should call these only
# when needed, and perhaps only once in a long while (they are mostly just
# approximations anyhow
# Availability - how many complete copies are among our peers
def calc_availability(self, unique_ID):
peer_info = self.get_core_torrent_peer_info(unique_ID)
return flood_stats.calc_availability(self.get_core_torrent_peer_info(unique_ID))
if len(peer_info) == 0:
return 0
num_pieces = len(peer_info[0].pieces)
freqs = [0]*num_pieces
for peer in peer_info:
for piece in num_pieces:
freqs[piece] = freqs[piece] + peer['pieces'][piece]
minimum = min(freqs)
# frac = freqs.count(minimum + 1) # Does this mean something?
return minimum
# Swarm speed - try to guess the speed of the entire swarm
def calc_swarm_speed(self, unique_ID):
pass
pieces_per_sec = flood_stats.calc_swarm_speed(self.get_core_torrent_peer_info(unique_ID))
piece_length = self.get_core_torrent_state(unique_ID, efficiently=True)
return pieces_per_sec * piece_length
# Miscellaneous minor functions

78
library/flood_stats.py Normal file
View File

@ -0,0 +1,78 @@
#
# Copyright (C) 2006 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import time
# Global variables. Caching of saved data, mostly
old_peer_info = None
old_peer_info_timestamp = None
# Availability - how many complete copies are among our peers
def calc_availability(peer_info):
if len(peer_info) == 0:
return 0
num_pieces = len(peer_info[0].pieces)
freqs = [0]*num_pieces
for peer in peer_info:
for piece in num_pieces:
freqs[piece] = freqs[piece] + peer['pieces'][piece]
minimum = min(freqs)
# frac = freqs.count(minimum + 1) # Does this mean something?
return minimum
# Swarm speed - try to guess the speed of the entire swarm
# We return #pieces / second. The calling function should convert pieces to KB, if it wants
# Note that we return the delta from the last call. If the client calls too soon, this may
# be too unreliable. But the client can smooth things out, if desired
def calc_swarm_speed(peer_info):
if old_peer_info is not None:
new_pieces = 0
peers_known = 0
# List new peers
new_peer_IPs = {} # ip->peerinfo dict (from the core)
for peer in peer_info:
new_peer_IPs[peer['ip']] = peer
for new_IP in new_peer_IPs.keys():
if new_IP in old_peer_IPs.keys():
# We know this peer from before, see what changed
peers_known = peers_known + 1
delta = sum(new_peer_IPs[new_IP].pieces) - sum(old_peer_IPs[new_IP].pieces)
if delta >= 0:
new_pieces = new_pieces + delta
else:
print "Flood.stat.calc_swarm_speed: Bad Delta: ", delta, old_peer_IPs[new_IP].pieces, new_peer_IPs[new_IP].pieces
# Calculate final value
time_delta = time.time() - old_peer_info_timestamp
ret = float(new_pieces)/( float(peers_known) * time_delta )
# Save info
old_peer_info = peer_info
old_peer_info_timestamp = time.time()
old_peer_IPs = new_peer_IPs
return ret