测量Groovy类方法所花费的时间

Mat*_*ias 3 profiler groovy annotations

我正在为基于Groovy的应用程序编写某种分析器.为此,我感兴趣的是在各种类方法中花费了多少处理时间.

现在,通过获取方法调用的开始和结束时间,可以简单地测量每种方法中花费的纳秒数.然而,这感觉很笨,我不想花时间"外"的方法(例如在通话之前和之后).我宁愿在班级内部测量时间,也不想通过开始和结束时间"手动",而是"自动",如果可能的话.

所以我的问题是:测量在类的各种方法中花费的时间最好,最Groovy的方法是什么?也许通过注释?

cfr*_*ick 9

Groovy附带BenchmarkInterceptor:

拦截器,用于在调用之前和之后注册每个方法调用的时间戳.时间戳存储在内部,可以通过使用

getCalls()
Run Code Online (Sandbox Code Playgroud)

statistic()
Run Code Online (Sandbox Code Playgroud)

API.

用法示例:

def proxy = ProxyMetaClass.getInstance(ArrayList.class)
proxy.interceptor = new BenchmarkInterceptor()
proxy.use {
    def list = (0..10000).collect{ it }
    4.times { list.size() }
    4000.times { list.set(it, it+1) }
}
proxy.interceptor.statistic()
Run Code Online (Sandbox Code Playgroud)

其中产生以下输出:

[[size, 4, 0], [set, 4000, 21]]
Run Code Online (Sandbox Code Playgroud)