版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wild46cat/article/details/82432253
1、引入slf4j-ext
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-ext</artifactId>
<version>1.7.25</version>
</dependency>
2、在程序中埋点
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
Profiler profiler = new Profiler("Timing profiler");
profiler.start("handle request");
LOG.info(fullHttpRequest.getUri());
LOG.info(fullHttpRequest.getMethod().toString());
profiler.start("handle response");
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set("content-Type", "text/html;charset=UTF-8");
StringBuilder sb = new StringBuilder();
sb.append("test");
ByteBuf responseBuf = Unpooled.copiedBuffer(sb, CharsetUtil.UTF_8);
response.content().writeBytes(responseBuf);
responseBuf.release();
profiler.start("write response");
channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
profiler.stop();
int spentMs = (int) profiler.elapsedTime() / 1000000;
LOG.warn(profiler.toString());
LOG.warn("Total Time is {}ms", spentMs);
}
3、查看打印出来的时间信息
22:05:35.921 [nioEventLoopGroup-3-2] WARN com.xueyou.demo.server.NettyHttpServerHandler - + Profiler [Timing profiler]
|-- elapsed time [handle request] 0.223 milliseconds.
|-- elapsed time [handle response] 1.492 milliseconds.
|-- elapsed time [write response] 36.974 milliseconds.
|-- Total [Timing profiler] 38.952 milliseconds.