简体   繁体   English

是否可以使用微基准测试框架来计算某些语句?

[英]Is it possible to use a micro-benchmark framework to only time some statements?

I am planning to micro benchmark my java code which involves several calls to local as well as remote database. 我计划对我的java代码进行微基准测试,该代码涉及对本地和远程数据库的多次调用。 I was about to use System.nanoTime() but started reading about the micro benchmarking frameworks such as jmh and caliper. 我即将使用System.nanoTime(),但开始阅读微基准测试框架,如jmh和caliper。 Use of these frameworks is definitely recommended but from whatever (little) I read, it seems that we can benchmark only a complete method and also it allows us to do this non-invasively (wrt existing code) ie, we need not litter existing code with the code/annotations of jmh/caliper. 绝对建议使用这些框架,但无论从哪个(很少)我读过,似乎我们只能对一个完整的方法进行基准测试,并且它允许我们非侵入性地执行此操作(现有代码),即我们不需要丢弃现有代码使用jmh / caliper的代码/注释。

I want to benchmark only specific pieces of code (statements) within some methods. 我想在一些方法中仅对特定的代码片段(语句)进行基准测试。 Is it possible to do this with any of micro benchmarking frameworks? 是否有可能使用任何微基准框架来做到这一点? Please provide some insights into this. 请提供一些见解。

I guess, calls to a DB are usually expensive enough to eliminate most of the problem with microbenchmarking. 我想,调用数据库通常很昂贵,足以消除微基准测试的大部分问题。 So your approach was probably fine. 所以你的方法可能很好。 If you're measuring it in production, repeating the measurement many times, and don't care about a few nanoseconds, stick with System.nanoTime . 如果你在生产中测量它,多次重复测量,并且不关心几纳秒,坚持使用System.nanoTime

You're doing something very different from microbenchmarking like eg I did here . 你正在做一些与microbenchmarking截然不同的事情,比如我在这里做的事情。 You're not trying to optimize a tiny piece of code and you don't want to eliminate external influences. 您不是要尝试优化一小段代码,而是不想消除外部影响。

Microbenchmarking a part of a method makes no sense to me, as a method gets optimized as a whole (and possibly also inlined). 微观标记方法的一部分对我来说没有意义,因为一个方法作为一个整体被优化(并且可能也被内联)。 It's a different level. 这是一个不同的水平。

I don't think any framework could help, all they can do in your case is automate the work, which you don't seem to need. 我不认为任何框架可以提供帮助,他们在您的情况下可以做的就是自动完成您似乎不需要的工作。 Note that System.nanoTime may take several hundreds cycles (which is probably fine in your case). 请注意, System.nanoTime可能需要几百个周期(在您的情况下可能很好)。

You can try using metrics from codehale . 您可以尝试使用codehale中的指标 I found its easy to use and low overhead if you are using in certain configuration ie Exponentially decaying Reservoir. 如果您在某些配置中使用,即指数衰减的储层,我发现它易于使用且开销低。

Micro level and precise benchmarking does comes with an associated cost with it ie memory overhead at run time for sampling, benchmark might it self take time for calculation and and stats generation (ideal one would be offsetting that from stats) . 微观级别和精确基准测试确实伴随着相关成本,即在运行时采样的内存开销,基准可能需要时间进行计算和统计数据生成(理想的一个将抵消统计数据)。 But if you want to bench mark db connection which I don't think should be very frequent, metrics might be appropriate, I found its easy to use. 但是如果你想基准数据库连接我认为不应该非常频繁,指标可能是合适的,我发现它很容易使用。 and yes it is bit invasive but configurable. 是的它有点侵入性但可配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM