async def test_influxdb(influxdb): rep = InfluxReporter(port=influxdb.port, tags={"mark": "X"}, autocreate_database=True) with timer("test").time(): pass with timer("test2").time(): pass cpu_micro = CPUMicroBenchMark() gauge("CPU", cpu_micro) await rep.report_now() assert influxdb.querycount == 1 assert influxdb.writecount == 1 # cpu micro has none-empty cache assert cpu_micro.last_value is not None if influxdb.failure: raise influxdb.failure for line in influxdb.lines: assert "mark=X" in line
async def post(self): #timer = metricsRegistry.timer("post_calls") #histogram = metricsRegistry.histogram("pos_calls"); #histogram.add(timer) msg = self.get_argument('message') channel = self.application.rabbitmq '''self.application.logger.msg("before publish to channel", msg=msg) await channel.basic_publish( payload=msg, exchange_name='', routing_key='msg_queue' )''' self.application.logger.msg("Get input msg to post", msg=msg) pool = self.application.db async with pool.acquire() as conn: with timer("test").time(): async with conn.cursor() as cur: await cur.execute("INSERT INTO msg_tb4 (msg) VALUES(%s)", (msg)) await cur.execute("SELECT LAST_INSERT_ID()") id_row = await cur.fetchone() id = id_row[0] await conn.commit() # commit might come after msg_cnt self.application.logger.msg("Execution of insertion to db completed after commit", id =id, msg=msg) self.application.logger.msg(str(timer("test").get_mean())) self.application.logger.msg("before publish to channel", id=id, msg=msg) await channel.basic_publish( payload=str(id) + '#' + msg, exchange_name='', routing_key='msg_queue' ) msg_cnt = await cur.execute("SELECT * from msg_tb4") self.application.logger.msg("Msg count:", msg_cnt = msg_cnt) res = [None] * msg_cnt for i in range(msg_cnt): tmp = await self.application.redis.get(i+1) if tmp is not None: res[i] = tmp.decode('utf-8') self.application.logger.msg("Read message from redis", msg_id = i+1, msg = res[i]) else: await cur.execute("SELECT * from msg_tb4 WHERE msg_id = %s", i+1) tmp = await cur.fetchone() res[i] = tmp[1] await self.application.redis.set(i+1, str(res[i])) self.application.logger.msg("Read message from mysql", msg_id = i+1, msg = res[i]) for i in range(msg_cnt): self.write(str(i+1) + "." + res[i] + '\n')
def test_time_calls(self): @time_calls def timed_func(): pass timed_func() func_timer = timer("timed_func_calls") self.assertEqual(func_timer.get_count(), 1) self.assertTrue(func_timer.get_mean())
def test_time_calls(self): @time_calls def timed_func(): pass timed_func() func_timer = timer( "RegistryTestCase.test_time_calls.<locals>.timed_func_calls") self.assertEqual(func_timer.get_count(), 1) self.assertTrue(func_timer.get_mean())
async def _call(self, kwargs: Dict[str, str], http_method: str, call_config: common.UrlMethod) -> None: """ An rpc like call """ if call_config is None: raise exceptions.NotFound("This method does not exist") if not self._transport.running: return with timer("rpc." + call_config.method_name).time(): self._transport.start_request() try: message = self._transport._decode(self.request.body) LOGGER.debug( f"HTTP version of request: {self.request.version}") if message is None: message = {} for key, value in self.request.query_arguments.items(): if len(value) == 1: message[key] = value[0].decode("latin-1") else: message[key] = [v.decode("latin-1") for v in value] request_headers = self.request.headers auth_token = self.get_auth_token(request_headers) auth_enabled = server_enable_auth.get() if not auth_enabled or auth_token is not None: result = await self._transport._execute_call( kwargs, http_method, call_config, message, request_headers, auth_token) self.respond(result.body, result.headers, result.status_code) else: raise exceptions.UnauthorizedException( "Access to this resource is unauthorized.") except JSONDecodeError as e: error_message = f"The request body couldn't be decoded as a JSON: {e}" LOGGER.info(error_message, exc_info=True) self.respond({"message": error_message}, {}, 400) except ValueError: LOGGER.exception("An exception occured") self.respond({"message": "Unable to decode request body"}, {}, 400) except exceptions.BaseHttpException as e: self.respond(e.to_body(), {}, e.to_status()) except CancelledError: self.respond({"message": "Request is cancelled on the server"}, {}, 500) finally: try: await self.finish() except iostream.StreamClosedError: # The connection has been closed already. pass except Exception: LOGGER.exception("An exception occurred responding to %s", self.request.remote_ip) self._transport.end_request()