github-actions[bot] commented on code in PR #62493: URL: https://github.com/apache/doris/pull/62493#discussion_r3083935712
########## be/benchmark/benchmark_binary_dict_page.hpp: ########## @@ -0,0 +1,143 @@ +// 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. + +#pragma once + +#include <benchmark/benchmark.h> + +#include <algorithm> +#include <memory> +#include <random> +#include <string> +#include <vector> + +#include "common/status.h" +#include "storage/segment/binary_dict_page.h" +#include "storage/segment/options.h" +#include "storage/segment/page_builder.h" +#include "util/slice.h" + +namespace doris::segment_v2 { + +inline std::vector<std::string> bdp_gen_strings(size_t num_unique, size_t avg_len, + uint32_t seed) { + std::mt19937 rng(seed); + size_t lo = std::max<size_t>(1, avg_len / 2); + size_t hi = avg_len + avg_len / 2; + std::uniform_int_distribution<size_t> len_dist(lo, hi); + std::uniform_int_distribution<int> ch_dist('a', 'z'); + std::vector<std::string> out; + out.reserve(num_unique); + for (size_t i = 0; i < num_unique; ++i) { + size_t len = len_dist(rng); + std::string s; + s.reserve(len); + for (size_t j = 0; j < len; ++j) { + s.push_back(static_cast<char>(ch_dist(rng))); + } + out.push_back(std::move(s)); + } + return out; +} + +// Benchmark BinaryDictPageBuilder::add with an input stream that has an +// exact target hit rate. +// +// range(0) = target hit rate in percent (0..100) +// range(1) = average string length (bytes) +// range(2) = batch size per add() call +// +// The input is built as row[i] = dict[i % num_unique], where +// num_unique = kTotalRows * (100 - hit_rate_pct) / 100 +// giving exactly num_unique misses followed by (kTotalRows - num_unique) +// hits for a deterministic hit rate. +static void BM_BinaryDictPageBuilder_Add(benchmark::State& state) { + const size_t hit_rate_pct = static_cast<size_t>(state.range(0)); + const size_t avg_len = static_cast<size_t>(state.range(1)); + const size_t batch = static_cast<size_t>(state.range(2)); + constexpr size_t kTotalRows = 4096; + + size_t num_unique = kTotalRows - (kTotalRows * hit_rate_pct / 100); + if (num_unique == 0) { + num_unique = 1; + } + + auto dict = bdp_gen_strings(num_unique, avg_len, /*seed=*/12345); + + std::vector<Slice> slices; + slices.reserve(kTotalRows); + for (size_t i = 0; i < kTotalRows; ++i) { + const std::string& s = dict[i % num_unique]; + slices.emplace_back(s.data(), s.size()); + } + + PageBuilderOptions opts; + + int64_t items = 0; + for (auto _ : state) { + state.PauseTiming(); + PageBuilder* raw = nullptr; + Status st = BinaryDictPageBuilder::create(&raw, opts); + if (!st.ok()) { + state.SkipWithError(st.to_string().c_str()); + break; + } + std::unique_ptr<PageBuilder> builder(raw); + state.ResumeTiming(); + + size_t off = 0; + while (off < slices.size()) { + size_t cnt = std::min(batch, slices.size() - off); + size_t added = cnt; + Status add_st = builder->add( + reinterpret_cast<const uint8_t*>(slices.data() + off), &added); + if (!add_st.ok()) { + state.SkipWithError(add_st.to_string().c_str()); + break; + } + if (added == 0) { + break; + } + off += added; + } + benchmark::DoNotOptimize(builder); + + state.PauseTiming(); + OwnedSlice tmp; + (void)builder->finish(&tmp); Review Comment: `builder->finish(&tmp)` returns a `Status`, but this new benchmark drops it with `(void)`. That violates the BE common review rule that every `Status` must be checked, and it also means a failed finish path would be silently reported as a successful benchmark iteration. Please handle it like the surrounding `create()` / `add()` calls, e.g. `if (Status st = builder->finish(&tmp); !st.ok()) { state.SkipWithError(st.to_string().c_str()); break; }`. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
