1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
/*
* 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
import java.util.HashMap
import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite}
import org.apache.spark.memory.{StaticMemoryManager, TaskMemoryManager}
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.catalyst.expressions.UnsafeRow
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types.IntegerType
import org.apache.spark.unsafe.Platform
import org.apache.spark.unsafe.hash.Murmur3_x86_32
import org.apache.spark.unsafe.map.BytesToBytesMap
import org.apache.spark.util.Benchmark
/**
* Benchmark to measure whole stage codegen performance.
* To run this:
* build/sbt "sql/test-only *BenchmarkWholeStageCodegen"
*/
class BenchmarkWholeStageCodegen extends SparkFunSuite {
lazy val conf = new SparkConf().setMaster("local[1]").setAppName("benchmark")
.set("spark.sql.shuffle.partitions", "1")
.set("spark.sql.autoBroadcastJoinThreshold", "1")
lazy val sc = SparkContext.getOrCreate(conf)
lazy val sqlContext = SQLContext.getOrCreate(sc)
def runBenchmark(name: String, values: Long)(f: => Unit): Unit = {
val benchmark = new Benchmark(name, values)
Seq(false, true).foreach { enabled =>
benchmark.addCase(s"$name codegen=$enabled") { iter =>
sqlContext.setConf("spark.sql.codegen.wholeStage", enabled.toString)
f
}
}
benchmark.run()
}
// These benchmark are skipped in normal build
ignore("range/filter/sum") {
val N = 500L << 20
runBenchmark("rang/filter/sum", N) {
sqlContext.range(N).filter("(id & 1) = 1").groupBy().sum().collect()
}
/*
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
rang/filter/sum: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
rang/filter/sum codegen=false 14332 / 16646 36.0 27.8 1.0X
rang/filter/sum codegen=true 897 / 1022 584.6 1.7 16.4X
*/
}
ignore("range/limit/sum") {
val N = 500L << 20
runBenchmark("range/limit/sum", N) {
sqlContext.range(N).limit(1000000).groupBy().sum().collect()
}
/*
Westmere E56xx/L56xx/X56xx (Nehalem-C)
range/limit/sum: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
range/limit/sum codegen=false 609 / 672 861.6 1.2 1.0X
range/limit/sum codegen=true 561 / 621 935.3 1.1 1.1X
*/
}
ignore("stat functions") {
val N = 100L << 20
runBenchmark("stddev", N) {
sqlContext.range(N).groupBy().agg("id" -> "stddev").collect()
}
runBenchmark("kurtosis", N) {
sqlContext.range(N).groupBy().agg("id" -> "kurtosis").collect()
}
/**
Using ImperativeAggregate (as implemented in Spark 1.6):
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
stddev: Avg Time(ms) Avg Rate(M/s) Relative Rate
-------------------------------------------------------------------------------
stddev w/o codegen 2019.04 10.39 1.00 X
stddev w codegen 2097.29 10.00 0.96 X
kurtosis w/o codegen 2108.99 9.94 0.96 X
kurtosis w codegen 2090.69 10.03 0.97 X
Using DeclarativeAggregate:
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
stddev: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
stddev codegen=false 5630 / 5776 18.0 55.6 1.0X
stddev codegen=true 1259 / 1314 83.0 12.0 4.5X
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
kurtosis: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
kurtosis codegen=false 14847 / 15084 7.0 142.9 1.0X
kurtosis codegen=true 1652 / 2124 63.0 15.9 9.0X
*/
}
ignore("aggregate with keys") {
val N = 20 << 20
runBenchmark("Aggregate w keys", N) {
sqlContext.range(N).selectExpr("(id & 65535) as k").groupBy("k").sum().collect()
}
/*
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Aggregate w keys: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
Aggregate w keys codegen=false 2429 / 2644 8.6 115.8 1.0X
Aggregate w keys codegen=true 1535 / 1571 13.7 73.2 1.6X
*/
}
ignore("broadcast hash join") {
val N = 100 << 20
val M = 1 << 16
val dim = broadcast(sqlContext.range(M).selectExpr("id as k", "cast(id as string) as v"))
runBenchmark("Join w long", N) {
sqlContext.range(N).join(dim, (col("id") bitwiseAND M) === col("k")).count()
}
/*
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Join w long: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
Join w long codegen=false 5744 / 5814 18.3 54.8 1.0X
Join w long codegen=true 735 / 853 142.7 7.0 7.8X
*/
val dim2 = broadcast(sqlContext.range(M)
.selectExpr("cast(id as int) as k1", "cast(id as int) as k2", "cast(id as string) as v"))
runBenchmark("Join w 2 ints", N) {
sqlContext.range(N).join(dim2,
(col("id") bitwiseAND M).cast(IntegerType) === col("k1")
&& (col("id") bitwiseAND M).cast(IntegerType) === col("k2")).count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Join w 2 ints: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
Join w 2 ints codegen=false 7159 / 7224 14.6 68.3 1.0X
Join w 2 ints codegen=true 1135 / 1197 92.4 10.8 6.3X
*/
val dim3 = broadcast(sqlContext.range(M)
.selectExpr("id as k1", "id as k2", "cast(id as string) as v"))
runBenchmark("Join w 2 longs", N) {
sqlContext.range(N).join(dim3,
(col("id") bitwiseAND M) === col("k1") && (col("id") bitwiseAND M) === col("k2"))
.count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
Join w 2 longs: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
Join w 2 longs codegen=false 7877 / 8358 13.3 75.1 1.0X
Join w 2 longs codegen=true 3877 / 3937 27.0 37.0 2.0X
*/
runBenchmark("outer join w long", N) {
sqlContext.range(N).join(dim, (col("id") bitwiseAND M) === col("k"), "left").count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
outer join w long: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
outer join w long codegen=false 15280 / 16497 6.9 145.7 1.0X
outer join w long codegen=true 769 / 796 136.3 7.3 19.9X
*/
runBenchmark("semi join w long", N) {
sqlContext.range(N).join(dim, (col("id") bitwiseAND M) === col("k"), "leftsemi").count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
semi join w long: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
semi join w long codegen=false 5804 / 5969 18.1 55.3 1.0X
semi join w long codegen=true 814 / 934 128.8 7.8 7.1X
*/
}
ignore("sort merge join") {
val N = 2 << 20
runBenchmark("merge join", N) {
val df1 = sqlContext.range(N).selectExpr(s"id * 2 as k1")
val df2 = sqlContext.range(N).selectExpr(s"id * 3 as k2")
df1.join(df2, col("k1") === col("k2")).count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
merge join: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
merge join codegen=false 1588 / 1880 1.3 757.1 1.0X
merge join codegen=true 1477 / 1531 1.4 704.2 1.1X
*/
runBenchmark("sort merge join", N) {
val df1 = sqlContext.range(N)
.selectExpr(s"(id * 15485863) % ${N*10} as k1")
val df2 = sqlContext.range(N)
.selectExpr(s"(id * 15485867) % ${N*10} as k2")
df1.join(df2, col("k1") === col("k2")).count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
sort merge join: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
sort merge join codegen=false 3626 / 3667 0.6 1728.9 1.0X
sort merge join codegen=true 3405 / 3438 0.6 1623.8 1.1X
*/
}
ignore("shuffle hash join") {
val N = 4 << 20
sqlContext.setConf("spark.sql.shuffle.partitions", "2")
sqlContext.setConf("spark.sql.autoBroadcastJoinThreshold", "10000000")
sqlContext.setConf("spark.sql.join.preferSortMergeJoin", "false")
runBenchmark("shuffle hash join", N) {
val df1 = sqlContext.range(N).selectExpr(s"id as k1")
val df2 = sqlContext.range(N / 5).selectExpr(s"id * 3 as k2")
df1.join(df2, col("k1") === col("k2")).count()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
shuffle hash join: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
shuffle hash join codegen=false 1168 / 1902 3.6 278.6 1.0X
shuffle hash join codegen=true 850 / 1196 4.9 202.8 1.4X
*/
}
ignore("cube") {
val N = 5 << 20
runBenchmark("cube", N) {
sqlContext.range(N).selectExpr("id", "id % 1000 as k1", "id & 256 as k2")
.cube("k1", "k2").sum("id").collect()
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
cube: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
cube codegen=false 3188 / 3392 1.6 608.2 1.0X
cube codegen=true 1239 / 1394 4.2 236.3 2.6X
*/
}
ignore("hash and BytesToBytesMap") {
val N = 10 << 20
val benchmark = new Benchmark("BytesToBytesMap", N)
benchmark.addCase("hash") { iter =>
var i = 0
val keyBytes = new Array[Byte](16)
val key = new UnsafeRow(1)
key.pointTo(keyBytes, Platform.BYTE_ARRAY_OFFSET, 16)
var s = 0
while (i < N) {
key.setInt(0, i % 1000)
val h = Murmur3_x86_32.hashUnsafeWords(
key.getBaseObject, key.getBaseOffset, key.getSizeInBytes, 42)
s += h
i += 1
}
}
benchmark.addCase("fast hash") { iter =>
var i = 0
val keyBytes = new Array[Byte](16)
val key = new UnsafeRow(1)
key.pointTo(keyBytes, Platform.BYTE_ARRAY_OFFSET, 16)
var s = 0
while (i < N) {
key.setInt(0, i % 1000)
val h = Murmur3_x86_32.hashLong(i % 1000, 42)
s += h
i += 1
}
}
benchmark.addCase("arrayEqual") { iter =>
var i = 0
val keyBytes = new Array[Byte](16)
val valueBytes = new Array[Byte](16)
val key = new UnsafeRow(1)
key.pointTo(keyBytes, Platform.BYTE_ARRAY_OFFSET, 16)
val value = new UnsafeRow(1)
value.pointTo(valueBytes, Platform.BYTE_ARRAY_OFFSET, 16)
value.setInt(0, 555)
var s = 0
while (i < N) {
key.setInt(0, i % 1000)
if (key.equals(value)) {
s += 1
}
i += 1
}
}
benchmark.addCase("Java HashMap (Long)") { iter =>
var i = 0
val keyBytes = new Array[Byte](16)
val valueBytes = new Array[Byte](16)
val value = new UnsafeRow(1)
value.pointTo(valueBytes, Platform.BYTE_ARRAY_OFFSET, 16)
value.setInt(0, 555)
val map = new HashMap[Long, UnsafeRow]()
while (i < 65536) {
value.setInt(0, i)
map.put(i.toLong, value)
i += 1
}
var s = 0
i = 0
while (i < N) {
if (map.get(i % 100000) != null) {
s += 1
}
i += 1
}
}
benchmark.addCase("Java HashMap (two ints) ") { iter =>
var i = 0
val valueBytes = new Array[Byte](16)
val value = new UnsafeRow(1)
value.pointTo(valueBytes, Platform.BYTE_ARRAY_OFFSET, 16)
value.setInt(0, 555)
val map = new HashMap[Long, UnsafeRow]()
while (i < 65536) {
value.setInt(0, i)
val key = (i.toLong << 32) + Integer.rotateRight(i, 15)
map.put(key, value)
i += 1
}
var s = 0
i = 0
while (i < N) {
val key = ((i & 100000).toLong << 32) + Integer.rotateRight(i & 100000, 15)
if (map.get(key) != null) {
s += 1
}
i += 1
}
}
benchmark.addCase("Java HashMap (UnsafeRow)") { iter =>
var i = 0
val keyBytes = new Array[Byte](16)
val valueBytes = new Array[Byte](16)
val key = new UnsafeRow(1)
key.pointTo(keyBytes, Platform.BYTE_ARRAY_OFFSET, 16)
val value = new UnsafeRow(1)
value.pointTo(valueBytes, Platform.BYTE_ARRAY_OFFSET, 16)
value.setInt(0, 555)
val map = new HashMap[UnsafeRow, UnsafeRow]()
while (i < 65536) {
key.setInt(0, i)
value.setInt(0, i)
map.put(key, value.copy())
i += 1
}
var s = 0
i = 0
while (i < N) {
key.setInt(0, i % 100000)
if (map.get(key) != null) {
s += 1
}
i += 1
}
}
Seq("off", "on").foreach { heap =>
benchmark.addCase(s"BytesToBytesMap ($heap Heap)") { iter =>
val taskMemoryManager = new TaskMemoryManager(
new StaticMemoryManager(
new SparkConf().set("spark.memory.offHeap.enabled", s"${heap == "off"}")
.set("spark.memory.offHeap.size", "102400000"),
Long.MaxValue,
Long.MaxValue,
1),
0)
val map = new BytesToBytesMap(taskMemoryManager, 1024, 64L<<20)
val keyBytes = new Array[Byte](16)
val valueBytes = new Array[Byte](16)
val key = new UnsafeRow(1)
key.pointTo(keyBytes, Platform.BYTE_ARRAY_OFFSET, 16)
val value = new UnsafeRow(1)
value.pointTo(valueBytes, Platform.BYTE_ARRAY_OFFSET, 16)
var i = 0
while (i < N) {
key.setInt(0, i % 65536)
val loc = map.lookup(key.getBaseObject, key.getBaseOffset, key.getSizeInBytes,
Murmur3_x86_32.hashLong(i % 65536, 42))
if (loc.isDefined) {
value.pointTo(loc.getValueBase, loc.getValueOffset, loc.getValueLength)
value.setInt(0, value.getInt(0) + 1)
i += 1
} else {
loc.putNewKey(key.getBaseObject, key.getBaseOffset, key.getSizeInBytes,
value.getBaseObject, value.getBaseOffset, value.getSizeInBytes)
}
}
}
}
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
BytesToBytesMap: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
hash 651 / 678 80.0 12.5 1.0X
fast hash 336 / 343 155.9 6.4 1.9X
arrayEqual 417 / 428 125.0 8.0 1.6X
Java HashMap (Long) 145 / 168 72.2 13.8 0.8X
Java HashMap (two ints) 157 / 164 66.8 15.0 0.8X
Java HashMap (UnsafeRow) 538 / 573 19.5 51.3 0.2X
BytesToBytesMap (off Heap) 2594 / 2664 20.2 49.5 0.2X
BytesToBytesMap (on Heap) 2693 / 2989 19.5 51.4 0.2X
*/
benchmark.run()
}
ignore("collect") {
val N = 1 << 20
val benchmark = new Benchmark("collect", N)
benchmark.addCase("collect 1 million") { iter =>
sqlContext.range(N).collect()
}
benchmark.addCase("collect 2 millions") { iter =>
sqlContext.range(N * 2).collect()
}
benchmark.addCase("collect 4 millions") { iter =>
sqlContext.range(N * 4).collect()
}
benchmark.run()
/**
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
collect: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
collect 1 million 439 / 654 2.4 418.7 1.0X
collect 2 millions 961 / 1907 1.1 916.4 0.5X
collect 4 millions 3193 / 3895 0.3 3044.7 0.1X
*/
}
ignore("collect limit") {
val N = 1 << 20
val benchmark = new Benchmark("collect limit", N)
benchmark.addCase("collect limit 1 million") { iter =>
sqlContext.range(N * 4).limit(N).collect()
}
benchmark.addCase("collect limit 2 millions") { iter =>
sqlContext.range(N * 4).limit(N * 2).collect()
}
benchmark.run()
/**
model name : Westmere E56xx/L56xx/X56xx (Nehalem-C)
collect limit: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative
-------------------------------------------------------------------------------------------
collect limit 1 million 833 / 1284 1.3 794.4 1.0X
collect limit 2 millions 3348 / 4005 0.3 3193.3 0.2X
*/
}
}
|