Writing a java annotation for timing method call

2020-02-18 20:46发布

I want to write a java annotation which times the method call. something like this:

@TimeIt
public int someMethod() { ... }

and when this method is invoked, it should output on console how long this method took

I know how to do it in python, this is what I want it to do:

from time import time, sleep

def time_it(func):
    def wrapper(*args, **kwargs):
        start = time()
        func(*args, **kwargs)
        stop = time()
        print "The function", func.__name__, " took %.3f" % (stop - start)
    wrapper.__name__ = func.__name__
    return wrapper

@time_it
def print_something(*args, **kwargs):
    print "before sleeping"
    print args, kwargs
    sleep(3) # wait 3 seconds
    print "after sleeping"

print_something(1, 2, 3, a="what is this?")

So my questions are? Where do I find some documentation to write something like this, I tried apt documentation, had no luck with it. can someone help with writing something like this?

9条回答
贪生不怕死
2楼-- · 2020-02-18 21:28

I am surprised to see that no one pointed out java.lang.reflect.Proxy. Its an old thread, but I think this information would be helpful to someone.

Proxy has an interesting property which gives

  1. proxy instanceof Foo as true.
  2. You can have a method in your invocation handler, which prints the time first and then fires the actual method from the object.

You can have this proxy for all objects by making them implement some interface or you can use Comparable.

Look for section Dynamic proxies as decorator.

http://www.ibm.com/developerworks/library/j-jtp08305/

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-18 21:29

As of 2016, there's a nifty aspect annotation library jcabi-aspects.

From the docs:

Annotate your methods with @Loggable annotation and every time they are called, your SLF4J logging facility will receive a message with the details of execution and the total execution time:

public class Resource {
  @Loggable(Loggable.DEBUG)
  public String load(URL url) {
    return url.openConnection().getContent();
  }
}

Something like this will appear in the log:

[DEBUG] #load('http://www.google.com'): returned "<html ..." in 23ms

Read more about @Loggable here.

查看更多
叼着烟拽天下
4楼-- · 2020-02-18 21:29

Check out the Coda Hale Metrics library. It provides a @Timed annotation for methods that provides this capability. While you're at it check out Code Hale Dropwizard which has examples for how its been integrated into their service framework.

@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
    return new Saying(counter.incrementAndGet(),
                      String.format(template, name.or(defaultName)));
}
查看更多
干净又极端
5楼-- · 2020-02-18 21:37

AFAIK, Tomasz is right in saying that this can't be done using annotations. I think the confusion stems from the fact that Python decorators and Java annotations share the same syntax but are completely different in terms of the behavior they offer!

Annotations are metadata attached to your class/methods/fields. This blog post addresses the point of timing methods using AOP. Though it uses Spring, the basic premise remains the same. If you are good to go with an AOP compiler, it shouldn't be too difficult to translate the code. Another reference (spring specific) here.

EDIT: If your aim is to have a overall method timing for your application without using full blown profilers, you can use hprof for collecting total execution statistics.

查看更多
forever°为你锁心
6楼-- · 2020-02-18 21:38

Simply put: you can't!

Annotations are not pieces of code that get automatically started together with your code, they are just annotation, pieces of information that can be used by other programs working on your code like loading or running it.

What you need is AOP: aspect oriented programming.

查看更多
淡お忘
7楼-- · 2020-02-18 21:39

As already stated you can't and AOP or hprof should cover most of your needs, but if you insist there's a workaround using JSR269. FYI, apt is obsolete and an annotation processing API and tool has been incorporated into 1.6 (and it is called with the evocative name JSR269).

The workaround would be to create an annotation processor that generates a class that extends the class that contains the method with the @TimeIt annotation. This generated class must override the timed method, it will look like the Python time_it but the line func(*args, **kwargs) would be replaced by super.methodName(arg1, arg2, ...).

There are however two caveats:

  1. Elsewhere in your code you must be sure that you create instances of the generated class instead of the original class. That is a problem because you reference a class that does not exist yet: it will be created at the end of the first processing round.
  2. You will need to get familiar with the javax.annotation.processing and javax.lang.model packages, they are a bit awkward IMHO.
查看更多
登录 后发表回答