访问性能良好的堆栈跟踪? [英] Access stacktraces with good performance?

查看:51
本文介绍了访问性能良好的堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近应用了几乎所有应用程序模块/组件(大约 50 个项目)都使用的缓存解决方案.为了更好地了解在不同系统位置"上执行了哪些缓存操作,我们为当前执行的缓存操作添加了日志记录,包括堆栈跟踪,以准确了解触发缓存操作的原因.

We recently applied a caching solution that is used by almost all application modules/components (approx. 50 projects). To get a better understanding which cache operations are executed on the different system "places", we added logging for the currently performed cache operations including a stack trace to exactly know what triggered the cache operation.

我们目前的方法是这样的:我们从 new Throwable() 中获取堆栈跟踪,过滤不相关的行并记录剩余的堆栈跟踪.然而,创建一个新的异常来遗憾地记录并不是廉价的操作.由于我们不直接使用缓存而是通过休眠,因此在不访问堆栈跟踪的情况下找出哪个调用者触发了操作并不容易.

Our current approach looks like this: We take the stack trace from new Throwable(), filter the irrelevant lines and log the remaining stack trace. Creating a new exception to log regretfully is no cheap operation though. Since we do not use the cache directly but through hibernate it's not so easy to find out which caller triggered the operation without having access to the stack trace.

因此,我的问题是:是否有比 Throwable().getStackTrace 或 Thread.currentThread().getStackTrace() 更高效的访问当前堆栈跟踪的解决方案?

My question thus is: Is there a more performant solution to access the current stacktrace then Throwable().getStackTrace or Thread.currentThread().getStackTrace()?

推荐答案

Thread.currentThread().getStackTrace() 与@apangin 指出的基本相同,但在如果它避免创建 Throwable.

Thread.currentThread().getStackTrace() is basically the same thing as @apangin points out, but it could be faster in future if it avoided creating a Throwable.

但是,您可能会发现子采样会给您带来所需的改进.不是记录每一次访问,而是记录每 N 次访问.如果您每 10 次记录一次访问,您可以将开销减少多达 90%,如果您有大量访问,它几乎同样准确.

However, you might find sub-sampling will give you the improvement you need. Instead of recording every access, record every N-th access. If you record every 10th access you can reduce the overhead by up to 90% and if you have a high number of accesses it will be almost as accurate.

另一种选择是使用像 YourKit 这样的分析器,它可以更有效地做到这一点.这可以向您显示一个方法的不同调用者及其堆栈跟踪(通常每 10 次记录一次)

Another option is to use a profiler like YourKit which can do this much more efficiently. This can show you how much different callers to a method and their stack traces (typically it records every 10th)

这篇关于访问性能良好的堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