|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.benchmark |
| 19 | + |
| 20 | +import org.apache.spark.benchmark.Benchmark |
| 21 | +import org.apache.spark.sql.catalyst.expressions.Literal |
| 22 | +import org.apache.spark.sql.expressions.UserDefinedFunction |
| 23 | +import org.apache.spark.sql.functions._ |
| 24 | +import org.apache.spark.sql.types.{IntegerType, StringType} |
| 25 | + |
| 26 | +/** |
| 27 | + * Synthetic benchmark for Scala User Defined Functions. |
| 28 | + * To run this benchmark: |
| 29 | + * {{{ |
| 30 | + * 1. without sbt: |
| 31 | + * bin/spark-submit --class <this class> --jars <spark core test jar> <sql core test jar> |
| 32 | + * 2. build/sbt "sql/test:runMain <this class>" |
| 33 | + * 3. generate result: |
| 34 | + * SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "sql/test:runMain <this class>" |
| 35 | + * Results will be written to "benchmarks/UDFBenchmark-results.txt". |
| 36 | + * }}} |
| 37 | + */ |
| 38 | +object UDFBenchmark extends SqlBasedBenchmark { |
| 39 | + |
| 40 | + private def doRunBenchmarkWithMixedTypes(udf: UserDefinedFunction, cardinality: Int): Unit = { |
| 41 | + val idCol = col("id") |
| 42 | + val nullableIntCol = when( |
| 43 | + idCol % 2 === 0, idCol.cast(IntegerType)).otherwise(Literal(null, IntegerType)) |
| 44 | + val stringCol = idCol.cast(StringType) |
| 45 | + spark.range(cardinality).select( |
| 46 | + udf(idCol, nullableIntCol, stringCol)).write.format("noop").save() |
| 47 | + } |
| 48 | + |
| 49 | + private def doRunBenchmarkWithPrimitiveTypes( |
| 50 | + udf: UserDefinedFunction, cardinality: Int): Unit = { |
| 51 | + val idCol = col("id") |
| 52 | + val nullableIntCol = when( |
| 53 | + idCol % 2 === 0, idCol.cast(IntegerType)).otherwise(Literal(null, IntegerType)) |
| 54 | + spark.range(cardinality).select(udf(idCol, nullableIntCol)).write.format("noop").save() |
| 55 | + } |
| 56 | + |
| 57 | + override def runBenchmarkSuite(mainArgs: Array[String]): Unit = { |
| 58 | + val cardinality = 100000 |
| 59 | + runBenchmark("UDF with mixed input types") { |
| 60 | + codegenBenchmark("long/nullable int/string to string", cardinality) { |
| 61 | + val sampleUDF = udf {(a: Long, b: java.lang.Integer, c: String) => |
| 62 | + s"$a,$b,$c" |
| 63 | + } |
| 64 | + doRunBenchmarkWithMixedTypes(sampleUDF, cardinality) |
| 65 | + } |
| 66 | + |
| 67 | + codegenBenchmark("long/nullable int/string to option", cardinality) { |
| 68 | + val sampleUDF = udf {(_: Long, b: java.lang.Integer, _: String) => |
| 69 | + Option(b) |
| 70 | + } |
| 71 | + doRunBenchmarkWithMixedTypes(sampleUDF, cardinality) |
| 72 | + } |
| 73 | + |
| 74 | + codegenBenchmark("long/nullable int/string to primitive", cardinality) { |
| 75 | + val sampleUDF = udf {(a: Long, b: java.lang.Integer, _: String) => |
| 76 | + Option(b).map(_.longValue()).getOrElse(a) |
| 77 | + } |
| 78 | + doRunBenchmarkWithMixedTypes(sampleUDF, cardinality) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + runBenchmark("UDF with primitive types") { |
| 83 | + codegenBenchmark("long/nullable int to string", cardinality) { |
| 84 | + val sampleUDF = udf {(a: Long, b: java.lang.Integer) => |
| 85 | + s"$a,$b" |
| 86 | + } |
| 87 | + doRunBenchmarkWithPrimitiveTypes(sampleUDF, cardinality) |
| 88 | + } |
| 89 | + |
| 90 | + codegenBenchmark("long/nullable int to option", cardinality) { |
| 91 | + val sampleUDF = udf {(_: Long, b: java.lang.Integer) => |
| 92 | + Option(b) |
| 93 | + } |
| 94 | + doRunBenchmarkWithPrimitiveTypes(sampleUDF, cardinality) |
| 95 | + } |
| 96 | + |
| 97 | + codegenBenchmark("long/nullable int to primitive", cardinality) { |
| 98 | + val sampleUDF = udf {(a: Long, b: java.lang.Integer) => |
| 99 | + Option(b).map(_.longValue()).getOrElse(a) |
| 100 | + } |
| 101 | + doRunBenchmarkWithPrimitiveTypes(sampleUDF, cardinality) |
| 102 | + } |
| 103 | + |
| 104 | + val benchmark = new Benchmark("UDF identity overhead", cardinality, output = output) |
| 105 | + |
| 106 | + benchmark.addCase(s"Baseline", numIters = 5) { _ => |
| 107 | + spark.range(cardinality).select( |
| 108 | + col("id"), col("id") * 2, col("id") * 3).write.format("noop").save() |
| 109 | + } |
| 110 | + |
| 111 | + val identityUDF = udf { x: Long => x } |
| 112 | + benchmark.addCase(s"With identity UDF", numIters = 5) { _ => |
| 113 | + spark.range(cardinality).select( |
| 114 | + identityUDF(col("id")), |
| 115 | + identityUDF(col("id") * 2), |
| 116 | + identityUDF(col("id") * 3)).write.format("noop").save() |
| 117 | + } |
| 118 | + |
| 119 | + benchmark.run() |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments