【发布时间】:2013-04-23 22:43:13
【问题描述】:
我正在对我的一些客户端代码执行bench marking。现在我正在尝试从我的Multithreading 代码中找出如何计算吞吐量-
我正在使用20 threads 运行我的程序。而且每个线程都会为15 minutes运行,所以所有的20 threads will run for 15 minutes。
下面是我的代码-
public static void main(String[] args) {
try {
// create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(20);
// queue some tasks
long startTime = System.currentTimeMillis();
long endTime = startTime + (15 * 60 * 1000);
for (int i = 0; i < 20; i++) {
service.submit(new CassandraReadTask(endTime, columnFamilyList));
}
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (Exception e) {
LOG.warn("Threw a Exception in" + CNAME + e);
}
}
下面是我实现Runnable interface的类-
class CassandraReadTask implements Runnable {
public void run() {
try {
while (System.currentTimeMillis() <= endTime) {
double randomNumber = random.nextDouble() * 100.0;
final String id = generateRandomId(random);
ICassandraClientDao clientDao = ClientFactory.getInstance().getDao(clientName);
clientDao.getAttributes(id, columnsList, columnFamily);
}
} catch (Exception e) {
}
}
}
从上面的代码中,我生成了一些随机 id,我使用它来传递给我的 getAttributes dao 方法。
所以根据我的理解。总计throughput 将是-
total number of request/ total duration the program was run
所以,就我而言,它将是-
total number of id's I have generated/15 minutes
我说的对吗?
【问题讨论】:
标签: java multithreading throughput