Example #1
0
 def _time(self, work, msg):
   with Timer() as timer:
     ret = work()
   elapsed = timer.elapsed
   print('%s in %f seconds.' % (msg, elapsed))
   self.total_time += elapsed
   return ret
Example #2
0
    def ping(self, netloc):
        """Time a single roundtrip to the netloc.

    Note that we don't use actual ICMP pings, because cmd-line ping is
    inflexible and platform-dependent, so shelling out to it is annoying,
    and the ICMP python lib can only be called by the superuser.
    """
        if netloc in _global_pinger_memo:
            return _global_pinger_memo[netloc]

        host, colon, portstr = netloc.partition(':')
        port = int(portstr) if portstr else None
        rt_secs = Pinger.UNREACHABLE
        for _ in xrange(self._tries):
            try:
                with Timer() as timer:
                    conn = httplib.HTTPConnection(host,
                                                  port,
                                                  timeout=self._timeout)
                    conn.request(
                        'HEAD', '/')  # Doesn't actually matter if this exists.
                    conn.getresponse()
                new_rt_secs = timer.elapsed
            except Exception:
                new_rt_secs = Pinger.UNREACHABLE
            rt_secs = min(rt_secs, new_rt_secs)
        _global_pinger_memo[netloc] = rt_secs
        return rt_secs
def test_defer():
  DELAY = 0.5
  results = Queue(maxsize=1)
  def func():
    results.put_nowait('success')
  defer(func, delay=DELAY)
  with Timer() as timer:
    assert results.get() == 'success'
  assert timer.elapsed >= DELAY
Example #4
0
def test_defer():
  clock = ThreadedClock()
  DELAY = 3
  results = Queue(maxsize=1)
  def func():
    results.put_nowait('success')
  defer(func, delay=DELAY, clock=clock)
  with Timer(clock=clock) as timer:
    clock.tick(4)
    assert results.get() == 'success'
  assert timer.elapsed >= DELAY
Example #5
0
def test_timer():
    with Timer() as t:
        time.sleep(0.1)
        assert t.start < time.time()
        assert t.elapsed > 0
        time.sleep(0.1)
        assert t.elapsed > 0.1
        time.sleep(0.1)
        assert t.finish is None
    assert t.elapsed > 0.2
    assert t.finish < time.time()
Example #6
0
def test_defer():
    DELAY = 3

    clock = ThreadedClock()
    results = Queue(maxsize=1)

    def func():
        results.put_nowait('success')

    defer(func, delay=DELAY, clock=clock)

    with Timer(clock=clock) as timer:
        with pytest.raises(Empty):
            results.get_nowait()
        clock.tick(DELAY + 1)
        assert results.get() == 'success'

    assert timer.elapsed == DELAY + 1