這是一個非線程安全的簡單示例,它可以用來計算處理單個項目時處理器的執行時間並記錄總執行時間。
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
private static final Logger log = LoggerFactory.getLogger(PersonItemProcessor.class);
private static int count;
private long processTime;
@Override
public Person process(final Person person) throws Exception {
count++;
long start = System.currentTimeMillis();
final String firstName = person.getFirstName().toUpperCase();
final String lastName = person.getLastName().toUpperCase();
final Person transformedPerson = new Person(firstName, lastName);
// bear in mind that this will pollute the logging when processing millions of records
log.info("Converting (" + person + ") into (" + transformedPerson + ")");
log.info("Processing 1 item took " + (System.currentTimeMillis() - start)/1e3 + " sec");
processTime += System.currentTimeMillis() - start;
// use some interval value, here I used 5.
// last few items won't be counted e.g. 18 items you will see this message 3 times.
if (count == 5) {
log.info(String.format("Processed %d items in %s sec", count, processTime/1e3));
count = 0;
}
return transformedPerson;
}
public long getProcessTime(){
return processTime;
}
}
其餘的代碼你可以找到here。只需將處理器調整到上方即可看到效果。
相同的邏輯可以應用於讀寫器。
您可以使用VisualMV來分析您的應用程序 –
我想將這些時間記錄在應用程序日誌中以備將來使用和分析。我可以有效地記錄作者的時間,因爲這需要一個List的項目,我測量完成write方法的時間,但是對於讀者和處理器,我所能做的只是測量單個項目的時間,這是非常無用的。 –
你可以寫一個方面來記錄所涉及的時間...... –