2016-11-30 31 views
0

我的問題與this SO Question非常相似,即我希望測量單個項目 - 讀取器,處理器和寫入器的性能。測量單個項目(閱讀器,處理器和寫入器)的性能 - 彈簧批量

正如在回答鏈接的問題的建議,我添加了一個ItemReadListener<T>但就是當我使用JdbcPagingItemReader和想讀的每一頁,而不是單個項目所用的時間給我時間,每次讀取項目。

對於處理器也是類似的情況(也就是說,它會給我每個項目的時間),而我希望處理器花費時間準備整個塊。

A ChunkListener看起來不合適,因爲那樣會再次測量閱讀器,處理器和寫入器的組合時間。

我該如何達到這個要求?

+0

您可以使用VisualMV來分析您的應用程序 –

+0

我想將這些時間記錄在應用程序日誌中以備將來使用和分析。我可以有效地記錄作者的時間,因爲這需要一個List的項目,我測量完成write方法的時間,但是對於讀者和處理器,我所能做的只是測量單個項目的時間,這是非常無用的。 –

+0

你可以寫一個方面來記錄所涉及的時間...... –

回答

1

這是一個非線程安全的簡單示例,它可以用來計算處理單個項目時處理器的執行時間並記錄總執行時間。

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。只需將處理器調整到上方即可看到效果。

相同的邏輯可以應用於讀寫器。

+0

我想,你還需要在'if(count == 5)'裏面重置'processTime''。我已經將我的塊大小作爲實例字段添加到處理器,並將'count'與該值進行比較,留下邊緣情況,即塊大小爲1000,總項爲5423,日誌將僅打印五次。 –

+0

如果你只對一定數量的項目(這裏是5)的processTime感興趣,那麼是的。 而且正如我的例子的評論所指出的,殘留物品不會被計算在內。 –

相關問題