Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-13131] [SQL] Use best and average time in benchmark #11018

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions core/src/main/scala/org/apache/spark/util/Benchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,25 @@ private[spark] object Benchmark {
* the rate of the function.
*/
def measure(num: Long, iters: Int, outputPerIteration: Boolean)(f: Int => Unit): Result = {
var totalTime = 0L
var bestTime = Long.MaxValue
for (i <- 0 until iters + 1) {
val start = System.nanoTime()

f(i)

val end = System.nanoTime()
if (i != 0) totalTime += end - start
val runTime = end - start
if (runTime < bestTime) {
bestTime = runTime
}

if (outputPerIteration) {
// scalastyle:off
println(s"Iteration $i took ${(end - start) / 1000} microseconds")
println(s"Iteration $i took ${runTime / 1000} microseconds")
// scalastyle:on
}
}
Result(totalTime.toDouble / 1000000 / iters, num * iters / (totalTime.toDouble / 1000))
Result(bestTime.toDouble / 1000000, num / (bestTime.toDouble / 1000))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed offline, I don't think this is a good idea. at the very least if you want to compare best time, you should also report avg time, which is a more reasonable number to compare.

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,50 @@ import org.apache.spark.util.Benchmark
*/
class BenchmarkWholeStageCodegen extends SparkFunSuite {
lazy val conf = new SparkConf().setMaster("local[1]").setAppName("benchmark")
.set("spark.sql.shuffle.partitions", "1")
lazy val sc = SparkContext.getOrCreate(conf)
lazy val sqlContext = SQLContext.getOrCreate(sc)

def testWholeStage(values: Int): Unit = {
val benchmark = new Benchmark("rang/filter/aggregate", values)
def runBenchmark(name: String, values: Int)(f: => Unit): Unit = {
val benchmark = new Benchmark(name, values)

benchmark.addCase("Without codegen") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", "false")
sqlContext.range(values).filter("(id & 1) = 1").count()
Seq(false, true).foreach { enabled =>
benchmark.addCase(s"$name codegen=$enabled") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", enabled.toString)
f
}
}

benchmark.addCase("With codegen") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", "true")
sqlContext.range(values).filter("(id & 1) = 1").count()
}
benchmark.run()
}

def testWholeStage(values: Int): Unit = {

runBenchmark("rang/filter/sum", values) {
sqlContext.range(values).filter("(id & 1) = 1").groupBy().sum().collect()
}
/*
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
rang/filter/aggregate: Avg Time(ms) Avg Rate(M/s) Relative Rate
-------------------------------------------------------------------------------
Without codegen 7775.53 26.97 1.00 X
With codegen 342.15 612.94 22.73 X
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
rang/filter/aggregate: Avg Time(ms) Avg Rate(M/s) Relative Rate
-------------------------------------------------------------------------------
rang/filter/aggregate codegen=false 12509.22 41.91 1.00 X
rang/filter/aggregate codegen=true 846.38 619.45 14.78 X
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query is changed from .count() to .groupBy().sum().collect(), it's 20X for previous query.

*/
benchmark.run()
}

def testAggregateWithKey(values: Int): Unit = {
val benchmark = new Benchmark("Aggregate with keys", values)

benchmark.addCase("Aggregate w/o codegen") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", "false")
sqlContext.range(values).selectExpr("(id & 65535) as k").groupBy("k").sum().collect()
}
benchmark.addCase(s"Aggregate w codegen") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", "true")
runBenchmark("Aggregate w keys", values) {
sqlContext.range(values).selectExpr("(id & 65535) as k").groupBy("k").sum().collect()
}

/*
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Aggregate with keys: Avg Time(ms) Avg Rate(M/s) Relative Rate
-------------------------------------------------------------------------------
Aggregate w/o codegen 4254.38 4.93 1.00 X
Aggregate w codegen 2661.45 7.88 1.60 X
Aggregate w keys codegen=false 2589.00 8.10 1.00 X
Aggregate w keys codegen=true 1645.38 12.75 1.57 X
*/
benchmark.run()
}

def testBytesToBytesMap(values: Int): Unit = {
Expand Down Expand Up @@ -138,18 +136,18 @@ class BenchmarkWholeStageCodegen extends SparkFunSuite {

/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Aggregate with keys: Avg Time(ms) Avg Rate(M/s) Relative Rate
BytesToBytesMap: Avg Time(ms) Avg Rate(M/s) Relative Rate
-------------------------------------------------------------------------------
hash 662.06 79.19 1.00 X
BytesToBytesMap (off Heap) 2209.42 23.73 0.30 X
BytesToBytesMap (on Heap) 2957.68 17.73 0.22 X
hash 603.61 86.86 1.00 X
BytesToBytesMap (off Heap) 3297.39 15.90 0.18 X
BytesToBytesMap (on Heap) 3607.09 14.53 0.17 X
*/
benchmark.run()
}

test("benchmark") {
// testWholeStage(1024 * 1024 * 200)
// testWholeStage(500 << 20)
// testAggregateWithKey(20 << 20)
// testBytesToBytesMap(1024 * 1024 * 50)
// testBytesToBytesMap(50 << 20)
}
}