本文整理汇总了Python中neubot.utils.ticks函数的典型用法代码示例。如果您正苦于以下问题:Python ticks函数的具体用法?Python ticks怎么用?Python ticks使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ticks函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
assert(len(RANDOMBLOCKS.get_block()) == RANDOMBLOCKS.blocksiz)
assert(RANDOMBLOCKS.get_block() != RANDOMBLOCKS.get_block())
fp, total = RandomBody(RANDOMBLOCKS.blocksiz + 789), 0
while True:
block = fp.read(128)
if not block:
break
total += len(block)
assert(total == RANDOMBLOCKS.blocksiz + 789)
fp = RandomBody(RANDOMBLOCKS.blocksiz + 789)
assert(len(fp.read()) == RANDOMBLOCKS.blocksiz)
assert(fp.tell() == 789)
assert(len(fp.read()) == 789)
fp.seek(7)
begin, total = utils.ticks(), 0
try:
while True:
total += len(RANDOMBLOCKS.get_block())
except KeyboardInterrupt:
print "Interrupt"
print "Speed:", utils.speed_formatter(total/(utils.ticks() - begin))
示例2: main
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))
示例3: connection_ready
def connection_ready(self, stream):
request = Message()
#
# With version 2, we upload bytes using chunked transfer
# encoding for TARGET seconds.
#
if self.conf['version'] == 2:
body = BytegenSpeedtest(TARGET)
request.compose(method='POST', chunked=body,
pathquery='/speedtest/upload', host=self.host_header)
request['authorization'] = self.conf[
'speedtest.client.authorization']
stream.send_request(request)
self.ticks[stream] = utils.ticks()
self.bytes[stream] = stream.bytes_sent_tot
return
request.compose(method="POST", body=RandomBody(ESTIMATE["upload"]),
pathquery="/speedtest/upload", host=self.host_header)
request["authorization"] = self.conf.get(
"speedtest.client.authorization", "")
self.ticks[stream] = utils.ticks()
self.bytes[stream] = stream.bytes_sent_tot
stream.send_request(request)
示例4: _waiting_pingback
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)
示例5: resched
def resched(self, delta, *args, **kwargs):
self.time = ticks() + delta
self.timestamp = timestamp() + int(delta)
if args:
self.args = args
if kwargs:
self.kwargs = kwargs
示例6: start_recv
def start_recv(self):
if (self.close_complete or self.close_pending
or self.recv_pending):
return
self.recv_ticks = utils.ticks()
self.recv_pending = True
if self.recv_blocked:
return
self.poller.set_readable(self)
#
# The client-side of an SSL connection must send the HELLO
# message to start the negotiation. This is done automagically
# by SSL_read and SSL_write. When the client first operation
# is a send(), no problem: the socket is always writable at
# the beginning and writable() invokes sosend() that invokes
# SSL_write() that negotiates. The problem is when the client
# first operation is recv(): in this case the socket would never
# become readable because the server side is waiting for an HELLO.
# The following piece of code makes sure that the first recv()
# of the client invokes readable() that invokes sorecv() that
# invokes SSL_read() that starts the negotiation.
#
if self.recv_ssl_needs_kickoff:
self.recv_ssl_needs_kickoff = False
self.handle_readable()
示例7: test_negotiate_add
def test_negotiate_add(self):
"""Verify negotiate behavior when we add a new stream"""
stream = Stream(None)
stream.peername = ("abc", 3456)
m = {
"stream": stream,
"ident": sha1stream(stream),
"module": "abc",
}
negotiator = _Negotiator()
negotiator._finalize_response = lambda m, ln: None
negotiator._send_response = lambda m: None
negotiator.register("abc", None)
negotiator.negotiate(m)
# Do we keep track of the new stream?
self.assertTrue(stream in negotiator._queue)
self.assertTrue(stream in negotiator._known)
# Is the watchdog correctly initialized?
self.assertTrue(negotiator._at_close in stream.atclosev)
self.assertEqual(stream.watchdog, 300)
self.assertTrue(utils.ticks() - stream.created < 1) #XXX
示例8: start
def start(self, message):
self.ticks = utils.ticks()
if self.noisy or not self.interactive:
self.info(message + " in progress...")
self.message = message
else:
sys.stderr.write(message + "...")
示例9: check_timeout
def check_timeout(self, *args, **kwargs):
self.sched(CHECK_TIMEOUT, self.check_timeout)
if self.readset or self.writeset:
now = ticks()
stale = set()
x = self.readset.values()
for stream in x:
if stream.readtimeout(now):
logging.warning("%s: read timeout" % repr(stream))
stale.add(stream)
elif (stream.watchdog > 0 and
now - stream.created > stream.watchdog):
logging.warning("%s: watchdog timeout" % repr(stream))
stale.add(stream)
x = self.writeset.values()
for stream in x:
if stream.writetimeout(now):
logging.warning("%s: write timeout" % repr(stream))
stale.add(stream)
elif (stream.watchdog > 0 and
now - stream.created > stream.watchdog):
logging.warning("%s: watchdog timeout" % repr(stream))
stale.add(stream)
for stream in stale:
self.close(stream)
示例10: _periodic_internal
def _periodic_internal(stream):
""" Periodically snap goodput (internal function) """
context = stream.opaque
utime, stime = os.times()[:2]
utimediff = utime - context.snap_utime
stimediff = stime - context.snap_stime
ticks = utils.ticks()
timediff = ticks - context.snap_ticks
bytesdiff = stream.bytes_in - context.snap_count
context.state.setdefault("goodput_snap", []).append(
{
"ticks": ticks,
"bytesdiff": bytesdiff,
"timediff": timediff,
"utimediff": utimediff,
"stimediff": stimediff,
}
)
logging.debug("raw_clnt: utime, stime = %f, %f", utime, stime)
if timediff > 1e-06:
speed = utils.speed_formatter(bytesdiff / timediff)
logging.debug("raw_clnt: goodput_snap: %s", speed)
context.snap_count = stream.bytes_in
context.snap_ticks = ticks
context.snap_utime = utime
context.snap_stime = stime
示例11: _waiting_pingback
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)
示例12: send_complete
def send_complete(self, stream):
''' Invoked when the send queue is empty '''
if self.version >= 2 and self.state == UPLOADING:
#
# The sender stops sending when the upload has run for
# enough time, notifies peer with CHOKE and waits for
# NOT_INTERESTED.
#
diff = utils.ticks() - self.begin_upload
if diff > TARGET:
self.state = WAIT_NOT_INTERESTED
stream.send_choke()
return
if self.version == 3:
return
#
# TODO Here we should use the random block
# generator but before we do that we should
# also make sure it does not slow down the
# bittorrent test.
#
block = chr(random.randint(32, 126)) * PIECE_LEN
index = random.randrange(self.numpieces)
stream.send_piece(index, 0, block)
示例13: got_piece
def got_piece(self, *args):
stream = args[0]
if self.state != DOWNLOADING:
raise RuntimeError("PIECE when state != DOWNLOADING")
# Start measuring
if not self.saved_ticks:
self.saved_bytes = stream.bytes_recv_tot
self.saved_ticks = utils.ticks()
#
# The download is driven by the sender and
# we just need to discard the pieces.
# Periodically send some requests to the other
# end, with probability 10%.
#
if self.version >= 2:
if self.version == 3 or random.random() < 0.1:
index = random.randrange(self.numpieces)
stream.send_request(index, 0, PIECE_LEN)
return
self.get_piece_old(stream)
示例14: complete
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
示例15: _waiting_rawtest
def _waiting_rawtest(self, stream, data):
''' Waiting for RAWTEST message from client '''
context = stream.opaque
context.bufferise(data)
tmp = context.pullup(len(RAWTEST))
if not tmp:
stream.recv(len(RAWTEST), self._waiting_rawtest)
return
if tmp[4:5] == PING_CODE:
logging.debug('< PING')
stream.send(PINGBACK, self._sent_pingback)
logging.debug('> PINGBACK')
return
if tmp[4:5] != RAWTEST_CODE:
raise RuntimeError('raw_srvr: received invalid message')
logging.debug('< RAWTEST')
logging.info('raw_srvr: waiting for RAWTEST message... complete')
logging.info('raw_srvr: raw test... in progress')
context.count = context.snap_count = stream.bytes_out
context.ticks = context.snap_ticks = utils.ticks()
context.snap_utime, context.snap_stime = os.times()[:2]
message = PIECE_CODE + context.auth * int(LEN_MESSAGE / AUTH_LEN)
context.message = struct.pack('!I', len(message)) + message
stream.send(context.message, self._piece_sent)
#logging.debug('> PIECE')
POLLER.sched(1, self._periodic, stream)
stream.recv(1, self._waiting_eof)