westonpace commented on a change in pull request #9095:
URL: https://github.com/apache/arrow/pull/9095#discussion_r564736538



##########
File path: cpp/src/arrow/csv/reader_benchmark.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 "benchmark/benchmark.h"
+
+#include <string>
+
+#include "arrow/buffer.h"
+#include "arrow/csv/options.h"
+#include "arrow/csv/reader.h"
+#include "arrow/csv/test_common.h"
+#include "arrow/io/interfaces.h"
+#include "arrow/io/memory.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/thread_pool.h"
+
+namespace arrow {
+namespace csv {
+
+class SlowInputStream : public io::InputStream {
+ public:
+  explicit SlowInputStream(std::shared_ptr<io::BufferReader> target, int64_t 
latency_ms)
+      : target_(std::move(target)) {
+    latency_s_ = static_cast<double>(latency_ms) / 1000.0;
+  }
+  virtual ~SlowInputStream() {}
+
+  Result<util::string_view> Peek(int64_t nbytes) override {
+    return target_->Peek(nbytes);
+  }
+  bool supports_zero_copy() const override { return 
target_->supports_zero_copy(); }
+  Status Close() override { return target_->Close(); }
+  Status Abort() override { return target_->Abort(); }
+  Result<int64_t> Tell() const override { return target_->Tell(); }
+  bool closed() const override { return target_->closed(); }
+  Result<int64_t> Read(int64_t nbytes, void* out) override {
+    if (latency_s_ > 0) {
+      SleepFor(latency_s_);
+    }
+    return target_->Read(nbytes, out);
+  }
+  Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) override {
+    if (latency_s_ > 0) {
+      SleepFor(latency_s_);
+    }
+    return target_->Read(nbytes);
+  }
+  Status Seek(int64_t pos) { return target_->Seek(pos); }
+
+ private:
+  std::shared_ptr<io::BufferReader> target_;
+  double latency_s_;
+};
+
+static ReadOptions CreateReadOptions(bool use_threads, bool use_async) {
+  auto result = csv::ReadOptions::Defaults();
+  result.use_threads = use_threads;
+  result.legacy_blocking_reads = !use_async;
+  // Simulate larger files by using smaller block files so the impact of 
multiple
+  // blocks is seen but we don't have to spend the time waiting on the large 
I/O
+  result.block_size = (1 << 20) / 100;
+  return result;
+}
+
+static std::shared_ptr<SlowInputStream> 
CreateStreamReader(std::shared_ptr<Buffer> buffer,
+                                                           int64_t latency_ms) 
{
+  auto buffer_reader = std::make_shared<io::BufferReader>(buffer);
+  return std::make_shared<SlowInputStream>(buffer_reader, latency_ms);
+}
+
+static void BenchmarkReader(benchmark::State& state, bool use_threads, bool 
use_async) {
+  auto latency_ms = state.range(0);
+  auto num_rows = state.range(1);
+  auto num_files = state.range(2);
+  if (num_files > 5 && use_threads && !use_async) {
+    state.SkipWithError("Would deadlock");
+  }
+  auto input_buffer = *MakeSampleCsvBuffer(num_rows);
+  // Hard coding # of threads so we don't deadlock if there are too few cores

Review comment:
       I don't think we want to include the benchmark at the end of the day.  I 
created it originally to be able to demonstrate the nested deadlock case 
encountered by the dataset API.  I wrote it for my own benefit to both confirm 
that the async table reader was avoiding the deadlock and show that it can 
outperform the threaded table reader since threads aren't wasted blocking on 
I/O.  So it is mimicking the dataset API in two ways that don't make sense for 
a benchmark and, somewhat intentionally, cause the threaded table reader to 
underperform.
   
   First, the table reader is running inside a thread pool thread for the 
threaded/serial case.  This simulates the way the dataset API currently burns a 
thread waiting for the threaded table reader in much the same way.
   
   Second, we are fixing the thread pool to size 6.
   
   Eliminating that "waiter thread task" would prevent the deadlock entirely.  
With that in there however, as soon as you have as many waiters (one per file) 
as you have threads in your pool then you will deadlock.
   
   Thus, this benchmark is probably more of a temporary demo than it is a 
benchmark.  In the future, once the dataset logic has moved over to async, we 
can put in a real benchmark showing the actual gain.  I will rearrange the 
commits so that this demo/benchmark is a separate branch built on top of the 
10183 branch that can be optionally checked out.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to