diff --git a/benchmark/graph.rb b/benchmark/graph.rb index 9d6f3cd..d2f40e7 100755 --- a/benchmark/graph.rb +++ b/benchmark/graph.rb @@ -26,14 +26,20 @@ "integer", "string", ] +types = [ + "insert", + "select", +] benchmarks.each do |benchmark| base_dir = File.join(__dir__, benchmark) - data = CSV.read(File.join(base_dir, "result.csv"), - headers: true, - converters: :all) - plotter = Charty.bar_plot(data: data, - x: "N records", - y: "Elapsed time (sec)", - color: "Approach") - plotter.save(File.join(base_dir, "result.svg")) + types.each do |type| + data = CSV.read(File.join(base_dir, "#{type}.csv"), + headers: true, + converters: :all) + plotter = Charty.bar_plot(data: data, + x: "N records", + y: "Elapsed time (sec)", + color: "Approach") + plotter.save(File.join(base_dir, "#{type}.svg")) + end end diff --git a/benchmark/insert-copy.cc b/benchmark/insert-copy.cc new file mode 100644 index 0000000..d2f8165 --- /dev/null +++ b/benchmark/insert-copy.cc @@ -0,0 +1,152 @@ +/* + * 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. + */ + +#include +#include +#include +#include + +#include + +class ConnectionFinisher { + public: + ConnectionFinisher(PGconn* connection) : connection_(connection) {} + ~ConnectionFinisher() { PQfinish(connection_); } + + private: + PGconn* connection_; +}; + +class ResultClearner { + public: + ResultClearner(PGresult* result) : result_(result) {} + ~ResultClearner() { PQclear(result_); } + + private: + PGresult* result_; +}; + +int +main(int argc, char** argv) +{ + std::string connectionString; + if (!std::getenv("PGDATABASE")) + { + connectionString += "dbname=afs_benchmark"; + } + auto connection = PQconnectdb(connectionString.c_str()); + ConnectionFinisher connectionFinisher(connection); + if (PQstatus(connection) != CONNECTION_OK) + { + std::cerr << "failed to connect: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + + { + auto result = PQexec(connection, "DROP TABLE IF EXISTS data_insert"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to drop: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + } + + { + auto result = PQexec(connection, "CREATE TABLE data_insert (LIKE data)"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to create: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + } + + std::vector buffers; + { + auto result = PQexec(connection, "COPY data TO STDOUT (FORMAT binary)"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COPY_OUT) + { + std::cerr << "failed to copy to: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + while (true) + { + char* data; + auto size = PQgetCopyData(connection, &data, 0); + if (size == -1) + { + break; + } + if (size == -2) + { + std::cerr << "failed to read copy data: " << PQerrorMessage(connection) + << std::endl; + return EXIT_FAILURE; + } + buffers.emplace_back(data, size); + free(data); + } + } + auto before = std::chrono::steady_clock::now(); + { + auto result = PQexec(connection, "COPY data_insert FROM STDOUT (FORMAT binary)"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COPY_IN) + { + std::cerr << "failed to copy from: " << PQerrorMessage(connection) + << std::endl; + return EXIT_FAILURE; + } + for (const auto& buffer : buffers) + { + auto copyDataResult = PQputCopyData(connection, buffer.data(), buffer.size()); + if (copyDataResult == -1) + { + std::cerr << "failed to put copy data: " << PQerrorMessage(connection) + << std::endl; + return EXIT_FAILURE; + } + } + auto copyEndResult = PQputCopyEnd(connection, nullptr); + if (copyEndResult == -1) + { + std::cerr << "failed to end copy data: " << PQerrorMessage(connection) + << std::endl; + return EXIT_FAILURE; + } + } + { + auto result = PQgetResult(connection); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to copy from: " << PQerrorMessage(connection) + << std::endl; + return EXIT_FAILURE; + } + } + auto elapsedTime = std::chrono::duration_cast( + std::chrono::steady_clock::now() - before) + .count(); + printf("%.3f\n", elapsedTime / 1000.0); + + return EXIT_SUCCESS; +} diff --git a/benchmark/insert-flight-sql.rb b/benchmark/insert-flight-sql.rb new file mode 100755 index 0000000..e8d291a --- /dev/null +++ b/benchmark/insert-flight-sql.rb @@ -0,0 +1,54 @@ +#!/usr/bin/env ruby +# +# 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. + +require "time" + +require "arrow-flight-sql" + +call_options = ArrowFlight::CallOptions.new +call_options.add_header("x-flight-sql-database", + ENV["PGDATABASE"] || "afs_benchmark") +client = ArrowFlight::Client.new("grpc://127.0.0.1:15432") +client.authenticate_basic(ENV["PGUSER"] || ENV["USER"], + ENV["PGPASSWORD"] || "", + call_options) +sql_client = ArrowFlightSQL::Client.new(client) + +sql_client.execute_update("DROP TABLE IF EXISTS data_insert", call_options) +sql_client.execute_update("CREATE TABLE data_insert (LIKE data)", call_options) + +info = sql_client.execute("SELECT * FROM data", call_options) +endpoint = info.endpoints.first +reader = sql_client.do_get(endpoint.ticket, call_options) +record_batches = [] +loop do + record_batch = reader.read_next + break if record_batch.nil? + record_batches << record_batch.data +end + +before = Time.now +sql_client.prepare("INSERT INTO data_insert VALUES ($1)", + call_options) do |statement| + record_batches.each do |record_batch| + statement.record_batch = record_batch + statement.execute_update(call_options) + end +end +puts("%.3f" % (Time.now - before)) diff --git a/benchmark/insert.cc b/benchmark/insert.cc new file mode 100644 index 0000000..7eb58f5 --- /dev/null +++ b/benchmark/insert.cc @@ -0,0 +1,139 @@ +/* + * 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. + */ + +#include +#include +#include + +#include + +#include + +class ConnectionFinisher { + public: + ConnectionFinisher(PGconn* connection) : connection_(connection) {} + ~ConnectionFinisher() { PQfinish(connection_); } + + private: + PGconn* connection_; +}; + +class ResultClearner { + public: + ResultClearner(PGresult* result) : result_(result) {} + ~ResultClearner() { PQclear(result_); } + + private: + PGresult* result_; +}; + +int +main(int argc, char** argv) +{ + std::string connectionString; + if (!std::getenv("PGDATABASE")) + { + connectionString += "dbname=afs_benchmark"; + } + auto connection = PQconnectdb(connectionString.c_str()); + ConnectionFinisher connectionFinisher(connection); + if (PQstatus(connection) != CONNECTION_OK) + { + std::cerr << "failed to connect: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + + { + auto result = PQexec(connection, "DROP TABLE IF EXISTS data_insert"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to drop: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + } + + { + auto result = PQexec(connection, "CREATE TABLE data_insert (LIKE data)"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to create: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + } + + std::string insert = "INSERT INTO data_insert VALUES "; + { + auto result = PQexec(connection, "SELECT * FROM data"); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_TUPLES_OK) + { + std::cerr << "failed to select: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + auto nTuples = PQntuples(result); + auto nFields = PQnfields(result); + for (int iTuple = 0; iTuple < nTuples; iTuple++) + { + if (iTuple > 0) + { + insert += ", "; + } + for (int iField = 0; iField < nFields; iField++) + { + if (PQgetisnull(result, iTuple, iField)) + { + insert += "(null)"; + } + else + { + insert += "("; + auto type = PQftype(result, iField); + if (type == TEXTOID) + { + insert += "'"; + } + insert += PQgetvalue(result, iTuple, iField); + if (type == TEXTOID) + { + insert += "'"; + } + insert += ")"; + } + } + } + } + auto before = std::chrono::steady_clock::now(); + { + auto result = PQexec(connection, insert.c_str()); + ResultClearner resultClearner(result); + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + std::cerr << "failed to insert: " << PQerrorMessage(connection) << std::endl; + return EXIT_FAILURE; + } + } + auto elapsedTime = std::chrono::duration_cast( + std::chrono::steady_clock::now() - before) + .count(); + printf("%.3f\n", elapsedTime / 1000.0); + + return EXIT_SUCCESS; +} diff --git a/benchmark/integer/README.md b/benchmark/integer/README.md index 3777f78..60e7a7c 100644 --- a/benchmark/integer/README.md +++ b/benchmark/integer/README.md @@ -23,7 +23,7 @@ See the [README.md in the parent directory](../README.md). -## Result +## Environment Here is a benchmark result on the following environment: @@ -38,22 +38,48 @@ Here is a benchmark result on the following environment: 0.2.0 (not released yet) 14df9b5fe61eda2d71bdfbf67c61a227741f616c -![Graph](result.svg) +## Results + +### `SELECT` + +![Graph](select.svg) 100K records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 0.015 | 0.015 | 0.012 | +| 0.014 | 0.015 | 0.011 | 1M records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 0.111 | 0.148 | 0.114 | +| 0.082 | 0.145 | 0.110 | 10M records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 0.958 | 1.502 | 1.162 | +| 0.694 | 1.486 | 1.142 | + +### `INSERT` + +![Graph](insert.svg) + +100K records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 0.273 | 0.121 | 0.020 | + +1M records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 6.055 | 2.232 | 0.345 | + +10M records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 51.757 | 31.390 | 7.832 | diff --git a/benchmark/integer/insert.csv b/benchmark/integer/insert.csv new file mode 100644 index 0000000..068c12c --- /dev/null +++ b/benchmark/integer/insert.csv @@ -0,0 +1,10 @@ +Approach,N records,Elapsed time (sec) +Apache Arrow Flight SQL,100000,0.273 +INSERT,100000,0.121 +COPY,100000,0.020 +Apache Arrow Flight SQL,1000000,6.055 +INSERT,1000000,2.232 +COPY,1000000,0.345 +Apache Arrow Flight SQL,10000000,51.757 +INSERT,10000000,31.390 +COPY,10000000,7.832 diff --git a/benchmark/integer/insert.svg b/benchmark/integer/insert.svg new file mode 100644 index 0000000..edae65a --- /dev/null +++ b/benchmark/integer/insert.svg @@ -0,0 +1,1225 @@ + + + + + + + + 2023-11-16T18:51:19.434953 + image/svg+xml + + + Matplotlib v3.6.3, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/benchmark/integer/result.csv b/benchmark/integer/result.csv deleted file mode 100644 index bb27a19..0000000 --- a/benchmark/integer/result.csv +++ /dev/null @@ -1,10 +0,0 @@ -Approach,N records,Elapsed time (sec) -Apache Arrow Flight SQL,100000,0.015 -SELECT,100000,0.015 -COPY,100000,0.012 -Apache Arrow Flight SQL,1000000,0.111 -SELECT,1000000,0.148 -COPY,1000000,0.114 -Apache Arrow Flight SQL,10000000,0.958 -SELECT,10000000,1.502 -COPY,10000000,1.162 diff --git a/benchmark/integer/select.csv b/benchmark/integer/select.csv new file mode 100644 index 0000000..90fec54 --- /dev/null +++ b/benchmark/integer/select.csv @@ -0,0 +1,10 @@ +Approach,N records,Elapsed time (sec) +Apache Arrow Flight SQL,100000,0.014 +SELECT,100000,0.015 +COPY,100000,0.011 +Apache Arrow Flight SQL,1000000,0.082 +SELECT,1000000,0.145 +COPY,1000000,0.110 +Apache Arrow Flight SQL,10000000,0.694 +SELECT,10000000,1.486 +COPY,10000000,1.142 diff --git a/benchmark/integer/result.svg b/benchmark/integer/select.svg similarity index 90% rename from benchmark/integer/result.svg rename to benchmark/integer/select.svg index f8ed487..8703293 100644 --- a/benchmark/integer/result.svg +++ b/benchmark/integer/select.svg @@ -6,7 +6,7 @@ - 2023-11-13T18:19:07.890284 + 2023-11-16T18:51:19.944464 image/svg+xml @@ -40,42 +40,42 @@ z +" clip-path="url(#p3c234d4b8c)" style="fill: #1f77b4"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #1f77b4"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #1f77b4"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #ff7f0e"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #ff7f0e"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #ff7f0e"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #2ca02c"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #2ca02c"/> +" clip-path="url(#p3c234d4b8c)" style="fill: #2ca02c"/> - - + @@ -173,7 +173,7 @@ z - + @@ -192,7 +192,7 @@ z - + @@ -385,12 +385,12 @@ z - - + @@ -414,12 +414,12 @@ z - + - + - + - + - + - + - + - + - + - + @@ -609,12 +609,12 @@ z - + - + @@ -624,12 +624,12 @@ z - + - + @@ -834,31 +834,31 @@ z - + - + - + - + - + - + - + - + - + + diff --git a/benchmark/markdown.rb b/benchmark/markdown.rb index e4225e4..f06ad17 100755 --- a/benchmark/markdown.rb +++ b/benchmark/markdown.rb @@ -24,6 +24,11 @@ "string", ] +types = [ + "select", + "insert", +] + def format_n_records(n_records) if n_records < 1_000_000 "%dK" % (n_records / 1_000.0) @@ -34,44 +39,51 @@ def format_n_records(n_records) benchmarks.each do |benchmark| base_dir = File.join(__dir__, benchmark) - data = CSV.read(File.join(base_dir, "result.csv"), - headers: true, - converters: :all) readme_path = File.join(base_dir, "README.md") - graph_markdown = "![Graph](result.svg)" + results_header = "## Results" readme = File.read(readme_path) - readme_before_graph_markdown = - readme.split(/^#{Regexp.escape(graph_markdown)}$/, 2)[0] - records_per_n = data.each.group_by do |row| - row["N records"] - end - result = "" - records_per_n.each do |n_records, rows| - result << "\n" - result << "#{format_n_records(n_records)} records:\n" - result << "\n" - approaches = rows.collect do |row| - approach = row["Approach"] - if approach == "Apache Arrow Flight SQL" - approach - else - "`#{approach}`" - end + readme_before_results_header = + readme.split(/^#{Regexp.escape(results_header)}$/, 2)[0] + results = "" + types.each do |type| + results << "### `#{type.upcase}`\n" + results << "\n" + results << "![Graph](#{type}.svg)" + results << "\n" + data = CSV.read(File.join(base_dir, "#{type}.csv"), + headers: true, + converters: :all) + records_per_n = data.each.group_by do |row| + row["N records"] end - result << ("| " + approaches.join(" | ") + " |\n") - separators = approaches.collect {|approach| "-" * approach.size} - result << ("| " + separators.join(" | ") + " |\n") - formatted_elapsed = approaches.zip(rows).collect do |approach, row| - width = approach.size - ("%.3f" % row["Elapsed time (sec)"]).ljust(width) + records_per_n.each do |n_records, rows| + results << "\n" + results << "#{format_n_records(n_records)} records:\n" + results << "\n" + approaches = rows.collect do |row| + approach = row["Approach"] + if approach == "Apache Arrow Flight SQL" + approach + else + "`#{approach}`" + end + end + results << ("| " + approaches.join(" | ") + " |\n") + separators = approaches.collect {|approach| "-" * approach.size} + results << ("| " + separators.join(" | ") + " |\n") + formatted_elapsed = approaches.zip(rows).collect do |approach, row| + width = approach.size + ("%.3f" % row["Elapsed time (sec)"]).ljust(width) + end + results << ("| " + formatted_elapsed.join(" | ") + " |\n") end - result << ("| " + formatted_elapsed.join(" | ") + " |\n") + results << "\n" end File.write(readme_path, <<-README) -#{readme_before_graph_markdown.strip} +#{readme_before_results_header.strip} -#{graph_markdown} +#{results_header} -#{result.strip} +#{results.strip} README end diff --git a/benchmark/meson.build b/benchmark/meson.build index dfb06a0..1f07151 100644 --- a/benchmark/meson.build +++ b/benchmark/meson.build @@ -16,5 +16,8 @@ # under the License. libpq = dependency('libpq') -executable('copy', 'copy.c', dependencies: [libpq, postgresql]) + +executable('insert', 'insert.cc', dependencies: [libpq, postgresql]) +executable('insert-copy', 'insert-copy.cc', dependencies: [libpq, postgresql]) executable('select', 'select.c', dependencies: [libpq, postgresql]) +executable('select-copy', 'select-copy.c', dependencies: [libpq, postgresql]) diff --git a/benchmark/run.sh b/benchmark/run.sh index eaf8baa..0e6ced9 100755 --- a/benchmark/run.sh +++ b/benchmark/run.sh @@ -35,8 +35,11 @@ benchmarks+=(integer) benchmarks+=(string) for benchmark in "${benchmarks[@]}"; do - result="${base_dir}/${benchmark}/result.csv" - echo "Approach,N records,Elapsed time (sec)" | tee "${result}" + select_result="${base_dir}/${benchmark}/select.csv" + insert_result="${base_dir}/${benchmark}/insert.csv" + echo "Approach,N records,Elapsed time (sec)" | \ + tee "${select_result}" \ + "${insert_result}" sizes=() sizes+=(100000) sizes+=(1000000) @@ -47,16 +50,30 @@ for benchmark in "${benchmarks[@]}"; do "${base_dir}/${benchmark}/prepare-sql.sh" "${size}" "${PGDATABASE}" | \ psql -d postgres - echo "${benchmark}: ${size}: Apache Arrow Flight SQL" - elapsed_time=$(measure "${base_dir}/select.rb") - echo "Apache Arrow Flight SQL,${size},${elapsed_time}" | tee -a "${result}" + echo "${benchmark}: select: ${size}: Apache Arrow Flight SQL" + elapsed_time=$(measure "${base_dir}/select-flight-sql.rb") + echo "Apache Arrow Flight SQL,${size},${elapsed_time}" | \ + tee -a "${select_result}" - echo "${benchmark}: ${size}: SELECT" + echo "${benchmark}: select: ${size}: SELECT" elapsed_time=$(measure benchmark/select) - echo "SELECT,${size},${elapsed_time}" | tee -a "${result}" + echo "SELECT,${size},${elapsed_time}" | tee -a "${select_result}" - echo "${benchmark}: ${size}: COPY" - elapsed_time=$(measure benchmark/copy) - echo "COPY,${size},${elapsed_time}" | tee -a "${result}" + echo "${benchmark}: select: ${size}: COPY" + elapsed_time=$(measure benchmark/select-copy) + echo "COPY,${size},${elapsed_time}" | tee -a "${select_result}" + + echo "${benchmark}: insert: ${size}: Apache Arrow Flight SQL" + elapsed_time=$(measure "${base_dir}/insert-flight-sql.rb") + echo "Apache Arrow Flight SQL,${size},${elapsed_time}" | \ + tee -a "${insert_result}" + + echo "${benchmark}: insert: ${size}: INSERT" + elapsed_time=$(measure benchmark/insert) + echo "INSERT,${size},${elapsed_time}" | tee -a "${insert_result}" + + echo "${benchmark}: insert: ${size}: COPY" + elapsed_time=$(measure benchmark/insert-copy) + echo "COPY,${size},${elapsed_time}" | tee -a "${insert_result}" done done diff --git a/benchmark/copy.c b/benchmark/select-copy.c similarity index 100% rename from benchmark/copy.c rename to benchmark/select-copy.c diff --git a/benchmark/select.rb b/benchmark/select-flight-sql.rb similarity index 100% rename from benchmark/select.rb rename to benchmark/select-flight-sql.rb diff --git a/benchmark/string/README.md b/benchmark/string/README.md index 473f7c4..d0f2594 100644 --- a/benchmark/string/README.md +++ b/benchmark/string/README.md @@ -23,7 +23,7 @@ See the [`README.md` in the parent directory](../README.md). -## Result +## Environment Here is a benchmark result on the following environment: @@ -38,22 +38,48 @@ Here is a benchmark result on the following environment: 0.2.0 (not released yet) 14df9b5fe61eda2d71bdfbf67c61a227741f616c -![Graph](result.svg) +## Results + +### `SELECT` + +![Graph](select.svg) 100K records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 0.047 | 0.017 | 0.017 | +| 0.041 | 0.016 | 0.016 | 1M records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 0.512 | 0.155 | 0.153 | +| 0.353 | 0.149 | 0.148 | 10M records: | Apache Arrow Flight SQL | `SELECT` | `COPY` | | ----------------------- | -------- | ------ | -| 2.951 | 1.706 | 1.640 | +| 2.154 | 1.924 | 1.596 | + +### `INSERT` + +![Graph](insert.svg) + +100K records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 0.343 | 0.338 | 0.416 | + +1M records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 6.906 | 5.730 | 2.870 | + +10M records: + +| Apache Arrow Flight SQL | `INSERT` | `COPY` | +| ----------------------- | -------- | ------ | +| 116.579 | 107.045 | 66.410 | diff --git a/benchmark/string/insert.csv b/benchmark/string/insert.csv new file mode 100644 index 0000000..73f8bb7 --- /dev/null +++ b/benchmark/string/insert.csv @@ -0,0 +1,10 @@ +Approach,N records,Elapsed time (sec) +Apache Arrow Flight SQL,100000,0.343 +INSERT,100000,0.338 +COPY,100000,0.416 +Apache Arrow Flight SQL,1000000,6.906 +INSERT,1000000,5.730 +COPY,1000000,2.870 +Apache Arrow Flight SQL,10000000,116.579 +INSERT,10000000,107.045 +COPY,10000000,66.410 diff --git a/benchmark/string/insert.svg b/benchmark/string/insert.svg new file mode 100644 index 0000000..cb8e05a --- /dev/null +++ b/benchmark/string/insert.svg @@ -0,0 +1,1253 @@ + + + + + + + + 2023-11-16T18:51:20.038826 + image/svg+xml + + + Matplotlib v3.6.3, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/benchmark/string/result.csv b/benchmark/string/result.csv deleted file mode 100644 index 3a5a181..0000000 --- a/benchmark/string/result.csv +++ /dev/null @@ -1,10 +0,0 @@ -Approach,N records,Elapsed time (sec) -Apache Arrow Flight SQL,100000,0.047 -SELECT,100000,0.017 -COPY,100000,0.017 -Apache Arrow Flight SQL,1000000,0.512 -SELECT,1000000,0.155 -COPY,1000000,0.153 -Apache Arrow Flight SQL,10000000,2.951 -SELECT,10000000,1.706 -COPY,10000000,1.640 diff --git a/benchmark/string/select.csv b/benchmark/string/select.csv new file mode 100644 index 0000000..8053df9 --- /dev/null +++ b/benchmark/string/select.csv @@ -0,0 +1,10 @@ +Approach,N records,Elapsed time (sec) +Apache Arrow Flight SQL,100000,0.041 +SELECT,100000,0.016 +COPY,100000,0.016 +Apache Arrow Flight SQL,1000000,0.353 +SELECT,1000000,0.149 +COPY,1000000,0.148 +Apache Arrow Flight SQL,10000000,2.154 +SELECT,10000000,1.924 +COPY,10000000,1.596 diff --git a/benchmark/string/result.svg b/benchmark/string/select.svg similarity index 86% rename from benchmark/string/result.svg rename to benchmark/string/select.svg index 590670f..e937061 100644 --- a/benchmark/string/result.svg +++ b/benchmark/string/select.svg @@ -6,7 +6,7 @@ - 2023-11-13T18:19:08.495288 + 2023-11-16T18:51:20.134370 image/svg+xml @@ -40,18 +40,18 @@ z +" clip-path="url(#p27f446f3c3)" style="fill: #1f77b4"/> +" clip-path="url(#p27f446f3c3)" style="fill: #1f77b4"/> +" clip-path="url(#p27f446f3c3)" style="fill: #1f77b4"/> +" clip-path="url(#p27f446f3c3)" style="fill: #ff7f0e"/> +" clip-path="url(#p27f446f3c3)" style="fill: #ff7f0e"/> +" clip-path="url(#p27f446f3c3)" style="fill: #ff7f0e"/> +" clip-path="url(#p27f446f3c3)" style="fill: #2ca02c"/> +" clip-path="url(#p27f446f3c3)" style="fill: #2ca02c"/> +" clip-path="url(#p27f446f3c3)" style="fill: #2ca02c"/> - - + @@ -173,7 +173,7 @@ z - + @@ -192,7 +192,7 @@ z - + @@ -385,12 +385,12 @@ z - - + @@ -414,12 +414,12 @@ z - + - + - + - + @@ -471,12 +471,12 @@ z - + - + @@ -486,12 +486,12 @@ z - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -785,32 +721,32 @@ z + + + + + + - + - + - + - + - + - + - - - - - - - + - + @@ -904,7 +840,7 @@ L 66.6 65.748563 z " style="fill: #1f77b4"/> - + @@ -1071,7 +1007,7 @@ L 66.6 80.426688 z " style="fill: #ff7f0e"/> - + @@ -1124,7 +1060,7 @@ L 66.6 95.104813 z " style="fill: #2ca02c"/> - + @@ -1193,7 +1129,7 @@ z - + diff --git a/dev/release/rat_exclude_files.txt b/dev/release/rat_exclude_files.txt index 61c7482..101343b 100644 --- a/dev/release/rat_exclude_files.txt +++ b/dev/release/rat_exclude_files.txt @@ -15,8 +15,8 @@ # specific language governing permissions and limitations # under the License. -benchmark/*/result.csv -benchmark/*/result.svg +benchmark/*/*.csv +benchmark/*/*.svg compile_commands.json dev/release/apache-rat-*.jar dev/release/filtered_rat.txt diff --git a/src/afs.cc b/src/afs.cc index 23940b9..867596c 100644 --- a/src/afs.cc +++ b/src/afs.cc @@ -1493,10 +1493,6 @@ class Executor : public WorkerProcessor { // TODO: Customizable. SharedRingBuffer::allocate_data( &(session->bufferData), area_, 1L * 1024L * 1024L); - - SetCurrentStatementStartTimestamp(); - SPI_connect(); - needFinish_ = true; } session->initialized = true; localSession_->valid = true; @@ -1884,8 +1880,9 @@ class Executor : public WorkerProcessor { ScopedSnapshot scopedSnapshot; SetCurrentStatementStartTimestamp(); - auto result = SPI_execute(query.c_str(), true, 0); + SPI_connect(); needFinish_ = true; + auto result = SPI_execute(query.c_str(), true, 0); if (result > 0) { pgstat_report_activity( @@ -2059,8 +2056,9 @@ class Executor : public WorkerProcessor { ScopedSnapshot scopedSnapshot; SetCurrentStatementStartTimestamp(); - auto result = SPI_execute(query.c_str(), false, 0); + SPI_connect(); needFinish_ = true; + auto result = SPI_execute(query.c_str(), false, 0); if (result > 0) { session->nUpdatedRecords = SPI_processed; @@ -2237,6 +2235,9 @@ class Executor : public WorkerProcessor { ScopedTransaction scopedTransaction; ScopedSnapshot scopedSnapshot; + SetCurrentStatementStartTimestamp(); + SPI_connect(); + needFinish_ = true; struct Data { Executor* executor; const char* tag; @@ -2247,7 +2248,6 @@ class Executor : public WorkerProcessor { return d->executor->write_record_batches(d->tag); }, &data); - needFinish_ = true; if (!status.ok()) { set_error_message(session, @@ -2282,9 +2282,11 @@ class Executor : public WorkerProcessor { ScopedTransaction scopedTransaction; ScopedSnapshot scopedSnapshot; + SetCurrentStatementStartTimestamp(); + SPI_connect(); + needFinish_ = true; auto input = std::make_shared(this, localSession_); auto n_updated_records_result = preparedStatement->update(input); - needFinish_ = true; if (n_updated_records_result.ok()) { session->nUpdatedRecords = *n_updated_records_result;