def connection_ready(self, stream): """ Invoked when the connection is ready """ if self.iteration == 0: STATE.update("test_latency", utils.time_formatter(self.rtts[0])) logging.info("dash: latency %s", utils.time_formatter(self.rtts[0])) # # Pick the greatest rate in the vector that is smaller # than the latest piece rate (saved in speed_kbit). # # Note: we pick one minus the bisect point because we # want to use the closest smaller rate for the next # chunk of "video" that we download. # rate_index = bisect.bisect(self.rates, self.speed_kbit) - 1 if rate_index < 0: rate_index = 0 self.rate_kbit = self.rates[rate_index] count = ((self.rate_kbit * 1000) / 8) * DASH_SECONDS uri = "/dash/download/%d" % count logging.debug("dash: connection ready - rate %d Kbit/s", self.rate_kbit) request = Message() request.compose(method="GET", pathquery=uri, host=self.host_header) if self.parent: auth = self.parent.get_auth() logging.debug("dash: authorization - %s", auth) request["authorization"] = auth self.saved_ticks = utils.ticks() self.saved_cnt = stream.bytes_recv_tot self.saved_times = os.times()[:2] response = Message() # Receive and discard the body response.body.write = lambda piece: None logging.debug( "dash: send request - ticks %f, bytes %d, times %s", self.saved_ticks, self.saved_cnt, self.saved_times ) stream.set_timeout(10) stream.send_request(request, response)
def main(): ''' Unit test for neubot/utils_random.py ''' assert (len(RANDOMBLOCKS.get_block()) == RANDOMBLOCKS.blocksiz) assert (RANDOMBLOCKS.get_block() != RANDOMBLOCKS.get_block()) filep, total = RandomBody(RANDOMBLOCKS.blocksiz + 789), 0 while True: block = filep.read(128) if not block: break total += len(block) assert (total == RANDOMBLOCKS.blocksiz + 789) filep = RandomBody(RANDOMBLOCKS.blocksiz + 789) assert (len(filep.read()) == RANDOMBLOCKS.blocksiz) assert (filep.tell() == 789) assert (len(filep.read()) == 789) filep.seek(7) begin, total = utils.ticks(), 0 while total < 1073741824: total += len(RANDOMBLOCKS.get_block()) elapsed = utils.ticks() - begin print('Elapsed: %s' % utils.time_formatter(elapsed)) print('Speed: %s' % utils.speed_formatter(total / elapsed))
def connection_made(self, sock, rtt=0): if rtt: LOG.debug("ClientHTTP: latency: %s" % utils.time_formatter(rtt)) self.rtt = rtt stream = ClientStream(self.poller) stream.attach(self, sock, self.conf, self.measurer) self.connection_ready(stream)
def main(): ''' Unit test for neubot/utils_random.py ''' assert(len(RANDOMBLOCKS.get_block()) == RANDOMBLOCKS.blocksiz) assert(RANDOMBLOCKS.get_block() != RANDOMBLOCKS.get_block()) filep, total = RandomBody(RANDOMBLOCKS.blocksiz + 789), 0 while True: block = filep.read(128) if not block: break total += len(block) assert(total == RANDOMBLOCKS.blocksiz + 789) filep = RandomBody(RANDOMBLOCKS.blocksiz + 789) assert(len(filep.read()) == RANDOMBLOCKS.blocksiz) assert(filep.tell() == 789) assert(len(filep.read()) == 789) filep.seek(7) begin, total = utils.ticks(), 0 while total < 1073741824: total += len(RANDOMBLOCKS.get_block()) elapsed = utils.ticks() - begin print('Elapsed: %s' % utils.time_formatter(elapsed)) print('Speed: %s' % utils.speed_formatter(total/elapsed))
def _waiting_pingback(self, stream, data): """ Invoke when waiting for PINGBACK """ context = stream.opaque context.bufferise(data) tmp = context.pullup(len(PINGBACK)) if not tmp: stream.recv(len(PINGBACK), self._waiting_pingback) return if tmp[4:5] != PINGBACK_CODE: raise RuntimeError("raw_clnt: received invalid message") timediff = utils.ticks() - context.alrtt_ticks context.state.setdefault("alrtt_list", []).append(timediff) logging.debug("< PINGBACK") logging.debug("raw_clnt: alrtt_sample: %f", timediff) context.alrtt_cnt -= 1 if context.alrtt_cnt > 0: self._send_ping(stream) return alrtt_list = context.state["alrtt_list"] alrtt_avg = sum(alrtt_list) / len(alrtt_list) context.state["alrtt_avg"] = alrtt_avg latency = utils.time_formatter(alrtt_avg) logging.info("raw_clnt: alrtt_avg: %s", latency) STATE.update("test_progress", "50%", publish=False) STATE.update("test_latency", latency) logging.info("raw_clnt: estimating ALRTT... complete") logging.info("raw_clnt: raw goodput test... in progress") logging.debug("> RAWTEST") stream.send(RAWTEST, self._rawtest_sent)
def complete(self, done="done\n"): elapsed = utils.time_formatter(utils.ticks() - self.ticks) done = "".join([done.rstrip(), " [in ", elapsed, "]\n"]) if not self.message: self.message = "???" self.info(self.message + "..." + done) self.message = None
def _waiting_pingback(self, stream, data): ''' Invoke when waiting for PINGBACK ''' #context = stream.opaque #context.bufferise(data) #tmp = context.pullup(len(PINGBACK)) #if not tmp: stream.recv(len(PINGBACK), self._waiting_pingback) return if tmp[4:5] != PINGBACK_CODE: raise RuntimeError('skype_clnt: received invalid message') timediff = utils.ticks() - context.alrtt_ticks context.state.setdefault('alrtt_list', []).append(timediff) logging.debug('< PINGBACK') logging.debug('skype_clnt: alrtt_sample: %f', timediff) context.alrtt_cnt -= 1 if context.alrtt_cnt > 0: self._send_ping(stream) return alrtt_list = context.state['alrtt_list'] alrtt_avg = sum(alrtt_list) / len(alrtt_list) context.state['alrtt_avg'] = alrtt_avg latency = utils.time_formatter(alrtt_avg) logging.info('skype_clnt: alrtt_avg: %s', latency) STATE.update('test_latency', latency) logging.info('skype_clnt: estimating ALRTT... complete') logging.info('skype_clnt: skype goodput test... in progress') logging.debug('> RAWTEST') stream.send(RAWTEST, self._skypetest_sent)
def handle_connect(self, connector, sock, rtt, sslconfig, state): logging.info("raw_clnt: connection established with %s", connector) logging.info("raw_clnt: connect_time: %s", utils.time_formatter(rtt)) state["connect_time"] = rtt Stream(sock, self._connection_ready, self._connection_lost, sslconfig, "", ClientContext(state)) STATE.update("test", "raw") state["mss"] = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_MAXSEG) state["rcvr_data"] = []
def connection_made(self, sock, rtt=0): ''' Invoked when the connection is created ''' if rtt: LOG.debug("ClientHTTP: latency: %s" % utils.time_formatter(rtt)) self.rtt = rtt stream = ClientStream(self.poller) stream.attach(self, sock, self.conf) self.connection_ready(stream)
def handle_connect(self, connector, sock, rtt, sslconfig, state): logging.info('raw_clnt: connection established with %s', connector) logging.info('raw_clnt: connect_time: %s', utils.time_formatter(rtt)) state['connect_time'] = rtt Stream(sock, self._connection_ready, self._connection_lost, sslconfig, '', ClientContext(state)) STATE.update('test', 'raw') state['mss'] = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_MAXSEG) state['rcvr_data'] = []
def complete(self, done="done\n"): elapsed = utils.time_formatter(utils.ticks() - self.ticks) done = "".join([done.rstrip(), " [in ", elapsed, "]\n"]) if self.noisy or not self.interactive: if not self.message: self.message = "???" self.info(self.message + "..." + done) self.message = None else: sys.stderr.write(done)
def connection_made(self, sock, endpoint, rtt): ''' Invoked when the connection is created ''' if rtt: logging.debug("ClientHTTP: latency: %s", utils.time_formatter(rtt)) self.rtt = rtt # XXX If we didn't connect via connect_uri()... if not self.host_header: self.host_header = utils_net.format_epnt(endpoint) stream = ClientStream(self.poller) stream.attach(self, sock, self.conf) self.connection_ready(stream)
def connection_made(self, sock, rtt=0): if rtt: latency = utils.time_formatter(rtt) LOG.complete("done, %s" % latency) STATE.update("test_latency", latency) self.rtt = rtt stream = StreamBitTorrent(self.poller) if not self.connector_side: # # Note that we use self.__class__() because self # might be a subclass of PeerNeubot. # peer = self.__class__(self.poller) peer.configure(self.conf) else: peer = self stream.attach(peer, sock, peer.conf) stream.watchdog = self.conf["bittorrent.watchdog"]
def connection_made(self, sock, endpoint, rtt): if rtt: latency = utils.time_formatter(rtt) logging.info("BitTorrent: connecting to %s ... done, %s", str(utils_net.getpeername(sock)), latency) STATE.update("test_latency", latency) self.rtt = rtt stream = StreamBitTorrent(self.poller) if not self.connector_side: # # Note that we use self.__class__() because self # might be a subclass of PeerNeubot. # peer = self.__class__(self.poller) peer.configure(self.conf) # Inherit version peer.version = self.version else: peer = self stream.attach(peer, sock, peer.conf) stream.watchdog = self.conf["bittorrent.watchdog"]
# You should have received a copy of the GNU General Public License # along with Neubot. If not, see <http://www.gnu.org/licenses/>. # ''' Unit test for neubot/utils_random.py ''' import sys sys.path.insert(0, '.') from neubot import utils BEFORE = utils.ticks() from neubot.utils_random import RANDOMBLOCKS from neubot.utils_random import RandomBody ELAPSED = utils.ticks() - BEFORE print('Time to import: %s' % (utils.time_formatter(ELAPSED))) def main(): ''' Unit test for neubot/utils_random.py ''' assert (len(RANDOMBLOCKS.get_block()) == RANDOMBLOCKS.blocksiz) assert (RANDOMBLOCKS.get_block() != RANDOMBLOCKS.get_block()) filep, total = RandomBody(RANDOMBLOCKS.blocksiz + 789), 0 while True: block = filep.read(128) if not block: break total += len(block) assert (total == RANDOMBLOCKS.blocksiz + 789)
def update(self): if self.finished: return # # Decide whether we can transition to the next phase of # the speedtest or not. Fall through to next request if # needed, or return to the caller and rewind the stack. # ostate = self.state if not self.state: self.state = "negotiate" del QUEUE_HISTORY[:] elif self.state == "negotiate": if self.conf.get("speedtest.client.unchoked", False): logging.info("* speedtest: %s ... authorized to " "take the test\n", self.state) self.state = "latency" elif "speedtest.client.queuepos" in self.conf: queuepos = self.conf["speedtest.client.queuepos"] logging.info("* speedtest: %s ... waiting in queue, " "pos %s\n", self.state, queuepos) STATE.update("negotiate", {"queue_pos": queuepos}) QUEUE_HISTORY.append(queuepos) elif self.state == "latency": tries = self.conf.get("speedtest.client.latency_tries", 10) if tries == 0: # Calculate average latency latency = self.conf["speedtest.client.latency"] latency = sum(latency) / len(latency) self.conf["speedtest.client.latency"] = latency # Advertise the result STATE.update("test_progress", "33%", publish=False) STATE.update("test_latency", utils.time_formatter(latency)) logging.info("* speedtest: %s ... done, %s\n", self.state, utils.time_formatter(latency)) self.state = "download" else: self.conf["speedtest.client.latency_tries"] = tries - 1 elif self.state in ("download", "upload"): if len(self.streams) == self.conf.get("speedtest.client.nconn", 1): # Calculate average speed speed = self.conf["speedtest.client.%s" % self.state] elapsed = max(map(lambda t: t[1], speed)) - min(map(lambda t: t[0], speed)) speed = sum(map(lambda t: t[2], speed)) / elapsed # # O(N) loopless adaptation to the channel w/ memory # TODO bittorrent/peer.py implements an enhanced version # of this algorithm, with a cap to the max number of # subsequent tests. In addition to that, the bittorrent # code also anticipates the update of target_bytes. # if elapsed > LO_THRESH: ESTIMATE[self.state] *= TARGET / elapsed self.conf["speedtest.client.%s" % self.state] = speed # Advertise STATE.update("test_%s" % self.state, utils.speed_formatter(speed), publish=False) logging.info("* speedtest: %s ... done, %s\n", self.state, utils.speed_formatter(speed)) if self.state == "download": STATE.update("test_progress", "66%") self.state = "upload" else: STATE.update("test_progress", "100%") self.state = "collect" elif elapsed > LO_THRESH / 3: del self.conf["speedtest.client.%s" % self.state] ESTIMATE[self.state] *= TARGET / elapsed else: del self.conf["speedtest.client.%s" % self.state] ESTIMATE[self.state] *= 2 else: # Wait for all pending requests to complete return elif self.state == "collect": logging.info("* speedtest: %s ... done\n", self.state) self.cleanup() return else: raise RuntimeError("Invalid state") # # Perform state transition and run the next phase of the # speedtest. Not all phases need to employ all the connection # with the upstream server. # if self.state == "negotiate": ctor, justone = ClientNegotiate, True elif self.state == "latency": ctor, justone = ClientLatency, True elif self.state == "download": ctor, justone = ClientDownload, False elif self.state == "upload": ctor, justone = ClientUpload, False elif self.state == "collect": ctor, justone = ClientCollect, True else: raise RuntimeError("Invalid state") if ostate != self.state: self.child = ctor(self.poller) self.child.configure(self.conf) self.child.host_header = self.host_header if self.state not in ("negotiate", "collect"): if ostate == "negotiate" and self.state == "latency": STATE.update("test", "speedtest") else: STATE.update(self.state) logging.info("* speedtest: %s in progress...", self.state) elif self.state == "negotiate": logging.info("* speedtest: %s in progress...", self.state) while self.streams: # # Override child Time-To-Connect (TTC) with our TTC # so for the child it's like it really performed the # connect(), not us. # self.child.rtts = self.rtts self.child.connection_ready(self.streams.popleft()) if justone: break
# along with Neubot. If not, see <http://www.gnu.org/licenses/>. # ''' Unit test for neubot/utils_random.py ''' import sys sys.path.insert(0, '.') from neubot import utils BEFORE = utils.ticks() from neubot.utils_random import RANDOMBLOCKS from neubot.utils_random import RandomBody ELAPSED = utils.ticks() - BEFORE print('Time to import: %s' % (utils.time_formatter(ELAPSED))) def main(): ''' Unit test for neubot/utils_random.py ''' assert(len(RANDOMBLOCKS.get_block()) == RANDOMBLOCKS.blocksiz) assert(RANDOMBLOCKS.get_block() != RANDOMBLOCKS.get_block()) filep, total = RandomBody(RANDOMBLOCKS.blocksiz + 789), 0 while True: block = filep.read(128) if not block: break total += len(block) assert(total == RANDOMBLOCKS.blocksiz + 789)