|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <benchmark/benchmark.h> |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <cstdint> |
| 24 | +#include <memory> |
| 25 | +#include <string> |
| 26 | +#include <string_view> |
| 27 | + |
| 28 | +#include "core/block/block.h" |
| 29 | +#include "core/column/column_const.h" |
| 30 | +#include "core/column/column_string.h" |
| 31 | +#include "core/column/column_varbinary.h" |
| 32 | +#include "core/data_type/data_type_string.h" |
| 33 | +#include "core/data_type/data_type_varbinary.h" |
| 34 | +#include "exprs/function/simple_function_factory.h" |
| 35 | +#include "exprs/function_context.h" |
| 36 | +#include "util/defer_op.h" |
| 37 | + |
| 38 | +namespace doris { |
| 39 | +namespace { |
| 40 | + |
| 41 | +struct CharacterEncodingData { |
| 42 | + std::string input; |
| 43 | + std::string expected; |
| 44 | + std::string charset; |
| 45 | +}; |
| 46 | + |
| 47 | +template <int Scenario> |
| 48 | +CharacterEncodingData make_character_encoding_data(size_t length) { |
| 49 | + CharacterEncodingData data; |
| 50 | + auto& [input, expected, charset] = data; |
| 51 | + if constexpr (Scenario == 0) { |
| 52 | + charset = "UTF-16BE"; |
| 53 | + input.assign(length, 'A'); |
| 54 | + for (size_t i = 0; i < length; ++i) { |
| 55 | + expected.append("\0A", 2); |
| 56 | + } |
| 57 | + } else if constexpr (Scenario == 1) { |
| 58 | + charset = "ISO-8859-1"; |
| 59 | + input.assign(length, '\xE9'); |
| 60 | + for (size_t i = 0; i < length; ++i) { |
| 61 | + expected += "é"; |
| 62 | + } |
| 63 | + } else if constexpr (Scenario == 2) { |
| 64 | + charset = "UTF-8"; |
| 65 | + input.assign(length - 7, 'A'); |
| 66 | + input += "中😀"; |
| 67 | + expected = input; |
| 68 | + } else if constexpr (Scenario == 3) { |
| 69 | + charset = "UTF-16BE"; |
| 70 | + for (size_t i = 0; i < length / 2; ++i) { |
| 71 | + input += "N-"; // The UTF-16BE byte pair for 中. |
| 72 | + expected += "中"; |
| 73 | + } |
| 74 | + } else { |
| 75 | + charset = "US-ASCII"; |
| 76 | + input.assign(length, 'A'); |
| 77 | + expected = input; |
| 78 | + } |
| 79 | + return data; |
| 80 | +} |
| 81 | + |
| 82 | +// Benchmark actual block execution, including result allocation and converter setup, not just |
| 83 | +// ICU calls. Inputs are materialized columns so constant folding cannot eliminate conversion. |
| 84 | +// The charset is constant by contract. Large rows use smaller blocks to bound working memory. |
| 85 | +template <bool Encode, int Scenario> |
| 86 | +void BM_character_encoding(benchmark::State& state) { |
| 87 | + const size_t length = state.range(0); |
| 88 | + const size_t rows = std::max<size_t>(1, std::min<size_t>(4096, (1 << 20) / length)); |
| 89 | + const auto [input, expected, charset] = make_character_encoding_data<Scenario>(length); |
| 90 | + DataTypePtr string_type = std::make_shared<DataTypeString>(); |
| 91 | + DataTypePtr binary_type = std::make_shared<DataTypeVarbinary>(); |
| 92 | + DataTypePtr input_type = Encode ? string_type : binary_type; |
| 93 | + DataTypePtr result_type = Encode ? binary_type : string_type; |
| 94 | + auto values = input_type->create_column(); |
| 95 | + for (size_t i = 0; i < rows; ++i) { |
| 96 | + values->insert_data(input.data(), input.size()); |
| 97 | + } |
| 98 | + auto charsets = ColumnString::create(); |
| 99 | + charsets->insert_data(charset.data(), charset.size()); |
| 100 | + ColumnPtr charset_column = ColumnConst::create(std::move(charsets), rows); |
| 101 | + Block block {{std::move(values), input_type, "input"}, |
| 102 | + {std::move(charset_column), string_type, "charset"}}; |
| 103 | + auto function = SimpleFunctionFactory::instance().get_function( |
| 104 | + Encode ? "encode" : "decode", block.get_columns_with_type_and_name(), result_type); |
| 105 | + if (function == nullptr) { |
| 106 | + state.SkipWithError("Character encoding function not registered"); |
| 107 | + return; |
| 108 | + } |
| 109 | + auto context = FunctionContext::create_context(nullptr, result_type, {input_type, string_type}); |
| 110 | + auto status = function->open(context.get(), FunctionContext::FRAGMENT_LOCAL); |
| 111 | + if (!status.ok()) { |
| 112 | + state.SkipWithError(status.to_string()); |
| 113 | + return; |
| 114 | + } |
| 115 | + Defer close_fragment {[&] { |
| 116 | + auto close_status = function->close(context.get(), FunctionContext::FRAGMENT_LOCAL); |
| 117 | + if (!close_status.ok()) { |
| 118 | + state.SkipWithError(close_status.to_string()); |
| 119 | + } |
| 120 | + }}; |
| 121 | + status = function->open(context.get(), FunctionContext::THREAD_LOCAL); |
| 122 | + if (!status.ok()) { |
| 123 | + state.SkipWithError(status.to_string()); |
| 124 | + return; |
| 125 | + } |
| 126 | + Defer close_thread {[&] { |
| 127 | + auto close_status = function->close(context.get(), FunctionContext::THREAD_LOCAL); |
| 128 | + if (!close_status.ok()) { |
| 129 | + state.SkipWithError(close_status.to_string()); |
| 130 | + } |
| 131 | + }}; |
| 132 | + block.insert({nullptr, result_type, "result"}); |
| 133 | + |
| 134 | + // Verify every output outside the measured loop. |
| 135 | + status = function->execute(context.get(), block, {0, 1}, 2, rows); |
| 136 | + if (!status.ok()) { |
| 137 | + state.SkipWithError(status.to_string()); |
| 138 | + return; |
| 139 | + } |
| 140 | + for (size_t i = 0; i < rows; ++i) { |
| 141 | + auto actual = block.get_by_position(2).column->get_data_at(i); |
| 142 | + if (std::string_view(actual.data, actual.size) != expected) { |
| 143 | + state.SkipWithError("Character encoding result mismatch"); |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + for (auto _ : state) { |
| 148 | + status = function->execute(context.get(), block, {0, 1}, 2, rows); |
| 149 | + if (!status.ok()) { |
| 150 | + state.SkipWithError(status.to_string()); |
| 151 | + break; |
| 152 | + } |
| 153 | + benchmark::DoNotOptimize(block.get_by_position(2).column); |
| 154 | + benchmark::ClobberMemory(); |
| 155 | + } |
| 156 | + state.SetItemsProcessed(state.iterations() * rows); |
| 157 | + state.SetBytesProcessed(state.iterations() * rows * input.size()); |
| 158 | +} |
| 159 | + |
| 160 | +BENCHMARK_TEMPLATE(BM_character_encoding, true, 0) |
| 161 | + ->Name("encode_utf16be_ascii") |
| 162 | + ->ArgsProduct({{15, 63, 1023, 65535}}); |
| 163 | +BENCHMARK_TEMPLATE(BM_character_encoding, false, 1) |
| 164 | + ->Name("decode_latin1_nonascii") |
| 165 | + ->ArgsProduct({{15, 63, 1023, 65535}}); |
| 166 | +BENCHMARK_TEMPLATE(BM_character_encoding, true, 2) |
| 167 | + ->Name("encode_utf8_mixed") |
| 168 | + ->ArgsProduct({{15, 63, 1023, 65535}}); |
| 169 | +BENCHMARK_TEMPLATE(BM_character_encoding, false, 2) |
| 170 | + ->Name("decode_utf8_mixed") |
| 171 | + ->ArgsProduct({{15, 63, 1023, 65535}}); |
| 172 | +BENCHMARK_TEMPLATE(BM_character_encoding, false, 3) |
| 173 | + ->Name("decode_utf16be_cjk") |
| 174 | + ->ArgsProduct({{16, 64, 1024, 65536}}); |
| 175 | +BENCHMARK_TEMPLATE(BM_character_encoding, true, 4) |
| 176 | + ->Name("encode_ascii") |
| 177 | + ->ArgsProduct({{15, 63, 1023, 65535, 1 << 22}}); |
| 178 | + |
| 179 | +} // namespace |
| 180 | +} // namespace doris |
0 commit comments