如何测量从数据库异步读取数据的时间

时间:2019-03-27 10:15:41

标签: java parallel-processing

我想从数据库读取异步数据并测量该读取时间。我正在使用CompletableFuture.runAsync方法。

在我的应用程序中,我有很多类似的类:

public class ReadDataFromDB implements IInitialization {

    @Override
    public void init() throws ReadingException {
        //delegate to other class which will read data from db      
    }
}

初始化是一种方法public void init() throws ReadingException

的功能接口

我收集了所有这些类以列出:

List<IInitialization> input = Arrays.asList(new ReadDataFromDB(), ...);

并异步运行它:

long l = input.stream().map(i -> CompletableFuture.runAsync(() -> {
    try {
        i.init();
    } catch (ReadingException e) {
        e.printStackTrace();
    }
}, threadPool)).count();

我定义为的工人池:

ExecutorService threadPool = Executors.newFixedThreadPool(10, r -> {

    Thread thread = new Thread(r);
    thread.setUncaughtExceptionHandler(null);
    return thread;
});

所有这些我都用一种方法private void readDataAsync() throws ReadingException

我尝试通过以下方式测量时间:

long begin = System.currentTimeMillis();
readDataAsync();
long end = System.currentTimeMillis();
System.out.println("readDataAsync: " + (end - begin) / 1000.0);

这种方法不起作用,因为控制台显示readDataAsync: 0.043,服务器已启动,但是我的客户端在几秒钟后(比同步快)可以看到数据。

有什么想法我该如何正确地衡量这次从DB读取异步数据的时间?

0 个答案:

没有答案
相关问题