如何在Java中介绍阻止代码–测量执行时间而不是CPU时间

发布于 2025-02-07 02:56:38 字数 498 浏览 2 评论 0原文

我正在尝试介绍通常会在数据库中阻止和REST调用。该代码不是CPU绑定的。以下样本JUNIT方法应说明问题:

@RepeatedTest(10)
void fast() throws InterruptedException {
    Thread.sleep(100);
}

@RepeatedTest(10)
void slow() throws InterruptedException {
    // imagine a slow database or rest call or any other blocking code
    Thread.sleep(1000);
}

我正在使用Intellij和Java Flight Recorder。我希望方法快速报告使用约10%的执行时间和方法使用了约90%的执行时间。但是根本没有报道他们,因为他们不吃CPU时间。

我如何介绍仅在阻止代码而不是CPU时间的等待时间的实际执行时间?

I am trying to profile that is often blocking in either database and rest calls. The code is not cpu bound. The following sample junit methods should illustrate the issue:

@RepeatedTest(10)
void fast() throws InterruptedException {
    Thread.sleep(100);
}

@RepeatedTest(10)
void slow() throws InterruptedException {
    // imagine a slow database or rest call or any other blocking code
    Thread.sleep(1000);
}

I am using IntelliJ and Java Flight Recorder. I expect that method fast is reported as using about 10% of execution time and method slow is using about 90% of execution time. But they are not reported at all because they don't eat CPU time.

How can I profile the real execution time that includes waiting time in blocking code instead of CPU time only?

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

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

发布评论

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

评论(2

习惯成性 2025-02-14 02:56:38

异步分析师是JVM分析的出色,免费且非常轻巧的工具。在典型的设置中,它确实可以在CPU时间进行测量,但是它可以选择进行“ Wall-clock”配置文件: https://github.com/jvm-profiling-tools/async-profiler#wall-clock-profling

您可以尝试一下,看看是否能为您提供更好结果。

Async profiler is an excellent, free, and very lightweight tool for JVM profiling. In typical settings, it indeed measures on-CPU time but it has an option to do "Wall-clock" profiling: https://github.com/jvm-profiling-tools/async-profiler#wall-clock-profiling

You can experiment with that and see if that gives you better results.

羁〃客ぐ 2025-02-14 02:56:38

您需要一个理解JDBC和HTTP调用语义的剖面。例如,使用Jprofiler,分析代理捕获了当线程状态选择器设置为“所有状态”时包含的合成网I/O线程状态。

免责声明:我的公司开发Jprofiler

You need a profiler that understands the semantics of JDBC and HTTP calls. For example, with JProfiler, the profiling agent captures a synthetic Net I/O thread state that is included when the thread status selector is set to "All states".

enter image description here

Disclaimer: My company develops JProfiler

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