我有一个无限的轮询循环使用java.nio.file.WatchService寻找新文件。在循环中我有固定的线程池执行器服务来同时处理文件。
当轮询服务继续运行时,我如何对一批10 / n个文件进行处理所花费的时间进行基准测试。我能够为runnable类中的每个文件计时但是如何获得批处理时间? / p>
答案 0 :(得分:1)
这样的事情应该有效:
// 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);
}
}