我有一个无限轮询循环使用java.nio.file.WatchService寻找新的文件。在循环中我有固定的线程池执行器服务并发处理文件。
当轮询服务继续运行时,我如何对一批文件(例如10/n个文件)的处理时间进行基准测试?我能够在可运行类中的每个文件计时,但如何获得批处理时间?
应该这样做:
// inside the listener for the WatchService
final MyTimer t = new MyTimer(); // takes current time, initialized to 0 tasks
for (Change c : allChanges) {
t.incrementTaskCount(); // synchronized
launchConcurrentProcess(c, t);
}
// inside your processor, after processing a change
t.decrementTaskCount(); // also synchronized
// inside MyTimer
public void synchronized decrementTaskCount() {
totalTasks --;
// depending on your benchmarking needs, you can do different things here
// I am printing max time only (= end of last), but min/max/avg may also be nice
if (totalTasks == 0) {
System.err.println("The time spent on this batch was "
+ System.currentTimeMillis() - initialTime);
}
}