/
AvroReadBenchmark.scala
277 lines (230 loc) · 9.2 KB
/
AvroReadBenchmark.scala
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
/*
* 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 java.time.Instant
import scala.util.Random
import org.apache.spark.benchmark.Benchmark
import org.apache.spark.sql.{Column, DataFrame}
import org.apache.spark.sql.functions.lit
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
/**
* Benchmark to measure Avro read performance.
* {{{
* To run this benchmark:
* 1. without sbt: bin/spark-submit --class <this class>
* --jars <catalyst test jar>,<core test jar>,<spark-avro jar> <avro test jar>
* 2. build/sbt "avro/test:runMain <this class>"
* 3. generate result: SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "avro/test:runMain <this class>"
* Results will be written to "benchmarks/AvroReadBenchmark-results.txt".
* }}}
*/
object AvroReadBenchmark extends SqlBasedBenchmark {
import spark.implicits._
def withTempTable(tableNames: String*)(f: => Unit): Unit = {
try f finally tableNames.foreach(spark.catalog.dropTempView)
}
private def prepareTable(dir: File, df: DataFrame, partition: Option[String] = None): Unit = {
val dirAvro = dir.getCanonicalPath
if (partition.isDefined) {
df.write.partitionBy(partition.get).format("avro").save(dirAvro)
} else {
df.write.format("avro").save(dirAvro)
}
spark.read.format("avro").load(dirAvro).createOrReplaceTempView("avroTable")
}
def numericScanBenchmark(values: Int, dataType: DataType): Unit = {
val benchmark =
new Benchmark(s"SQL Single ${dataType.sql} Column Scan", values, output = output)
withTempPath { dir =>
withTempTable("t1", "avroTable") {
import spark.implicits._
spark.range(values).map(_ => Random.nextLong).createOrReplaceTempView("t1")
prepareTable(dir, spark.sql(s"SELECT CAST(value as ${dataType.sql}) id FROM t1"))
benchmark.addCase("Sum") { _ =>
spark.sql("SELECT sum(id) FROM avroTable").noop()
}
benchmark.run()
}
}
}
def intStringScanBenchmark(values: Int): Unit = {
val benchmark = new Benchmark("Int and String Scan", values, output = output)
withTempPath { dir =>
withTempTable("t1", "avroTable") {
import spark.implicits._
spark.range(values).map(_ => Random.nextLong).createOrReplaceTempView("t1")
prepareTable(
dir,
spark.sql("SELECT CAST(value AS INT) AS c1, CAST(value as STRING) AS c2 FROM t1"))
benchmark.addCase("Sum of columns") { _ =>
spark.sql("SELECT sum(c1), sum(length(c2)) FROM avroTable").noop()
}
benchmark.run()
}
}
}
def partitionTableScanBenchmark(values: Int): Unit = {
val benchmark = new Benchmark("Partitioned Table", values, output = output)
withTempPath { dir =>
withTempTable("t1", "avroTable") {
import spark.implicits._
spark.range(values).map(_ => Random.nextLong).createOrReplaceTempView("t1")
prepareTable(dir, spark.sql("SELECT value % 2 AS p, value AS id FROM t1"), Some("p"))
benchmark.addCase("Data column") { _ =>
spark.sql("SELECT sum(id) FROM avroTable").noop()
}
benchmark.addCase("Partition column") { _ =>
spark.sql("SELECT sum(p) FROM avroTable").noop()
}
benchmark.addCase("Both columns") { _ =>
spark.sql("SELECT sum(p), sum(id) FROM avroTable").noop()
}
benchmark.run()
}
}
}
def repeatedStringScanBenchmark(values: Int): Unit = {
val benchmark = new Benchmark("Repeated String", values, output = output)
withTempPath { dir =>
withTempTable("t1", "avroTable") {
spark.range(values).createOrReplaceTempView("t1")
prepareTable(dir, spark.sql("SELECT CAST((id % 200) + 10000 as STRING) AS c1 FROM t1"))
benchmark.addCase("Sum of string length") { _ =>
spark.sql("SELECT sum(length(c1)) FROM avroTable").noop()
}
benchmark.run()
}
}
}
def stringWithNullsScanBenchmark(values: Int, fractionOfNulls: Double): Unit = {
withTempPath { dir =>
withTempTable("t1", "avroTable") {
spark.range(values).createOrReplaceTempView("t1")
prepareTable(
dir,
spark.sql(
s"SELECT IF(RAND(1) < $fractionOfNulls, NULL, CAST(id as STRING)) AS c1, " +
s"IF(RAND(2) < $fractionOfNulls, NULL, CAST(id as STRING)) AS c2 FROM t1"))
val percentageOfNulls = fractionOfNulls * 100
val benchmark =
new Benchmark(s"String with Nulls Scan ($percentageOfNulls%)", values, output = output)
benchmark.addCase("Sum of string length") { _ =>
spark.sql("SELECT SUM(LENGTH(c2)) FROM avroTable " +
"WHERE c1 IS NOT NULL AND c2 IS NOT NULL").noop()
}
benchmark.run()
}
}
}
def columnsBenchmark(values: Int, width: Int): Unit = {
val benchmark =
new Benchmark(s"Single Column Scan from $width columns", values, output = output)
withTempPath { dir =>
withTempTable("t1", "avroTable") {
import spark.implicits._
val middle = width / 2
val selectExpr = (1 to width).map(i => s"value as c$i")
spark.range(values).map(_ => Random.nextLong).toDF()
.selectExpr(selectExpr: _*).createOrReplaceTempView("t1")
prepareTable(dir, spark.sql("SELECT * FROM t1"))
benchmark.addCase("Sum of single column") { _ =>
spark.sql(s"SELECT sum(c$middle) FROM avroTable").noop()
}
benchmark.run()
}
}
}
private def filtersPushdownBenchmark(rowsNum: Int, numIters: Int): Unit = {
val benchmark = new Benchmark("Filters pushdown", rowsNum, output = output)
val colsNum = 100
val fields = Seq.tabulate(colsNum)(i => StructField(s"col$i", TimestampType))
val schema = StructType(StructField("key", LongType) +: fields)
def columns(): Seq[Column] = {
val ts = Seq.tabulate(colsNum) { i =>
lit(Instant.ofEpochSecond(-30610224000L + i * 123456)).as(s"col$i")
}
($"id" % 1000).as("key") +: ts
}
withTempPath { path =>
// Write and read timestamp in the LEGACY mode to make timestamp conversions more expensive
withSQLConf(SQLConf.AVRO_REBASE_MODE_IN_WRITE.key -> "LEGACY") {
spark.range(rowsNum).select(columns(): _*)
.write
.format("avro")
.save(path.getAbsolutePath)
}
def readback = {
spark.read
.schema(schema)
.format("avro")
.load(path.getAbsolutePath)
}
benchmark.addCase("w/o filters", numIters) { _ =>
withSQLConf(SQLConf.AVRO_REBASE_MODE_IN_READ.key -> "LEGACY") {
readback.noop()
}
}
def withFilter(configEnabled: Boolean): Unit = {
withSQLConf(
SQLConf.AVRO_REBASE_MODE_IN_READ.key -> "LEGACY",
SQLConf.AVRO_FILTER_PUSHDOWN_ENABLED.key -> configEnabled.toString()) {
readback.filter($"key" === 0).noop()
}
}
benchmark.addCase("pushdown disabled", numIters) { _ =>
withSQLConf(SQLConf.AVRO_REBASE_MODE_IN_READ.key -> "LEGACY") {
withFilter(configEnabled = false)
}
}
benchmark.addCase("w/ filters", numIters) { _ =>
withFilter(configEnabled = true)
}
benchmark.run()
}
}
override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
runBenchmark("SQL Single Numeric Column Scan") {
Seq(ByteType, ShortType, IntegerType, LongType, FloatType, DoubleType).foreach { dataType =>
numericScanBenchmark(1024 * 1024 * 15, dataType)
}
}
runBenchmark("Int and String Scan") {
intStringScanBenchmark(1024 * 1024 * 10)
}
runBenchmark("Partitioned Table Scan") {
partitionTableScanBenchmark(1024 * 1024 * 15)
}
runBenchmark("Repeated String Scan") {
repeatedStringScanBenchmark(1024 * 1024 * 10)
}
runBenchmark("String with Nulls Scan") {
for (fractionOfNulls <- List(0.0, 0.50, 0.95)) {
stringWithNullsScanBenchmark(1024 * 1024 * 10, fractionOfNulls)
}
}
runBenchmark("Single Column Scan From Wide Columns") {
columnsBenchmark(1024 * 1024 * 1, 100)
columnsBenchmark(1024 * 1024 * 1, 200)
columnsBenchmark(1024 * 1024 * 1, 300)
}
// Benchmark pushdown filters that refer to top-level columns.
// TODO (SPARK-32328): Add benchmarks for filters with nested column attributes.
filtersPushdownBenchmark(rowsNum = 1000 * 1000, numIters = 3)
}
}