westonpace commented on code in PR #13366: URL: https://github.com/apache/arrow/pull/13366#discussion_r909068110
########## cpp/src/arrow/compute/exec/filter_benchmark.cc: ########## @@ -0,0 +1,175 @@ +// 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 <condition_variable> +#include <mutex> + +#include "benchmark/benchmark.h" + +#include "arrow/compute/cast.h" +#include "arrow/compute/exec.h" +#include "arrow/compute/exec/expression.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/task_util.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/dataset/partition.h" +#include "arrow/record_batch.h" +#include "arrow/testing/future_util.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/type.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +static constexpr int64_t kTotalBatchSize = 1000000; +constexpr auto kSeed = 0x94378165; + +/* + Will return batches of size length, with fields as specified. + null_probability controls the likelihood that an element within the batch is null, + across all fields. bool_true_probability controls the likelihood that an element + belonging to a boolean field is true. +*/ +static std::shared_ptr<arrow::RecordBatch> GetBatchesWithNullProbability( + const FieldVector& fields, int64_t length, double null_probability, + double bool_true_probability = 0.5) { + std::vector<std::shared_ptr<Array>> arrays(fields.size()); + auto rand = random::RandomArrayGenerator(kSeed); + for (size_t i = 0; i < fields.size(); i++) { + const auto& field = fields[i]; + if (field.get()->name() == "bool") { + arrays[i] = rand.Boolean(length, bool_true_probability, null_probability); + } else { + arrays[i] = rand.ArrayOf(field.get()->type(), length, null_probability); + } + } + return RecordBatch::Make(schema(fields), length, std::move(arrays)); +} + +BatchesWithSchema MakeRandomBatchesWithNullProbability( + std::shared_ptr<Schema> schema, int num_batches, int batch_size, + double null_probability = 0.5, double bool_true_probability = 0.5) { + BatchesWithSchema out; + out.batches.resize(num_batches); + + for (int i = 0; i < num_batches; ++i) { + out.batches[i] = ExecBatch(*GetBatchesWithNullProbability( + schema->fields(), batch_size, null_probability, bool_true_probability)); + out.batches[i].values.emplace_back(i); + } + out.schema = schema; + return out; +} + +static void FilterOverhead(benchmark::State& state, std::vector<Expression> expr_vector) { + const int32_t batch_size = static_cast<int32_t>(state.range(0)); + const double null_prob = state.range(1) / 100.0; + const double bool_true_probability = state.range(2) / 100.0; + const int32_t num_batches = kTotalBatchSize / batch_size; + + arrow::compute::BatchesWithSchema data = MakeRandomBatchesWithNullProbability( + schema({field("i64", int64()), field("bool", boolean())}), num_batches, batch_size, + null_prob, bool_true_probability); + ExecContext ctx(default_memory_pool(), arrow::internal::GetCpuThreadPool()); + std::vector<arrow::compute::Declaration> filter_node_dec; + for (Expression expr : expr_vector) { + filter_node_dec.push_back({"filter", FilterNodeOptions(expr)}); + } + BenchmarkNodeOverhead(state, ctx, num_batches, batch_size, data, filter_node_dec); +} + +static void FilterOverheadIsolated(benchmark::State& state, Expression expr) { + ExecContext ctx(default_memory_pool(), arrow::internal::GetCpuThreadPool()); + const int32_t batch_size = static_cast<int32_t>(state.range(0)); + const int32_t num_batches = kTotalBatchSize / batch_size; + arrow::compute::BatchesWithSchema data = MakeRandomBatches( + schema({field("i64", int64()), field("bool", boolean())}), num_batches, batch_size); + FilterNodeOptions options = FilterNodeOptions{expr}; + BenchmarkIsolatedNodeOverhead(state, ctx, expr, num_batches, batch_size, data, "filter", + options); +} + +arrow::compute::Expression complex_expression = + less(less(field_ref("i64"), literal(20)), greater(field_ref("i64"), literal(0))); +arrow::compute::Expression simple_expression = + less(call("negate", {field_ref("i64")}), literal(0)); +arrow::compute::Expression ref_only_expression = field_ref("bool"); + +arrow::compute::Expression is_not_null_expression = + not_(is_null(field_ref("bool"), false)); +arrow::compute::Expression is_true_expression = equal(field_ref("bool"), literal(true)); +arrow::compute::Expression is_not_null_and_true_expression = + and_(is_not_null_expression, is_true_expression); + +void SetArgs(benchmark::internal::Benchmark* bench) { + for (int batch_size = 1000; batch_size <= kTotalBatchSize; batch_size *= 10) { + bench->ArgNames({"batch_size", "null_prob", "bool_true_prob"}) + ->Args({batch_size, 0, 50}) + ->UseRealTime(); + } +} + +void SelectivityArgs(benchmark::internal::Benchmark* bench) { Review Comment: Nit: `SetSelectivityArgs` ########## cpp/src/arrow/compute/exec/filter_benchmark.cc: ########## @@ -0,0 +1,175 @@ +// 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 <condition_variable> +#include <mutex> + +#include "benchmark/benchmark.h" + +#include "arrow/compute/cast.h" +#include "arrow/compute/exec.h" +#include "arrow/compute/exec/expression.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/task_util.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/dataset/partition.h" +#include "arrow/record_batch.h" +#include "arrow/testing/future_util.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/type.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +static constexpr int64_t kTotalBatchSize = 1000000; +constexpr auto kSeed = 0x94378165; Review Comment: These should probably both be static or neither be static. For a benchmark file I don't think it really matters (so I'd probably just drop the `static`). ########## cpp/src/arrow/compute/exec/filter_benchmark.cc: ########## @@ -0,0 +1,175 @@ +// 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 <condition_variable> +#include <mutex> + +#include "benchmark/benchmark.h" + +#include "arrow/compute/cast.h" +#include "arrow/compute/exec.h" +#include "arrow/compute/exec/expression.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/task_util.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/dataset/partition.h" +#include "arrow/record_batch.h" +#include "arrow/testing/future_util.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/type.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +static constexpr int64_t kTotalBatchSize = 1000000; +constexpr auto kSeed = 0x94378165; + +/* + Will return batches of size length, with fields as specified. + null_probability controls the likelihood that an element within the batch is null, + across all fields. bool_true_probability controls the likelihood that an element + belonging to a boolean field is true. +*/ +static std::shared_ptr<arrow::RecordBatch> GetBatchesWithNullProbability( + const FieldVector& fields, int64_t length, double null_probability, + double bool_true_probability = 0.5) { + std::vector<std::shared_ptr<Array>> arrays(fields.size()); + auto rand = random::RandomArrayGenerator(kSeed); + for (size_t i = 0; i < fields.size(); i++) { + const auto& field = fields[i]; + if (field.get()->name() == "bool") { + arrays[i] = rand.Boolean(length, bool_true_probability, null_probability); + } else { + arrays[i] = rand.ArrayOf(field.get()->type(), length, null_probability); + } + } + return RecordBatch::Make(schema(fields), length, std::move(arrays)); +} + +BatchesWithSchema MakeRandomBatchesWithNullProbability( + std::shared_ptr<Schema> schema, int num_batches, int batch_size, + double null_probability = 0.5, double bool_true_probability = 0.5) { + BatchesWithSchema out; + out.batches.resize(num_batches); + + for (int i = 0; i < num_batches; ++i) { + out.batches[i] = ExecBatch(*GetBatchesWithNullProbability( + schema->fields(), batch_size, null_probability, bool_true_probability)); + out.batches[i].values.emplace_back(i); + } + out.schema = schema; Review Comment: ```suggestion out.schema = std::move(schema); ``` ########## cpp/src/arrow/util/benchmark_util.h: ########## @@ -135,4 +149,114 @@ struct RegressionArgs { bool size_is_bytes_; }; -} // namespace arrow +/* + Generates batches from data, then benchmark rows_per_second and batches_per_second for + an isolated node. We do this by passing in batches through a task scheduler, and calling + InputFinished and InputReceived. +*/ Review Comment: Prefer `//` over `/*` ########## cpp/src/arrow/compute/exec/filter_benchmark.cc: ########## @@ -0,0 +1,175 @@ +// 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 <condition_variable> +#include <mutex> + +#include "benchmark/benchmark.h" + +#include "arrow/compute/cast.h" +#include "arrow/compute/exec.h" +#include "arrow/compute/exec/expression.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/task_util.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/dataset/partition.h" +#include "arrow/record_batch.h" +#include "arrow/testing/future_util.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/type.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +static constexpr int64_t kTotalBatchSize = 1000000; +constexpr auto kSeed = 0x94378165; + +/* + Will return batches of size length, with fields as specified. + null_probability controls the likelihood that an element within the batch is null, + across all fields. bool_true_probability controls the likelihood that an element + belonging to a boolean field is true. +*/ Review Comment: Can you use `//` for internal comments ########## cpp/src/arrow/util/benchmark_util.h: ########## @@ -135,4 +149,114 @@ struct RegressionArgs { bool size_is_bytes_; }; -} // namespace arrow +/* + Generates batches from data, then benchmark rows_per_second and batches_per_second for + an isolated node. We do this by passing in batches through a task scheduler, and calling + InputFinished and InputReceived. +*/ +void BenchmarkIsolatedNodeOverhead(benchmark::State& state, + arrow::compute::ExecContext ctx, + arrow::compute::Expression expr, int32_t num_batches, + int32_t batch_size, + arrow::compute::BatchesWithSchema data, + std::string factory_name, + arrow::compute::ExecNodeOptions& options) { + for (auto _ : state) { + state.PauseTiming(); + AsyncGenerator<util::optional<arrow::compute::ExecBatch>> sink_gen; + + ASSERT_OK_AND_ASSIGN(std::shared_ptr<arrow::compute::ExecPlan> plan, + arrow::compute::ExecPlan::Make(&ctx)); + // Source and sink nodes have no effect on the benchmark. + // Used for dummy purposes as they are referenced in InputReceived and InputFinished. + ASSERT_OK_AND_ASSIGN(arrow::compute::ExecNode * source_node, + MakeExecNode("source", plan.get(), {}, + arrow::compute::SourceNodeOptions{ + data.schema, data.gen(/*parallel=*/true, + /*slow=*/false)})); + + ASSERT_OK_AND_ASSIGN(arrow::compute::ExecNode * node, + MakeExecNode(factory_name, plan.get(), {source_node}, options)); + MakeExecNode("sink", plan.get(), {node}, arrow::compute::SinkNodeOptions{&sink_gen}); + + std::unique_ptr<arrow::compute::TaskScheduler> scheduler = + arrow::compute::TaskScheduler::Make(); + std::condition_variable all_tasks_finished_cv; + std::mutex mutex; + + int task_group_id = scheduler->RegisterTaskGroup( + [&](size_t thread_id, int64_t task_id) { + node->InputReceived(source_node, data.batches[task_id]); + return Status::OK(); + }, + [&](size_t thread_id) { + node->InputFinished(source_node, static_cast<int>(data.batches.size())); + std::unique_lock<std::mutex> lk(mutex); + all_tasks_finished_cv.notify_one(); + return Status::OK(); + }); + scheduler->RegisterEnd(); + + arrow::compute::ThreadIndexer thread_indexer; + + state.ResumeTiming(); + arrow::internal::ThreadPool* thread_pool = arrow::internal::GetCpuThreadPool(); + ASSERT_OK(scheduler->StartScheduling( + thread_indexer(), + [&](std::function<Status(size_t)> task) -> Status { + return thread_pool->Spawn([&, task]() { + size_t tid = thread_indexer(); + ARROW_DCHECK_OK(task(tid)); + }); + }, + thread_pool->GetCapacity(), + /*use_sync_execution=*/false)); + std::unique_lock<std::mutex> lk(mutex); + ASSERT_OK(scheduler->StartTaskGroup(thread_indexer(), task_group_id, num_batches)); + all_tasks_finished_cv.wait(lk); + ASSERT_TRUE(node->finished().is_finished()); + } + state.counters["rows_per_second"] = benchmark::Counter( + static_cast<double>(state.iterations() * num_batches * batch_size), + benchmark::Counter::kIsRate); + + state.counters["batches_per_second"] = benchmark::Counter( + static_cast<double>(state.iterations() * num_batches), benchmark::Counter::kIsRate); +} + +/* Review Comment: Prefer `//` over `/*` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
