如何检测信息流并跟踪进度?(香草Java8或cylcops反应型反应流)

凯西

给定一些使用流处理大量项目的代码,什么是记录日志记录和性能/性能分析各个步骤的最佳方法?

实际示例:

  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();

记录进度很重要,因此我可以在另一个更新用户界面的线程中触发进度事件。

希望跟踪此流中的过滤和映射步骤的性能特征,以了解可以在何处进行优化以加快速度。

我看到三个选择:

  1. 在每个函数中放置日志记录/性能分析代码
  2. 使用peek围绕每一个步骤而无需实际使用值
  3. 某种基于注释或AOP的解决方案(不知道是什么)

哪个最好?关于#3的想法如何?还有其他解决方案吗?

约翰·麦克林

您在这里有几个选择(如果我理解正确的话):-

  1. 我们可以利用经过的运算符来跟踪元素发射之间的经过时间,例如

      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;
      }
    

这仅适用于独眼巨人反应流。

  1. 我们可以在FluentFunctions中使用类似AOP的功能

例如

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章