Skip to content

Commit

Permalink
add benchmark for hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jing10.gao committed Aug 13, 2023
1 parent f6bb8c0 commit c8b401c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion be/src/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ ADD_BE_BENCH(${SRC_DIR}/bench/parquet_writer_array_bench)
#ADD_BE_BENCH(${SRC_DIR}/bench/block_cache_bench)
ADD_BE_BENCH(${SRC_DIR}/bench/roaring_bitmap_mem_bench)
ADD_BE_BENCH(${SRC_DIR}/bench/parquet_dict_decode_bench)
ADD_BE_BENCH(${SRC_DIR}/bench/get_dict_codes_bench)
ADD_BE_BENCH(${SRC_DIR}/bench/get_dict_codes_bench)
ADD_BE_BENCH(${SRC_DIR}/bench/hash_functions_bench)
91 changes: 91 additions & 0 deletions be/src/bench/hash_functions_bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 <glog/logging.h>
#include <gtest/gtest.h>
#include <testutil/assert.h>

#include <memory>
#include <vector>

#include "bench.h"
#include "exprs/hash_functions.h"

namespace starrocks {

class HashFunctionsBench {
public:
void SetUp();
void TearDown() {}

HashFunctionsBench(size_t num_column, size_t num_rows) : _num_column(num_column), _num_rows(num_rows) {}

void do_bench(benchmark::State& state, size_t num_column, bool test_default_hash);

private:
const TypeDescriptor type_desc = TypeDescriptor(TYPE_VARCHAR);
size_t _num_column = 0;
size_t _num_rows = 0;
std::vector<ColumnPtr> _columns{};
};

void HashFunctionsBench::SetUp() {
for (int i = 0; i < _num_column; i++) {
auto columnPtr = Bench::create_random_column(type_desc, _num_rows, false, false);
_columns.push_back(std::move(columnPtr));
}
}

void HashFunctionsBench::do_bench(benchmark::State& state, size_t num_rows, bool test_default_hash) {
std::unique_ptr<FunctionContext> ctx(FunctionContext::create_test_context());
if (test_default_hash) {
ColumnPtr result = HashFunctions::murmur_hash3_32(ctx.get(), _columns).value();
auto column = ColumnHelper::cast_to<TYPE_INT>(result);
} else {
ColumnPtr result = HashFunctions::xx_hash3_64(ctx.get(), _columns).value();
auto column = ColumnHelper::cast_to<TYPE_BIGINT>(result);
}
}

static void BM_HashFunctions_Eval_Arg(benchmark::internal::Benchmark* b) {
b->Args({1, 10, true});
b->Args({1, 10, false});
b->Args({1, 100, true});
b->Args({1, 100, false});
b->Args({1, 10000, true});
b->Args({1, 10000, false});
b->Args({1, 1000000, true});
b->Args({1, 1000000, false});
b->Iterations(10000);
}

static void BM_HashFunctions_Eval(benchmark::State& state) {
size_t num_column = state.range(0);
size_t num_rows = state.range(1);
bool test_default_hash = state.range(3);

HashFunctionsBench hashFunctionsBench(num_column, num_rows);
hashFunctionsBench.SetUp();

for (auto _ : state) {
hashFunctionsBench.do_bench(state, num_rows, test_default_hash);
}
}

BENCHMARK(BM_HashFunctions_Eval)->Apply(BM_HashFunctions_Eval_Arg);

} // namespace starrocks

BENCHMARK_MAIN();

0 comments on commit c8b401c

Please sign in to comment.