GideonPotok commented on code in PR #45453: URL: https://github.com/apache/spark/pull/45453#discussion_r1538204774
########## sql/core/src/test/scala/org/apache/spark/sql/execution/benchmark/CollationBenchmark.scala: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.spark.sql.execution.benchmark + +import org.apache.spark.benchmark.Benchmark +import org.apache.spark.sql.DataFrame +import org.apache.spark.sql.catalyst.util.CollationFactory +import org.apache.spark.sql.functions._ +import org.apache.spark.unsafe.types.UTF8String + +/** + * Benchmark to measure performance for comparisons between collated strings. To run this benchmark: + * {{{ + * 1. without sbt: + * bin/spark-submit --class <this class> + * --jars <spark core test jar>,<spark catalyst test jar> <spark sql test jar> + * 2. build/sbt "sql/Test/runMain org.apache.spark.sql.execution.benchmark.CollationBenchmark" + * 3. generate result: + * SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "sql/Test/runMain <this class>" + * Results will be written to "benchmarks/CollationBenchmark-results.txt". + * }}} + */ + +object CollationBenchmark extends SqlBasedBenchmark { + private val collationTypes = Seq("UTF8_BINARY_LCASE", "UNICODE", "UTF8_BINARY", "UNICODE_CI") + + def generateSeqInput(n: Long): Seq[UTF8String] = { + val input = Seq("ABC", "ABC", "aBC", "aBC", "abc", "abc", "DEF", "DEF", "def", + "def", "GHI", "ghi", + "JKL", "jkl", "MNO", "mno", "PQR", "pqr", "STU", "stu", "VWX", "vwx", "YZ", + "ABC", "ABC", "aBC", "aBC", "abc", "abc", "DEF", "DEF", "def", "def", "GHI", "ghi", + "JKL", "jkl", "MNO", "mno", "PQR", "pqr", "STU", "stu", "VWX", "vwx", "YZ") + .map(UTF8String.fromString) + val inputLong: Seq[UTF8String] = (0L until n).map(i => input(i.toInt % input.size)) + inputLong + } + + private def getDataFrame(strings: Seq[String]): DataFrame = { + val asPairs = strings.sliding(2, 1).toSeq.map { + case Seq(s1, s2) => (s1, s2) + } + val d = spark.createDataFrame(asPairs).toDF("s1", "s2") + d + } + + private def generateDataframeInput(l: Long): DataFrame = { + getDataFrame(generateSeqInput(l).map(_.toString)) + } + + def benchmarkUTFString(collationTypes: Seq[String], utf8Strings: Seq[UTF8String]): Unit = { + val sublistStrings = utf8Strings + + val benchmark = new Benchmark("collation unit benchmarks", utf8Strings.size, output = output) + collationTypes.foreach(collationType => { + val collation = CollationFactory.fetchCollation(collationType) + benchmark.addCase(s"equalsFunction - $collationType") { _ => + sublistStrings.foreach(s1 => + utf8Strings.foreach(s => + collation.equalsFunction(s, s1).booleanValue() + ) + ) + } + benchmark.addCase(s"collator.compare - $collationType") { _ => + sublistStrings.foreach(s1 => + utf8Strings.foreach(s => + collation.comparator.compare(s, s1) + ) + ) + } + benchmark.addCase(s"hashFunction - $collationType") { _ => + sublistStrings.foreach(_ => + utf8Strings.foreach(s => + collation.hashFunction.applyAsLong(s) + ) + ) + } + } + ) + benchmark.run() + } + + def benchmarkFilterEqual(collationTypes: Seq[String], + dfUncollated: DataFrame): Unit = { + val benchmark = + new Benchmark("filter df column with collation", dfUncollated.count(), output = output) + collationTypes.foreach(collationType => { + val dfCollated = dfUncollated.selectExpr( + s"collate(s2, '$collationType') as k2_$collationType", + s"collate(s1, '$collationType') as k1_$collationType") + benchmark.addCase(s"filter df column with collation - $collationType") { _ => + dfCollated.where(col(s"k1_$collationType") === col(s"k2_$collationType")) + .queryExecution.executedPlan.executeCollect() Review Comment: The issue I encountered with `noop()` was that it would hang indefinitely during local execution (for any benchmark I ran), at least with the default JVM settings. Interestingly, I didn't find that modifying `.jvmopts` to have an effect on the observed local JVM properties. I ultimately decided to sidestep the issue and just use `executeCollect`... However, `noop()` does function correctly in GHA. So let's discuss switching to using `noop`, as I am aware it is the preferred choice in the codebase: I'm wondering if you can shed some light on why `noop` tends to be preferred? I would think that both tactics—utilizing executeCollect and executing no-op/in-memory write operations—are effectively identical. I am aware that write is preferable, when benchmarking, to functions such as `count` or `show`, because Spark might optimize calls to those functions streamlining the query execution plan during a count operation, potentially omitting the precise transformation we intend to benchmark if deemed non-critical for producing the count outcome. Does employing executeCollect carry a similar threat of bypassing essential transformations as observed with count? For reference, here are the GHA test runs: - [GHA Test Run 1](https://github.com/GideonPotok/spark/actions/runs/8425725217) - [GHA Test Run 2](https://github.com/GideonPotok/spark/actions/runs/8425691894) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org