示例#1
0
 def test_stop(self, _time):
     _time.return_value = 20.0
     t = Timer()
     t.started = 10.0
     t.stop()
     self.assertEqual(t.started, 10.0)
     self.assertEqual(t.stopped, 20.0)
示例#2
0
 def send_reply(self, request, result):
     """
     Send the reply if requested.
     :param request: The received request.
     :type request: Document
     :param result: The request result.
     :type result: object
     """
     sn = request.sn
     data = request.data
     ts = request.ts
     now = time()
     duration = Timer(ts, now)
     address = request.replyto
     log.info('Request: %s processed in: %s', sn, duration)
     if not address:
         return
     try:
         self.producer.send(address,
                            sn=sn,
                            data=data,
                            result=result,
                            timestamp=timestamp())
     except Exception:
         log.exception('Send: reply, failed: %s', result)
示例#3
0
 def test_context(self):
     t = Timer()
     t = t.__enter__()
     self.assertTrue(t.started > 0)
     self.assertEqual(t.stopped, 0)
     t.__exit__()
     self.assertTrue(t.stopped > 0)
示例#4
0
文件: server.py 项目: swipswaps/gofer
def test_performance():
    N = 200
    agent = Agent()
    dog = agent.Dog()
    t = Timer()
    t.start()
    print('measuring performance ...')
    for i in range(0, N):
        dog.bark('performance!')
    t.stop()
    print('total={}, percall={} (ms)'.format(t, (t.duration() / N) * 1000))
    # sys.exit(0)
    # ASYNCHRONOUS
    agent = Agent(wait=0)
    dog = agent.Dog()
    t = Timer()
    t.start()
    print('measuring (async) performance ...')
    for i in range(0, N):
        dog.bark('performance!')
    t.stop()
    print('total={}, percall={} (ms)'.format(t, (t.duration() / N) * 1000))
    sys.exit(0)
示例#5
0
文件: server.py 项目: swipswaps/gofer
def test_memory():
    N = 10000
    with open(__file__) as fp:
        content = fp.read()
    agent = Agent(data=content)
    dog = agent.Dog()
    t = Timer()
    t.start()
    print('testing memory ...')
    for n in range(0, N):
        dog.bark('hello!')
        print('tested {}'.format(n))
    t.stop()
    print('total={}, percall={} (ms)'.format(t, (t.duration() / N) * 1000))
    sys.exit(0)
示例#6
0
 def test_str(self):
     t = Timer()
     # not started
     self.assertEqual(str(t), 'not-running')
     # started but not stopped
     t.started = 1
     self.assertEqual(str(t), 'started: %d (running)' % t.started)
     # milliseconds
     t.started = 0.10
     t.stopped = 0.25
     self.assertEqual(str(t), '150 (ms)')
     # seconds
     t.started = 10.0
     t.stopped = 25.0
     self.assertEqual(str(t), '15.000 (seconds)')
     # minutes
     t.started = 10.0
     t.stopped = 100.0
     self.assertEqual(str(t), '1.500 (minutes)')
示例#7
0
 def test_unicode(self):
     t = Timer()
     # not started
     self.assertEqual(unicode(t), 'idle')
     # started but not stopped
     t.started = 1
     self.assertEqual(unicode(t), 'started')
     # milliseconds
     t.started = 0.10
     t.stopped = 0.25
     self.assertEqual(unicode(t), '150 (ms)')
     # seconds
     t.started = 10.0
     t.stopped = 25.0
     self.assertEqual(unicode(t), '15.000 (seconds)')
     # minutes
     t.started = 10.0
     t.stopped = 100.0
     self.assertEqual(unicode(t), '1.500 (minutes)')
示例#8
0
    def get_reply(self, sn, reader):
        """
        Get the reply matched by serial number.
        :param sn: The request serial number.
        :type sn: str
        :param reader: A reader.
        :type reader: gofer.messaging.consumer.Reader
        :return: The matched reply document.
        :rtype: Document
        """
        timer = Timer()
        timeout = float(self.wait)

        while not Thread.aborted():
            timer.start()
            document = reader.search(sn, int(timeout))
            timer.stop()
            elapsed = timer.duration()
            if elapsed > timeout:
                raise RequestTimeout(sn, self.wait)
            else:
                timeout -= elapsed

            if not document:
                raise RequestTimeout(sn, self.wait)

            # rejected
            if document.status == 'rejected':
                raise DocumentError(document.code, document.description,
                                    document.document, document.details)

            # accepted | started
            if document.status in ('accepted', 'started'):
                continue

            # progress reported
            if document.status == 'progress':
                self.on_progress(document)
                continue

            # reply
            return self.on_reply(document)
示例#9
0
 def duration(self):
     t = Timer()
     t.started = 10.0
     t.stopped = 100.0
     self.assertEqual(t.duration(), 90.0)
示例#10
0
 def test_init(self):
     t = Timer()
     self.assertEqual(t.started, 0)
     self.assertEqual(t.stopped, 0)