我正在做一个项目,我正在尝试对我的一些客户端代码进行基准测试。
因此,为此我编写了一个Multithreading program
将生成多个线程并且每个线程将运行特定持续时间的程序,并且在程序完成后,我调用logHistogramInfo() method
finally 块为我打印出historgram information
它将告诉我我的客户端代码的行为方式.
下面是我的代码,其中我产生了多个线程,在 finally 块中,我调用logHistogramInfo method
-
public static void main(String[] args) {
try {
readPropertyFiles(args);
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
long startTime = System.currentTimeMillis();
long endTime = startTime + (durationOfRun * 60 * 1000);
for (int i = 0; i < threads; i++) {
service.submit(new CassandraReadTask(endTime, columnFamilyList));
}
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
} catch (Exception e) {
} finally {
logHistogramInfo();
}
LOG.info("All threads are finished");
}
下面是我的直方图方法-
private static void logHistogramInfo() {
int[] definition = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
long[] buckets = new long[definition.length];
System.out.println("Milliseconds Number");
SortedSet<Long> keys = new TreeSet<Long>(CassandraTimer.histogram.keySet());
for (long key : keys) {
Long value = CassandraTimer.histogram.get(key);
System.out.println(key + " " + value);
}
//Histogram information
for (Long time : CassandraTimer.histogram.keySet()) {
for (int i = definition.length - 1; i >= 0; i--) {
if (time >= definition[i]) {
buckets[i] += CassandraTimer.histogram.get(time);
break;
}
}
}
for (int i = 0; i < definition.length; i++) {
String period = "";
if (i == definition.length - 1) {
period = "greater than " + definition[i] + " ms";
} else {
period = "between " + (definition[i] + 1) + " and " + definition[i + 1] + " ms";
}
LOG.info(buckets[i] + " came back " + period);
}
}
问题陈述:-
一切对我来说都很好。现在唯一的问题是,假设如果我正在运行我的程序,10hours
那么我需要等待我的程序完成。仅在 10 小时后,我就可以看到结果是什么。
我有兴趣在程序运行时查看结果。只是为了了解程序的行为方式。我不想等待 10 个小时然后看到结果。
现在我在想,假设我10 hours
每 10 分钟或 20 分钟(它应该是可配置的)运行我的程序,它应该调用logHistogramInfo() method
并向我显示结果。
并且程序运行完成后,会自动调用logHistogramInfo() method
finally 块中的 。所以我最终将能够得到全貌。但是每 20 分钟后,我可以看到程序的行为,而不是等待程序完成。这件事可能吗?
任何例子都会有很大帮助。
更新代码:-
使用下面的代码,它不会每隔几分钟打印一次日志。我尝试调试并将断点放在 try 块中,但它也没有达到该断点。知道为什么吗?
// queue some tasks
long startTime = System.currentTimeMillis();
long endTime = startTime + (durationOfRun * 60 * 1000);
for (int i = 0; i < threads; i++) {
service.submit(new CassandraReadTask(endTime, columnFamilyList));
}
service.shutdown();
loggingAfterEveryXMinutes();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
/**
* This is a simple which will call the logHistogramInfo method after every X Minutes
*/
private static void loggingAfterEveryXMilliseconds() {
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(loggingEveryXMilliseconds);
}
catch (InterruptedException ex) {
}
logHistogramInfo();
}
}
}.start();
}