您将如何分解并记录应用程序不同部分的执行时间?
我们构建了一个 Web 应用程序,它接受 SOAP 消息、进行一些处理、调用另一个 Web 服务、处理响应并将其发送回原始调用者。
我们希望在一行中记录这些不同步骤的不同执行时间。 我们将 log4j 与 JBossWS 堆栈结合使用。 换句话说,我们希望日志输出看起来像这样:
2009-06-10T16:19:31.487 3336/2449/861
其中 3336ms 是服务请求所花费的总时间,2449ms 是等待 Web 服务响应所花费的时间,861ms 是内部处理数据所花费的时间。
这些不同的计算发生在代码中的不同位置,我们不能只对每个计算进行计时并在单个方法结束时调用记录器。 我们中的一个人建议使用 log4j 的 MDC 作为一组穷人的全局变量来跟踪不同的执行时间。
所以我的问题如下:
- 这是否是对民主变革运动主要意图的可怕滥用?
- 如果是,否则你会怎么做?
We have built a web application that accepts SOAP messages, does some processing, calls out to another web service, massages the response and sendd it back to the original caller.
We'd like to log, in a single line, the different execution times for these different steps. We use log4j with the JBossWS stack. In other words, we'd like the log output to look something like:
2009-06-10T16:19:31.487 3336/2449/861
where 3336ms is the total time spent serving the request, 2449ms is the time spent waiting for the web service's response, and 861ms is the time spent internally massaging the data.
These different computations happen in different places in our code and we cannot just time each one of them and make a call to the logger at the end of a single method. One of us has suggested using log4j's MDC for this as a poor man's set of global variables to track the different executions times.
So my questions are the following:
- Is this a horrible abuse of the MDC's primary intent?
- If yes, how would you do this otherwise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请查看 SLF4J 分析器。 够有趣的。 tt 的开发是为了满足完全相同的需求,即测量和改进 SOAP 调用的性能。
Please have a look at SLF4J profiler. Interestingly enough. tt was developed to answer the exact same need, that is to measure and improve the performance of SOAP calls.
听起来像是 Perf4J 的工作。
Sounds like a job for Perf4J.
我会执行以下操作:
I'd do the following:
虽然这肯定会起作用,但我也对这个想法感到畏缩。 我的建议是为此目的创建一个计时类,它使用 ThreadLocal 为每个请求创建一个变量。
当请求到来时,您创建变量(带有开始时间的
long
)。 当变量已经存在时,您无需管它。 当结果发送回客户端时,您可以计算持续时间并记录下来。While it would certainly work, I cringe at the thought, too. My suggestion would be to create a timing class just for that purpose which uses
ThreadLocal
to create a variable per request.When the request comes in, you create the variable (a
long
with the start time). When the variable already exists, you leave it alone. When the result gets sent back to the client, you calculate the duration and log that.我会使用 Spring 和面向方面的编程来做到这一点。 好处是我只需编写一次逻辑,然后在需要的地方以声明方式应用它。
I'd use Spring and aspect-oriented programming to do it. The nice thing is that I'd write the logic once and apply it declaratively everywhere I needed it.
一种简单的方法是使用 System.currentTimeMillis() ,它返回以毫秒为单位的时间。
方法开始时的毫秒数与方法结束时的毫秒数之间的差异将给出执行该特定方法所需的毫秒数。
希望
System.currentTimeMillis()
能够帮助您。One Simple way is to use
System.currentTimeMillis()
which returns time in milli seconds.The difference between the millisec at the start of the method and the milli seconds at the end of the method will give the milli sec's taken for executing that particular method.
Hope
System.currentTimeMillis()
will help you.我们只是在第一个请求过滤器中使用它:
We just use this in our first request filter: