def request_stream(self, request): m = request.urlargs['m'] n = request.urlargs['n'] if m*n > 8388608: # limit at 8 megabytes of total data raise HttpException(status=403) stream = ('Chunk %s\n%s\n\n' % (i+1, ''.join(( choice(characters) for _ in range(m)))) for i in range(n)) request.response.content = stream return request.response
def randompaths(request, num_paths=1, size=250, mu=0, sigma=1): '''Lists of random walks.''' r = [] for p in range(num_paths): v = 0 path = [v] r.append(path) for t in range(size): v += normalvariate(mu, sigma) path.append(v) return r
def request_stream(self, request): m = request.urlargs['m'] n = request.urlargs['n'] if m * n > 8388608: # limit at 8 megabytes of total data raise HttpException(status=403) stream = ('Chunk %s\n%s\n\n' % (i + 1, ''.join((choice(characters) for _ in range(m)))) for i in range(n)) request.response.content = stream return request.response
def testTimeIt(self): msg = b''.join((b'a' for x in range(2**10))) response = self.pool.timeit(10, self.server.address, msg) yield response self.assertTrue(response.locked_time >= 0) self.assertTrue(response.total_time >= response.locked_time) self.assertEqual(response.num_failures, 0)
def spawn_actors(self): '''Spawn new actors if needed. If the :class:`PoolMixin` is spawning do nothing.''' to_spawn = self.cfg.workers - len(self.managed_actors) if self.cfg.workers and to_spawn > 0: for _ in range(to_spawn): self.spawn()
def spawn_actors(self, monitor): '''Spawn new actors if needed. ''' to_spawn = monitor.cfg.workers - len(self.managed_actors) if monitor.cfg.workers and to_spawn > 0: for _ in range(to_spawn): monitor.spawn()
def timeit(self, func, times, *args, **kwargs): '''Usefull little utility for timing responses from server. The usage is simple:: >>> from pulsar.apps import rpc >>> p = rpc.JsonProxy('http://127.0.0.1:8060') >>> p.timeit('ping',10) 0.56... ''' func = getattr(self, func) return multi_async((func(*args, **kwargs) for t in range(times)))
def stop_actors(self, monitor): """Maintain the number of workers by spawning or killing as required """ if monitor.cfg.workers: num_to_kill = len(self.managed_actors) - monitor.cfg.workers for i in range(num_to_kill, 0, -1): w, kage = 0, sys.maxsize for worker in itervalues(self.managed_actors): age = worker.impl.age if age < kage: w, kage = w, age self.manage_actor(monitor, w, True)
def stop_actors(self): """Maintain the number of workers by spawning or killing as required """ if self.cfg.workers: num_to_kill = len(self.managed_actors) - self.cfg.workers for i in range(num_to_kill, 0, -1): w, kage = 0, sys.maxsize for worker in itervalues(self.managed_actors): age = worker.impl.age if age < kage: w, kage = w, age self.manage_actor(w, True)
def testStreamed(self): stream = ('line {0}\n'.format(l + 1) for l in range(10)) r = wsgi.WsgiResponse(content=stream) self.assertEqual(r.status_code, 200) self.assertEqual(r.status, '200 OK') self.assertEqual(r.content, stream) self.assertTrue(r.is_streamed) data = [] for l, a in enumerate(r): data.append(a) self.assertTrue(r.started) self.assertEqual(a, ('line {0}\n'.format(l + 1)).encode('utf-8')) self.assertEqual(len(data), 10)
def testStreamed(self): stream = ('line {0}\n'.format(l+1) for l in range(10)) r = wsgi.WsgiResponse(content=stream) self.assertEqual(r.status_code, 200) self.assertEqual(r.status, '200 OK') self.assertEqual(r.content, stream) self.assertTrue(r.is_streamed) data = [] for l, a in enumerate(r): data.append(a) self.assertTrue(r.started) self.assertEqual(a, ('line {0}\n'.format(l+1)).encode('utf-8')) self.assertEqual(len(data), 10)
def _run_once(self, timeout=None): timeout = timeout or self.poll_timeout self._num_loops += 1 # # Compute the desired timeout if self._callbacks: timeout = 0 elif self._scheduled: timeout = min(max(0, self._scheduled[0].deadline - self.timer()), timeout) # poll events self._poll(timeout) # # append scheduled callback now = self.timer() while self._scheduled and self._scheduled[0].deadline <= now: self._callbacks.append(heappop(self._scheduled)) # # Run callbacks callbacks = self._callbacks todo = len(callbacks) for i in range(todo): exc_info = None callback = callbacks.popleft() try: value = callback() except socket.error as e: if raise_socket_error(e) and self.running: exc_info = sys.exc_info() if self._raise_loop_error(e): exc_info = sys.exc_info() except Exception: exc_info = sys.exc_info() else: if isgenerator(value): self.task_factory(value, event_loop=self) if exc_info: Failure(exc_info).log( msg='Unhadled exception in event loop callback.')
def _call(self): simple = lambda info, *args: info testMethod = self.testMethod testStartUp = getattr(self.test, 'startUp', lambda: None) testGetTime = getattr(self.test, 'getTime', lambda dt: dt) testGetInfo = getattr(self.test, 'getInfo', simple) testGetSummary = getattr(self.test, 'getSummary', simple) t = 0 t2 = 0 info = {} for r in range(self.number): testStartUp() start = default_timer() yield testMethod() delta = default_timer() - start dt = testGetTime(delta) testGetInfo(info, delta, dt) t += dt t2 += dt*dt self.updateSummary(info, self.number, t, t2) self.set_test_attribute('bench_info', testGetSummary(info, self.number, t, t2))
def _call(self): simple = lambda info, *args: info testMethod = self.testMethod testStartUp = getattr(self.test, 'startUp', lambda: None) testGetTime = getattr(self.test, 'getTime', lambda dt: dt) testGetInfo = getattr(self.test, 'getInfo', simple) testGetSummary = getattr(self.test, 'getSummary', simple) t = 0 t2 = 0 info = {'name': '%s.%s' % (self.test.__class__.__name__, testMethod.__name__)} for r in range(self.number): testStartUp() start = default_timer() testMethod() delta = default_timer() - start dt = testGetTime(delta) testGetInfo(info, delta, dt) t += dt t2 += dt*dt self.updateSummary(info, self.number, t, t2) self.set_test_attribute('bench_info', testGetSummary(info, self.number, t, t2))
def timeit(self, times, *args, **kwargs): '''Send ``times`` requests asynchronously and evaluate the time taken to obtain all responses. In the standard implementation this method will open ``times`` :class:`Connection` with the remote server. Usage:: client = Client(...) multi = client.timeit(100, ...) response = yield multi multi.total_time :return: a :class:`MultiDeferred` which results in the list of results for the individual requests. Its :attr:`MultiDeferred.total_time` attribute indicates the number of seconds taken (once the deferred has been called back). ''' results = [] for _ in range(times): r = self.request(*args, **kwargs) if hasattr(r, 'on_finished'): r = r.on_finished results.append(r) return multi_async(results)
def getsize(self, request): size = request.urlargs["size"] data = {"size": size, "data": "".join(("d" for n in range(size)))} return self.info_data_response(request, **data)
def on_message(self, websocket, msg): websocket.write(json.dumps([(i, random()) for i in range(100)]))
def getsize(self, request): size = request.urlargs['size'] data = {'size': size, 'data': ''.join(('d' for n in range(size)))} return self.info_data_response(request, **data)
def test_large(self): '''Echo a 3MB message''' msg = b''.join((b'a' for x in range(2**13))) result = yield self.client(msg) self.assertEqual(result, msg)
def test_getbench(self): c = self.client yield MultiDeferred((c.get(self.uri) for _ in range(1))).lock()
def __call__(self, func, *args, **kwargs): self.start = self._loop.time() data = (func(*args, **kwargs) for t in range(self.times)) self.result = multi_async(data, loop=self._loop) return chain_future(self.result, callback=self._done)
def test_large(self): """Echo a 3MB message""" msg = b"".join((b"a" for x in range(2 ** 13))) result = yield self.client(msg) self.assertEqual(result, msg)
def random_string(characters=None, len=20): characters or default_characters return ''.join((choice(characters) for s in range(len)))