如何在Java中介绍阻止代码–测量执行时间而不是CPU时间
我正在尝试介绍通常会在数据库中阻止和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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
异步分析师是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.
您需要一个理解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".
Disclaimer: My company develops JProfiler