繁体   English   中英

Apache Python 与 Java 之间的光束性能在 GCP 数据流上运行

[英]Apache Beam Performance Between Python Vs Java Running on GCP Dataflow

我们在使用 Python 和 Java 编写的 GCP 数据流上运行 Beam 数据管道。 一开始,我们有一些简单直接的 python 梁作业,效果很好。 所以最近我们决定将更多的 java 光束转换为 python 光束作业。 当我们有更复杂的工作时,特别是需要在光束中开窗的工作时,我们注意到 python 工作比 java 工作明显慢,最终使用更多的 CPU 和 ZCD69B4957F06CD818D7BF3D61980E2 和成本更多

一些示例 python 代码如下所示:

        step1 = (
        read_from_pub_sub
        | "MapKey" >> beam.Map(lambda elem: (elem.data[key], elem))
        | "WindowResults"
        >> beam.WindowInto(
            beam.window.SlidingWindows(360,90),
            allowed_lateness=args.allowed_lateness,
        )
        | "GroupById" >> beam.GroupByKey()

Java 代码如下:

 PCollection<DataStructure> step1 =
      message
          .apply(
              "MapKey",
              MapElements.into(
                      TypeDescriptors.kvs(
                          TypeDescriptors.strings(), TypeDescriptor.of(DataStructure.class)))
                  .via(event -> KV.of(event.key, event)))
          .apply(
              "WindowResults",
              Window.<KV<String, CustomInterval>>into(
                      SlidingWindows.of(Duration.standardSeconds(360))
                          .every(Duration.standardSeconds(90)))
                  .withAllowedLateness(Duration.standardSeconds(this.allowedLateness))
                  .discardingFiredPanes())
          .apply("GroupById", GroupByKey.<String, DataStructure>create())

我们注意到 Python 使用的 CPU 和 memory 总是比所需的 Java 多 3 倍。 我们做了一些实验测试,只运行 JSON 输入和 JSON output,结果相同。 We are not sure that is just because Python, in general, is slower than java or the way the GCP Dataflow execute Beam Python and Java is different. 任何类似的经验、测试和原因都值得赞赏。

是的,这是 Python 和 Java 之间的一个非常正常的性能因素。 事实上,对于许多程序来说,这个因素可能是 10 倍或更多。

程序的细节可以从根本上改变相对性能。 这里有一些要考虑的事情:

如果您更喜欢 Python 的简洁语法或库生态系统,那么实现速度的方法是使用优化的 C 库或 Cython 进行核心处理,例如使用 pandas/numpy/等。 如果您使用Beam 新的兼容 Pandas 的 dataframe API ,您将自动获得此好处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM