class App(object): def __init__(self, flask_app): super(App, self).__init__() self.flask_app = flask_app self.loopback = FlaskLoopback(self.flask_app) self.hostname = str(uuid1()) self.url = URLObject("http://{0}".format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def get_page(self, page_size, page, path=None): if path is None: path = "objects" response = requests.get(self.url.add_path(path).set_query_param("page", str(page)).set_query_param("page_size", str(page_size))) response.raise_for_status() data = response.json() assert data["metadata"]["page"] == page assert data["metadata"]["page_size"] == page_size return data["result"] def get_all_paged(self, page_size, path=None): return list(itertools.chain.from_iterable(self.get_page(page_size, page, path=path) for page in range(1, int(self.num_objects / page_size) + 5)))
class Webapp(object): def __init__(self, app): super(Webapp, self).__init__() self.app = app self.loopback = FlaskLoopback(self.app) self.hostname = str(uuid4()) self.url = URL('http://{0}'.format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def _request(self, method, path, *args, **kwargs): raw_response = kwargs.pop("raw_response", False) if path.startswith("/"): path = path[1:] assert not path.startswith("/") returned = requests.request( method, "http://{0}/{1}".format(self.hostname, path), *args, **kwargs) if raw_response: return returned returned.raise_for_status() return returned.json()
def main(verbose, quiet): from _benchmark_queries import queries with logbook.NullHandler(), logbook.StreamHandler(sys.stderr, level=logbook.CRITICAL-verbose+quiet, bubble=False): loopback = FlaskLoopback(create_app()) loopback.activate_address((_root_address, 80)) num_attempts = 5 for obj, query in queries: times = [] has_error = False print(obj, '|', click.style(query, fg='cyan'), '--') for i in range(num_attempts): start_time = time.time() resp = requests.get(_root_url.add_path('/rest').add_path(obj).add_query_param('search', query).add_query_param('page_size', 25)) end_time = time.time() if resp.status_code == requests.codes.internal_server_error: print('\t', click.style('Timeout', fg='red')) has_error=True break else: resp.raise_for_status() times.append(end_time - start_time) if not has_error: print('\t', len(resp.json()[obj]), 'results --', '(Out of {}) Best: {:.03}s Avg: {:.03}s Worst: {:.03}s'.format(num_attempts, min(times), sum(times) / len(times), max(times)))
class App(object): def __init__(self, flask_app): super(App, self).__init__() self.flask_app = flask_app self.loopback = FlaskLoopback(self.flask_app) self.hostname = str(uuid1()) self.url = URLObject("http://{0}".format(self.hostname)) def activate(self): self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def get_page(self, page_size, page, path=None): if path is None: path = "objects" response = requests.get( self.url.add_path(path).set_query_param("page", str(page)).set_query_param( "page_size", str(page_size))) response.raise_for_status() data = response.json() assert data["metadata"]["page"] == page assert data["metadata"]["page_size"] == page_size return data["result"] def get_all_paged(self, page_size, path=None): return list( itertools.chain.from_iterable( self.get_page(page_size, page, path=path) for page in range(1, int(self.num_objects / page_size) + 5)))
def mailboxer_url(request, db_engine): loopback = FlaskLoopback(create_app()) hostname = str(uuid.uuid1()) loopback.activate_address((hostname, 80)) @request.addfinalizer def close(): loopback.deactivate_address((hostname, 80)) return URL("http://{0}".format(hostname))
def url(request, flask_app): address = str(uuid1()) returned = URL('http://{0}'.format(address)) webapp = FlaskLoopback(flask_app) webapp.activate_address((address, 80)) @request.addfinalizer def finalize(): # pylint: disable=unused-variable webapp.deactivate_address((address, 80)) return returned
class Webapp(object): def __init__(self): super(Webapp, self).__init__() self.hostname = str(uuid.uuid1()) def __enter__(self): self.activate() return self def __exit__(self, *args): self.deactivate() def activate(self): self.app = build_app(use_cached=True) self.app.config["SECRET_KEY"] = "testing_key" self.app.config["TESTING"] = True self.loopback = FlaskLoopback(self.app) self.loopback.activate_address((self.hostname, 80)) def deactivate(self): self.loopback.deactivate_address((self.hostname, 80)) def request(self, method, path, *args, **kwargs): """Performs a request against the server app All arguments are similar to requests' ``request``, except for a few exceptions listed below :keyword raw_response: if ``True``, returns the raw response object and avoids checking for success :keyword data_json: if provided, sends the value provided serialized to JSON, and sets the content type to be json accordingly """ if not isinstance(path, str): path = str(path) raw_response = kwargs.pop("raw_response", False) data_json = kwargs.pop('data_json', NOTHING) if data_json is not NOTHING: if 'data' in kwargs: raise RuntimeError('`data` is not supported when combined with `data_json`') kwargs['data'] = json.dumps(data_json) headers = kwargs['headers'] = dict(kwargs.get('headers', ())) if not any(k.lower() == 'content-type' for k in headers): headers['Content-type'] = 'application/json' if path.startswith("/"): path = path[1:] assert not path.startswith("/") returned = requests.request(method, f"http://{self.hostname}/{path}", *args, **kwargs) if raw_response: return returned returned.raise_for_status() return returned.json() def get(self, *args, **kwargs): """Shortcut for self.request('get', ...) """ return self.request("get", *args, **kwargs) def post(self, *args, **kwargs): """Shortcut for self.request('post', ...) """ return self.request("post", *args, **kwargs) def delete(self, *args, **kwargs): """Shortcut for self.request('delete', ...) """ return self.request("delete", *args, **kwargs) def put(self, *args, **kwargs): """Shortcut for self.request('put', ...) """ return self.request("put", *args, **kwargs)