dongjoon-hyun commented on a change in pull request #33748:
URL: https://github.com/apache/spark/pull/33748#discussion_r690471027



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/benchmark/FileMetaCacheReadBenchmark.scala
##########
@@ -0,0 +1,129 @@
+/*
+ * 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 java.io.File
+
+import scala.util.Random
+
+import org.apache.spark.SparkConf
+import org.apache.spark.benchmark.Benchmark
+import org.apache.spark.sql.{DataFrame, DataFrameWriter, Row, SparkSession}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Benchmark to measure the performance of fileMetaCache in data source read.
+ * 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 <this class>"
+ *   3. generate result: SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt 
"sql/test:runMain <this class>"
+ *      Results will be written to 
"benchmarks/FileMetaCacheReadBenchmark-results.txt".
+ * }}}
+ */
+object FileMetaCacheReadBenchmark extends SqlBasedBenchmark {
+
+  override def getSparkSession: SparkSession = {
+    val conf = new SparkConf()
+      .setAppName("FileMetaCacheReadBenchmark")
+      // Since `spark.master` always exists, overrides this value
+      .set("spark.master", "local[1]")
+      .setIfMissing("spark.driver.memory", "4g")
+      .setIfMissing("spark.executor.memory", "4g")
+
+    val sparkSession = SparkSession.builder().config(conf).getOrCreate()
+
+    // Set default configs. Individual cases will change them if necessary.
+    sparkSession.conf.set(SQLConf.ORC_FILTER_PUSHDOWN_ENABLED.key, "true")
+    sparkSession.conf.set(SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key, "true")
+    sparkSession.conf.set(SQLConf.FILE_META_CACHE_TTL_SINCE_LAST_ACCESS.key, 
"5")
+
+    sparkSession
+  }
+
+  def withTempTable(tableNames: String*)(f: => Unit): Unit = {
+    try f finally tableNames.foreach(spark.catalog.dropTempView)
+  }
+
+  private def prepareTable(dir: File, df: DataFrame, fileCount: Int): Unit =
+    saveAsOrcTable( df.repartition(fileCount).write, dir.getCanonicalPath + 
"/orc")
+
+  private def saveAsOrcTable(df: DataFrameWriter[Row], dir: String): Unit = {
+    df.mode("overwrite").option("compression", "snappy").orc(dir)
+    spark.read.orc(dir).createOrReplaceTempView("orcTable")
+  }
+
+  def fileScanBenchmark(values: Int, width: Int, fileCount: Int): Unit = {
+    val benchmark = new Benchmark(
+      s"Scan from $width columns with $fileCount files",
+      values,
+      output = output)
+
+    withTempPath { dir =>
+      withTempTable("t1", "orcTable") {
+        import spark.implicits._
+        val middle = width / 2
+        val selectExpr = (1 to width).map(i => s"value as c$i")
+        val dataFrame = spark.range(values).map(_ => Random.nextLong).toDF()
+        dataFrame.selectExpr(selectExpr: _*).createOrReplaceTempView("t1")
+
+        prepareTable(dir, spark.sql("SELECT * FROM t1"), fileCount)
+
+        val filter = {
+          val rows =
+            spark.sql(s"SELECT c1, count(*) FROM orcTable group by c1")
+              .collect()
+              .sortBy(r => r.getLong(1))
+          rows.head.getLong(0)
+        }
+
+        benchmark.addCase("ORC Vectorized Full Scan: fileMetaCacheEnabled = 
false") { _ =>
+          spark.sql(s"SELECT sum(c$middle) FROM orcTable").noop()
+        }
+
+        benchmark.addCase("ORC Vectorized Full Scan: fileMetaCacheEnabled = 
true") { _ =>
+          withSQLConf(SQLConf.FILE_META_CACHE_ENABLED.key -> "true") {
+            spark.sql(s"SELECT sum(c$middle) FROM orcTable").noop()
+          }
+        }
+
+        benchmark.addCase("ORC Vectorized Filter Scan: fileMetaCacheEnabled = 
false") { _ =>
+          spark.sql(s"SELECT sum(c$middle) FROM orcTable where c1 = 
$filter").noop()
+        }
+
+        benchmark.addCase("ORC Vectorized Filter Scan: fileMetaCacheEnabled = 
true") { _ =>
+          withSQLConf(SQLConf.FILE_META_CACHE_ENABLED.key -> "true") {
+            spark.sql(s"SELECT sum(c$middle) FROM orcTable where c1 = 
$filter").noop()
+          }
+        }
+
+        benchmark.run()
+      }
+    }
+  }
+
+  override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
+    for (fileCount <- List(100, 500, 1000)) {
+      runBenchmark(s"Scan From $fileCount files") {
+        for (columnWidth <- List(10, 50, 100)) {
+          fileScanBenchmark(1024 * 1024 * 5, columnWidth, fileCount)

Review comment:
       Could you add a simple `countBenchmark`, @LuciferYang ?
   I guess your PR's contribution will improve the performance of `count(*)` 
queries without `filter`s most significantly when we need only read footer 
information.




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

Reply via email to