我想写一个java注解来计算方法调用的时间。如下所示:
@TimeIt
public int someMethod() { ... }
当调用此方法时,它应在控制台上输出此方法所用的时间
我知道如何在python中做到这一点,这就是我想让它做的:
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?")
所以我的问题是?我在哪里可以找到一些文档来编写这样的东西,我尝试了apt
文档,但没有成功。有人能帮我写出这样的东西吗?
发布于 2011-04-21 13:58:18
简单地说:你不能!
注释不是与代码一起自动启动的代码片段,它们只是注释,可以被其他处理代码的程序使用的信息片段,比如加载或运行代码。
您需要的是AOP:面向方面的编程。
发布于 2017-01-19 22:11:08
我对同样的事情想了好几次,最后写了下面的开头:
注释:
package main;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Clocking {
}
对象的接口:
package main;
public interface Examples {
@Clocking
void thisIsAMethod();
void thisIsAnotherMethod(String something);
@Clocking
void thisIsALongRunningMethod();
}
调用处理程序:
package main;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.Instant;
public class ExamplesInvocationHandler implements InvocationHandler {
// ******************************
// Fields
// ******************************
private Examples examples = new ExamplesImpl();
// ******************************
// Public methods
// ******************************
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// If the annotation is not present, just redirect the method call to its origin...
if(!method.isAnnotationPresent(Clocking.class)) {
return method.invoke(examples, args);
}
// ... otherwise log the execution time of it.
Instant start = Instant.now();
Object returnObj = method.invoke(examples, args);
Instant end = Instant.now();
// TODO: This is for demonstration purpose only and should use the application's logging system.
System.out.println("Method " + method.getName() + " executed in " + Duration.between(end, start) + ".");
return returnObj;
}
// ******************************
// Inner classes
// ******************************
private static class ExamplesImpl implements Examples {
@Override
public void thisIsAMethod() {
System.out.println("thisIsAMethod called!");
}
@Override
public void thisIsAnotherMethod(String something) {
System.out.println("thisIsAnotherMethod called!");
}
@Override
public void thisIsALongRunningMethod() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thisIsALongRunningMethod called!");
}
}
}
最后一个入口点来测试这个:
package main;
import java.lang.reflect.Proxy;
public class Main {
public static void main(String[] args) {
Examples examples = (Examples) Proxy.newProxyInstance(Examples.class.getClassLoader(), new Class[]{Examples.class}, new ExamplesInvocationHandler());
examples.thisIsAMethod();
examples.thisIsAnotherMethod("");
examples.thisIsALongRunningMethod();
}
}
这需要改进,因为它需要代理来实例化我们的对象,所以你不能真正将它用于“泛型已经编写”的代码。但它可能会让你找到更完整的东西。
发布于 2013-02-08 04:32:32
查看Coda Hale Metrics库。它为提供此功能的方法提供了@Timed注释。在此期间,请查看Code Hale Dropwizard,它提供了如何将其集成到其服务框架中的示例。
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
return new Saying(counter.incrementAndGet(),
String.format(template, name.or(defaultName)));
}
https://stackoverflow.com/questions/5740101
复制相似问题