您将如何分解并记录应用程序不同部分的执行时间?

发布于 2024-07-25 07:46:35 字数 497 浏览 1 评论 0原文

我们构建了一个 Web 应用程序,它接受 SOAP 消息、进行一些处理、调用另一个 Web 服务、处理响应并将其发送回原始调用者。

我们希望在一行中记录这些不同步骤的不同执行时间。 我们将 log4j 与 JBossWS 堆栈结合使用。 换句话说,我们希望日志输出看起来像这样:

2009-06-10T16:19:31.487 3336/2449/861

其中 3336ms 是服务请求所花费的总时间,2449ms 是等待 Web 服务响应所花费的时间,861ms 是内部处理数据所花费的时间。

这些不同的计算发生在代码中的不同位置,我们不能只对每个计算进行计时并在单个方法结束时调用记录器。 我们中的一个人建议使用 log4j 的 MDC 作为一组穷人的全局变量来跟踪不同的执行时间。

所以我的问题如下:

  1. 这是否是对民主变革运动主要意图的可怕滥用?
  2. 如果是,否则你会怎么做?

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:

  1. Is this a horrible abuse of the MDC's primary intent?
  2. If yes, how would you do this otherwise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吻风 2024-08-01 07:46:35

请查看 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.

万水千山粽是情ミ 2024-08-01 07:46:35

听起来像是 Perf4J 的工作。

Sounds like a job for Perf4J.

半世晨晓 2024-08-01 07:46:35

我会执行以下操作:

  1. 创建一个 PerformanceTimer 类,该类公开:
    • recordWaitedForWebServerReponse(操作Id,时间);
    • recordMassgedData(操作Id, 时间);
    • 其他方法(如果需要)
  2. 在需要的地方注入该类;
  3. 让 PerformanceTimer 维护一个 OperationId 的并发映射 -> 操作计时数据;
  4. 在已知的最后一个方法调用上,使用所需的输出调用 Log4j。

I'd do the following:

  1. create a PerformanceTimer class, which exposes:
    • recordWaitedForWebServerReponse(operationId, time);
    • recordMassagedData(operationId, time);
    • other methods, if needed
  2. inject that class where it's needed;
  3. have the PerformanceTimer maintain a concurrent map of OperationId -> OperationTimingData;
  4. on the method call known to be the last, invoke Log4j with the output you desired.
你好,陌生人 2024-08-01 07:46:35

虽然这肯定会起作用,但我也对这个想法感到畏缩。 我的建议是为此目的创建一个计时类,它使用 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.

沩ん囻菔务 2024-08-01 07:46:35

我会使用 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.

灼疼热情 2024-08-01 07:46:35

一种简单的方法是使用 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.

我也只是我 2024-08-01 07:46:35

我们只是在第一个请求过滤器中使用它:

logger.trace("Init");
long start = System.currentTimeMillis();

//do stuff here 

long stop = System.currentTimeMillis();
String time = Util.getTimeDifferenceInSec(start, stop);
logger.trace("Complete in " + time + " sec");

We just use this in our first request filter:

logger.trace("Init");
long start = System.currentTimeMillis();

//do stuff here 

long stop = System.currentTimeMillis();
String time = Util.getTimeDifferenceInSec(start, stop);
logger.trace("Complete in " + time + " sec");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文