测量生产环境中 Java Web 服务的方法执行时间

发布于 2024-10-07 01:13:57 字数 698 浏览 1 评论 0原文

我有兴趣找出测量我正在开发的 Java Web 服务中方法执行时间的最佳方法。

该服务将部署到多个客户端,因此在多个不同的生产环境中运行(客户端往往根据其需求而具有不同的设置),并且已决定该服务应记录处理请求的执行时间,以提供可能的一些指示性能问题。

到目前为止,大多数建议(例如此处 & < a href="https://stackoverflow.com/questions/302026/measuring-java-execution-time-memory-usage-and-cpu-load-for-a-code-segment">此处)我我见过在我感兴趣的代码的开头和结尾使用 System.currentTimeMillis() 并计算经过的时间,但这真的是生产环境中解决此问题的最佳解决方案吗?

System.currentTimeMillis() 测量方法执行时间有多合适,还有其​​他选择吗?

编辑 0: 为了澄清,一个关键要求该执行测量应作为标准部署的一部分进行部署,以收集数据,以便在出现与性能相关的支持问题时可以参考该数据,因为该服务构成了由新组件和旧组件组成的复杂系统的一部分

I'm interested in finding out the best way to measure the execution time of methods within a Java web service I'm working on.

The service will be deployed to multiple clients and hence run in multiple different production environments (clients tend to have varying setups as dictacted by their requirements), and its been decided the service should log the execution time for processing requests to provide some indication of possible performance issues.

So far, most of the suggestions (such as here & here) I've seen are to use System.currentTimeMillis() at the beginning and end of the code I'm interested in and calculate elapsed time, but is that really the best solution to this problem for a production environment?

How suitable is System.currentTimeMillis() for measuring method execution time, and are there any alternatives?

EDIT 0: To clarify, a key requirement is that this execution measurement should be deployed as part of the standard deployment to collect data so that it can be referred to should a performance related support issue be raised, as the service forms part of a complex system of new and legacy components

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

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

发布评论

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

评论(3

予囚 2024-10-14 01:13:57

您可以使用 Spring 框架“StopWatch”类:

您甚至可以使用 AOP,这样您就可以在不更改代码的情况下分析代码

You can use the Spring Framework 'StopWatch' class:

You can even use AOP, this way you can profile code without changing it

迷雾森÷林ヴ 2024-10-14 01:13:57

记录经过的时间很好,但是您需要在客户端对其进行计时才能真正了解它花费了多长时间。它还会告诉您在数据传输上花费了多少时间,这可能表明存在 I/O 瓶颈。

Logging the time elapsed is fine, but you need to time it on the client side to really understand how long it's taking. That will additionally tell you how much time you're spending in data transfer which could indicate an I/O bottleneck.

流心雨 2024-10-14 01:13:57

如果您的应用程序执行时间超过 1 或 2 毫秒,则 System.currentTimeMillis() 适合(尽管其精度可能大于毫秒,具体取决于您的操作系统)。

您可能更喜欢使用 System.nanoTime() ,它可以更精确,但不能达到纳秒。请注意,其精度还取决于您的底层系统。

这两种方法都是相当低级的(需要您管理时间戳),但比更高级的 API 具有更低的开销。

System.currentTimeMillis() is suitable if your application will take more than 1 or 2 milliseconds to execute (although its precisions might be greater than the ms depending on your OS).

You might prefer using System.nanoTime() which can be fare more precise, but not to the nanosecond. Please note its precision also depends on your underlying system.

Both these methods are quite low-level (need you to manage timestamps) but have a lower overhead than more high-level API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文