Repository: spark
Updated Branches:
  refs/heads/master 8b727994e -> f309b28bd


[SPARK-25485][SQL][TEST] Refactor UnsafeProjectionBenchmark to use main method

## What changes were proposed in this pull request?

Refactor `UnsafeProjectionBenchmark` to use main method.
Generate benchmark result:

```
SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "catalyst/test:runMain 
org.apache.spark.sql.UnsafeProjectionBenchmark"
```

## How was this patch tested?

manual test

Closes #22493 from yucai/SPARK-25485.

Lead-authored-by: yucai <y...@ebay.com>
Co-authored-by: Yucai Yu <yucai...@foxmail.com>
Co-authored-by: Dongjoon Hyun <dongj...@apache.org>
Signed-off-by: Dongjoon Hyun <dongj...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/f309b28b
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/f309b28b
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/f309b28b

Branch: refs/heads/master
Commit: f309b28bd9271719ca36fcf334f016ed6165a79b
Parents: 8b72799
Author: yucai <y...@ebay.com>
Authored: Wed Sep 26 23:27:45 2018 -0700
Committer: Dongjoon Hyun <dongj...@apache.org>
Committed: Wed Sep 26 23:27:45 2018 -0700

----------------------------------------------------------------------
 .../UnsafeProjectionBenchmark-results.txt       |  14 ++
 .../spark/sql/UnsafeProjectionBenchmark.scala   | 172 +++++++++----------
 2 files changed, 98 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/f309b28b/sql/catalyst/benchmarks/UnsafeProjectionBenchmark-results.txt
----------------------------------------------------------------------
diff --git a/sql/catalyst/benchmarks/UnsafeProjectionBenchmark-results.txt 
b/sql/catalyst/benchmarks/UnsafeProjectionBenchmark-results.txt
new file mode 100644
index 0000000..43156dc
--- /dev/null
+++ b/sql/catalyst/benchmarks/UnsafeProjectionBenchmark-results.txt
@@ -0,0 +1,14 @@
+================================================================================================
+unsafe projection
+================================================================================================
+
+OpenJDK 64-Bit Server VM 1.8.0_181-b13 on Linux 3.10.0-862.3.2.el7.x86_64
+Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz
+unsafe projection:                       Best/Avg Time(ms)    Rate(M/s)   Per 
Row(ns)   Relative
+------------------------------------------------------------------------------------------------
+single long                                   2867 / 2868         93.6         
 10.7       1.0X
+single nullable long                          3915 / 3949         68.6         
 14.6       0.7X
+7 primitive types                             8166 / 8167         32.9         
 30.4       0.4X
+7 nullable primitive types                  12767 / 12767         21.0         
 47.6       0.2X
+
+

http://git-wip-us.apache.org/repos/asf/spark/blob/f309b28b/sql/catalyst/src/test/scala/org/apache/spark/sql/UnsafeProjectionBenchmark.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/UnsafeProjectionBenchmark.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/UnsafeProjectionBenchmark.scala
index faff681..cbe723f 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/UnsafeProjectionBenchmark.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/UnsafeProjectionBenchmark.scala
@@ -17,7 +17,7 @@
 
 package org.apache.spark.sql
 
-import org.apache.spark.benchmark.Benchmark
+import org.apache.spark.benchmark.{Benchmark, BenchmarkBase}
 import org.apache.spark.sql.catalyst.InternalRow
 import org.apache.spark.sql.catalyst.encoders.RowEncoder
 import org.apache.spark.sql.catalyst.expressions.UnsafeProjection
@@ -25,8 +25,15 @@ import org.apache.spark.sql.types._
 
 /**
  * Benchmark `UnsafeProjection` for fixed-length/primitive-type fields.
+ * {{{
+ *   To run this benchmark:
+ *   1. without sbt: bin/spark-submit --class <this class> <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/UnsafeProjectionBenchmark-results.txt".
+ * }}}
  */
