java - 如何对无限循环 java nio watchservice 程序进行基准测试

标签 java benchmarking

我有一个使用 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);
   }
}

关于java - 如何对无限循环 java nio watchservice 程序进行基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775819/

相关文章:

java - 使用 webAllowOthers 的 H2 控制台远程访问

java - 从未知内容类型的文档中提取文本

benchmarking - Meteor 在众多客户之间共享大量收藏时的效率如何?

c++ - C++ 中的时间测量 (gettimeofday) 给出重复的结果

java - 我可以使用 printComponent 而不必先将组件绘制到屏幕上吗?

java - 将100万条记录写入一个excel文件

rust - 如何在 Rust 中为 Criterion 基准创建随机输入

c - 使用堆存储而不是堆栈(K&R 5.7)

ruby - Elixir 比 Ruby 慢吗?

java - 如果文件具有 unix 或 dos 样式的行结尾,eclipse 如何计算?