|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.execution.datasources.csv |
| 18 | + |
| 19 | +import java.io.File |
| 20 | + |
| 21 | +import org.apache.spark.SparkConf |
| 22 | +import org.apache.spark.sql.{Column, Row, SparkSession} |
| 23 | +import org.apache.spark.sql.functions.lit |
| 24 | +import org.apache.spark.sql.types._ |
| 25 | +import org.apache.spark.util.{Benchmark, Utils} |
| 26 | + |
| 27 | +/** |
| 28 | + * Benchmark to measure CSV read/write performance. |
| 29 | + * To run this: |
| 30 | + * spark-submit --class <this class> --jars <spark sql test jar> |
| 31 | + */ |
| 32 | +object CSVBenchmarks { |
| 33 | + val conf = new SparkConf() |
| 34 | + |
| 35 | + val spark = SparkSession.builder |
| 36 | + .master("local[1]") |
| 37 | + .appName("benchmark-csv-datasource") |
| 38 | + .config(conf) |
| 39 | + .getOrCreate() |
| 40 | + import spark.implicits._ |
| 41 | + |
| 42 | + def withTempPath(f: File => Unit): Unit = { |
| 43 | + val path = Utils.createTempDir() |
| 44 | + path.delete() |
| 45 | + try f(path) finally Utils.deleteRecursively(path) |
| 46 | + } |
| 47 | + |
| 48 | + def quotedValuesBenchmark(rowsNum: Int, numIters: Int): Unit = { |
| 49 | + val benchmark = new Benchmark(s"Parsing quoted values", rowsNum) |
| 50 | + |
| 51 | + withTempPath { path => |
| 52 | + val str = (0 until 10000).map(i => s""""$i"""").mkString(",") |
| 53 | + |
| 54 | + spark.range(rowsNum) |
| 55 | + .map(_ => str) |
| 56 | + .write.option("header", true) |
| 57 | + .csv(path.getAbsolutePath) |
| 58 | + |
| 59 | + val schema = new StructType().add("value", StringType) |
| 60 | + val ds = spark.read.option("header", true).schema(schema).csv(path.getAbsolutePath) |
| 61 | + |
| 62 | + benchmark.addCase(s"One quoted string", numIters) { _ => |
| 63 | + ds.filter((_: Row) => true).count() |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz |
| 68 | +
|
| 69 | + Parsing quoted values: Best/Avg Time(ms) Rate(M/s) Per Row(ns) Relative |
| 70 | + -------------------------------------------------------------------------------------------- |
| 71 | + One quoted string 38884 / 39065 0.0 777683.0 1.0X |
| 72 | + */ |
| 73 | + benchmark.run() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + def main(args: Array[String]): Unit = { |
| 78 | + quotedValuesBenchmark(rowsNum = 50 * 1000, numIters = 3) |
| 79 | + } |
| 80 | +} |
0 commit comments