Java虚拟机上的误导性微基准测试
Misleading Microbenchmarks on the Java Virtual Machines
AI总结:
本文揭示了在Java虚拟机上使用微基准测试时,由于不现实的性能剖析导致误导性优化结果的问题,并提出了改进指南。
摘要:
开发者经常使用微基准测试来选择方法或类的最优实现。在Java虚拟机(JVM)上,通常使用Java微基准测试工具(JMH)来规避测量代码性能的常见陷阱。然而,即使遵循JMH指南也无法克服上下文这一根本问题。微基准测试本质上是在隔离环境中执行代码,不受其他应用程序代码竞争CPU资源(如缓存或分支预测器容量)的干扰。在具有分层动态编译的托管运行时(如JVM)上,编译决策的推测性和基于性能剖析的特性意味着代码性能高度依赖于早期执行期间收集的性能剖析数据。由于性能剖析通常还包括分支概率和接收者类型(除了代码热度的度量),设计不当的微基准测试可能导致JVM收集不现实的性能剖析数据,从而产生激进但具有误导性的优化,而这些优化在实际应用中不会发生。在本文中,我们展示了在诱导JVM收集不现实性能剖析数据的条件下使用微基准测试,尽管遵循现有指南,仍会产生误导性结果。我们还通过建议使微基准测试结果更具代表性的措施来扩展这些指南。
英文摘要:
Developers often use microbenchmarks to choose the most performant implementation of a method or a class. On the Java Virtual Machine (JVM), this is commonly done using the Java Microbenchmark Harness (JMH) which addresses common pitfalls of measuring code performance on the JVM. However, even using JMH guidelines cannot overcome the fundamental issue of context. Microbenchmarks inherently execute code in isolation, without interference from other application code competing for CPU resources, such as cache or branch-predictor capacity. On managed runtimes with tiered dynamic compilation, such as the JVM, the speculative, profile-driven nature of compilation decisions means that code performance is highly dependent on profiles collected during early execution. Because profiles usually include also branch probabilities and receiver types (besides code hotness metrics), a badly designed microbenchmark may cause the JVM to collect an unrealistic profile, resulting in aggressive, yet misleading, optimizations, that would not occur in a real application. In this paper, we demonstrate how using microbenchmarks under conditions that induce the JVM to collect unrealistic profiles yields misleading results despite following existing guidelines. We also extend these guidelines by suggesting actions to make the microbenchmark results more representative.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。