给定一些使用流处理大量项目的代码,什么是记录日志记录和性能/性能分析各个步骤的最佳方法?
实际示例:
ReactiveSeq.fromStream(pairs)
.filter(this::satisfiesThreshold)
.filter(this::satisfiesPersistConditions)
.map((pair) -> convertToResult(pair, jobId))
.flatMap(Option::toJavaStream)
.grouped(CHUNK_SIZE)
.forEach((chunk) ->
{
repository.save(chunk);
incrementAndReport();
});
reportProcessingTime();
记录进度很重要,因此我可以在另一个更新用户界面的线程中触发进度事件。
希望跟踪此流中的过滤和映射步骤的性能特征,以了解可以在何处进行优化以加快速度。
我看到三个选择:
peek
围绕每一个步骤而无需实际使用值哪个最好?关于#3的想法如何?还有其他解决方案吗?
您在这里有几个选择(如果我理解正确的话):-
我们可以利用经过的运算符来跟踪元素发射之间的经过时间,例如
ReactiveSeq.fromStream(Stream.of(1,2))
.filter(this::include)
.elapsed()
.map(this::logAndUnwrap)
Long[] filterTimeTakenMillis = new Long[maxSize];
int filterIndex = 0;
private <T> T logAndUnwrap(Tuple2<T, Long> t) {
//capture the elapsed time (t.v2) and then unwrap the tuple
filterTimeTakenMillis[filterIndex++]=t.v2;
return t.v1;
}
这仅适用于独眼巨人反应流。
例如
ReactiveSeq.fromStream(Stream.of(1,2))
.filter(this::include)
.elapsed()
.map(this::logAndUnwrap)
.map(FluentFunctions.of(this::convertToResult)
.around(a->{
SimpleTimer timer = new SimpleTimer();
String r = a.proceed();
mapTimeTakenNanos[mapIndex++]=timer.getElapsedNanos();
return r;
}));
这也将适用于原始Java 8 Streams。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句