-object UnsafeProjectionBenchmark {
+object UnsafeProjectionBenchmark extends BenchmarkBase {
 
   def generateRows(schema: StructType, numRows: Int): Array[InternalRow] = {
     val generator = RandomDataGenerator.forType(schema, nullable = false).get
@@ -34,103 +41,92 @@ object UnsafeProjectionBenchmark {
     (1 to numRows).map(_ => 
encoder.toRow(generator().asInstanceOf[Row]).copy()).toArray
   }
 
-  def main(args: Array[String]) {
-    val iters = 1024 * 16
-    val numRows = 1024 * 16
-
-    val benchmark = new Benchmark("unsafe projection", iters * numRows.toLong)
-
-
-    val schema1 = new StructType().add("l", LongType, false)
-    val attrs1 = schema1.toAttributes
-    val rows1 = generateRows(schema1, numRows)
-    val projection1 = UnsafeProjection.create(attrs1, attrs1)
-
-    benchmark.addCase("single long") { _ =>
-      for (_ <- 1 to iters) {
-        var sum = 0L
-        var i = 0
-        while (i < numRows) {
-          sum += projection1(rows1(i)).getLong(0)
-          i += 1
+  override def benchmark(): Unit = {
+    runBenchmark("unsafe projection") {
+      val iters = 1024 * 16
+      val numRows = 1024 * 16
+
+      val benchmark = new Benchmark("unsafe projection", iters * 
numRows.toLong, output = output)
+
+      val schema1 = new StructType().add("l", LongType, false)
+      val attrs1 = schema1.toAttributes
+      val rows1 = generateRows(schema1, numRows)
+      val projection1 = UnsafeProjection.create(attrs1, attrs1)
+
+      benchmark.addCase("single long") { _ =>
+        for (_ <- 1 to iters) {
+          var sum = 0L
+          var i = 0
+          while (i < numRows) {
+            sum += projection1(rows1(i)).getLong(0)
+            i += 1
+          }
         }
       }
-    }
-
-    val schema2 = new StructType().add("l", LongType, true)
-    val attrs2 = schema2.toAttributes
-    val rows2 = generateRows(schema2, numRows)
-    val projection2 = UnsafeProjection.create(attrs2, attrs2)
 
-    benchmark.addCase("single nullable long") { _ =>
-      for (_ <- 1 to iters) {
-        var sum = 0L
-        var i = 0
-        while (i < numRows) {
-          sum += projection2(rows2(i)).getLong(0)
-          i += 1
+      val schema2 = new StructType().add("l", LongType, true)
+      val attrs2 = schema2.toAttributes
+      val rows2 = generateRows(schema2, numRows)
+      val projection2 = UnsafeProjection.create(attrs2, attrs2)
+
+      benchmark.addCase("single nullable long") { _ =>
+        for (_ <- 1 to iters) {
+          var sum = 0L
+          var i = 0
+          while (i < numRows) {
+            sum += projection2(rows2(i)).getLong(0)
+            i += 1
+          }
         }
       }
-    }
-
 
-    val schema3 = new StructType()
-      .add("boolean", BooleanType, false)
-      .add("byte", ByteType, false)
-      .add("short", ShortType, false)
-      .add("int", IntegerType, false)
-      .add("long", LongType, false)
-      .add("float", FloatType, false)
-      .add("double", DoubleType, false)
-    val attrs3 = schema3.toAttributes
-    val rows3 = generateRows(schema3, numRows)
-    val projection3 = UnsafeProjection.create(attrs3, attrs3)
-
-    benchmark.addCase("7 primitive types") { _ =>
-      for (_ <- 1 to iters) {
-        var sum = 0L
-        var i = 0
-        while (i < numRows) {
-          sum += projection3(rows3(i)).getLong(0)
-          i += 1
+      val schema3 = new StructType()
+        .add("boolean", BooleanType, false)
+        .add("byte", ByteType, false)
+        .add("short", ShortType, false)
+        .add("int", IntegerType, false)
+        .add("long", LongType, false)
+        .add("float", FloatType, false)
+        .add("double", DoubleType, false)
+      val attrs3 = schema3.toAttributes
+      val rows3 = generateRows(schema3, numRows)
+      val projection3 = UnsafeProjection.create(attrs3, attrs3)
+
+      benchmark.addCase("7 primitive types") { _ =>
+        for (_ <- 1 to iters) {
+          var sum = 0L
+          var i = 0
+          while (i < numRows) {
+            sum += projection3(rows3(i)).getLong(0)
+            i += 1
+          }
         }
       }
-    }
-
-
-    val schema4 = new StructType()
-      .add("boolean", BooleanType, true)
-      .add("byte", ByteType, true)
-      .add("short", ShortType, true)
-      .add("int", IntegerType, true)
-      .add("long", LongType, true)
-      .add("float", FloatType, true)
-      .add("double", DoubleType, true)
-    val attrs4 = schema4.toAttributes
-    val rows4 = generateRows(schema4, numRows)
-    val projection4 = UnsafeProjection.create(attrs4, attrs4)
 
-    benchmark.addCase("7 nullable primitive types") { _ =>
-      for (_ <- 1 to iters) {
-        var sum = 0L
-        var i = 0
-        while (i < numRows) {
-          sum += projection4(rows4(i)).getLong(0)
-          i += 1
+      val schema4 = new StructType()
+        .add("boolean", BooleanType, true)
+        .add("byte", ByteType, true)
+        .add("short", ShortType, true)
+        .add("int", IntegerType, true)
+        .add("long", LongType, true)
+        .add("float", FloatType, true)
+        .add("double", DoubleType, true)
+      val attrs4 = schema4.toAttributes
+      val rows4 = generateRows(schema4, numRows)
+      val projection4 = UnsafeProjection.create(attrs4, attrs4)
+
+      benchmark.addCase("7 nullable primitive types") { _ =>
+        for (_ <- 1 to iters) {
+          var sum = 0L
+          var i = 0
+          while (i < numRows) {
+            sum += projection4(rows4(i)).getLong(0)
+            i += 1
+          }
         }
       }
-    }
-
 
-    /*
-    Intel(R) Core(TM) i7-4960HQ CPU @ 2.60GHz
-    unsafe projection:                 Avg Time(ms)    Avg Rate(M/s)  Relative 
Rate
-    
-------------------------------------------------------------------------------
-    single long                             1533.34           175.07         
1.00 X
-    single nullable long                    2306.73           116.37         
0.66 X
-    primitive types                         8403.93            31.94         
0.18 X
-    nullable primitive types               12448.39            21.56         
0.12 X
-     */
-    benchmark.run()
+      benchmark.run()
+    }
   }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to