你的问题太模糊了。
例如,您是否有例如 1000 个 SQL 传递给 SQL 服务器以作为批处理执行?
在这种情况下,您可以这样做:
long start = System.currentTimeMillis();
execute SQL script
System.out.println("took:"+System.currentTimeMillis() - start);
如果您有一系列 sql 查询要作为独立于应用程序的操作运行并且想要测量平均时间,您可以使用执行器提交任务,添加每个任务的时间(类似于上面),最后划分按完成的查询数。
更新:
您可以为此使用线程池。
示例(省略异常处理):
Collection<Callable<Long>> sqlTasks = new ArrayList<Callable<Long>>();
sqlTasks.add(new Callable<Long>(){
@Override
public Long call() throws Exception {
long start = System.currentTimeMillis();
// DO SQL QUERY HERE
//return delay
return System.currentTimeMillis() - start;
}
});
//ADD MORE TO THE QUEUE
ExecutorService threadPool = Executors.newCachedThreadPool();
List<Future<Long>> results = threadPool.invokeAll(sqlTasks);
long totalDelay = 0;
for(Future<Long> delay:results){
System.out.println("Delay for task"+delay.get());
totalDelay += delay.get();
}
System.out.println("Average Delay per task"+totalDelay / sqlTasks.size());
您将 SQL 查询添加到队列中,它们由线程池中的线程执行并返回其延迟。
但是您还应该提及您正在尝试的 SQL DB。也许已经有一个性能工具可以做到这一点