我有一个用例,我通过在循环中调用一系列方法来测试工作流。现在,为了理解我的系统的性能,我试图计算一个方法完成执行所花费的时间。
以下是我的代码外观的示例:
public class Test {
public static void main(String[] args) {
while (true) {
String studentId = createStudent();
Thread.sleep(1000);
Runnable calls[] = new Runnable {
() -> getStudent(studentId),
() -> updateStudent(studentId),
() -> delete(studentId)
};
for (Runnable call: calls) {
call.run();
}
}
}
private String createStudent() {
final long startTime = System.nanoTime();
...
...
...
final long endTime = System.nanoTime();
final long actualRuntime = TimeUnit.MILLISECONDS.convert( endTime-startTime, TimeUnit.NANOSECONDS );
System.out.println("createStudent :" + actualRuntime);
return studentId;
}
private void getStudent(String studentId) {
final long startTime = System.nanoTime();
...
...
...
final long endTime = System.nanoTime();
final long actualRuntime = TimeUnit.MILLISECONDS.convert( endTime-startTime, TimeUnit.NANOSECONDS );
System.out.println("getStudent: " + actualRuntime);
}
private void updateStudent(String studentId) {
final long startTime = System.nanoTime();
...
...
...
final long endTime = System.nanoTime();
final long actualRuntime = TimeUnit.MILLISECONDS.convert( endTime-startTime, TimeUnit.NANOSECONDS );
System.out.println("updatesStudent: " + actualRuntime);
}
private void deleteStudent(String studentId) {
final long startTime = System.nanoTime();
...
...
...
final long endTime = System.nanoTime();
final long actualRuntime = TimeUnit.MILLISECONDS.convert( endTime-startTime, TimeUnit.NANOSECONDS );
System.out.println("deleteStudent : " + actualRuntime);
}
}
现在我不想在任何地方定义相同的代码片段,而是想知道是否有可以引入的某种拦截器来计算运行时间?
同样在拦截器可行的情况下,我猜测还需要一种解决方法,以便识别被调用的方法类型/计算运行时的方法类型。