Measuring Time Spent in Groovy Class Methods

I am writing some kind of profiler for my Groovy application. For this I am interested in how much processing time was spent on various methods of the class.

Now, it would be easy to measure the nanoseconds spent on each of these methods using the start and end times of the method call. However, this seems awkward, I don't want to waste time "outside" the methods (for example, before and after the call). I rather want to measure the time within the class itself, but also not "manually" by taking the start and end times, but rather "automatically" if possible.

So my question is, what would be the best and most Groovy way to measure the time spent on various methods of a class? Maybe through annotations?

+3


source to share


1 answer


Groovy comes with BenchmarkInterceptor :



An interceptor that registers the timestamp of each method call before and after the call. Timestamps are stored internally and can be retrieved via

getCalls()

      

and

statistic()

      

API.

Usage example:

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()

      

Which produces the following output:

[[size, 4, 0], [set, 4000, 21]]

      

+9


source







All Articles