|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <folly/Benchmark.h> |
| 17 | +#include <folly/init/Init.h> |
| 18 | + |
| 19 | +#include "velox/benchmarks/ExpressionBenchmarkBuilder.h" |
| 20 | +#include "velox/functions/lib/benchmarks/FunctionBenchmarkBase.h" |
| 21 | +#include "velox/functions/prestosql/registration/RegistrationFunctions.h" |
| 22 | + |
| 23 | +using namespace facebook::velox; |
| 24 | +using namespace facebook::velox::exec; |
| 25 | +using namespace facebook::velox::functions; |
| 26 | + |
| 27 | +int main(int argc, char** argv) { |
| 28 | + folly::Init init(&argc, &argv); |
| 29 | + |
| 30 | + functions::prestosql::registerArrayFunctions(); |
| 31 | + |
| 32 | + ExpressionBenchmarkBuilder benchmarkBuilder; |
| 33 | + |
| 34 | + auto* pool = benchmarkBuilder.pool(); |
| 35 | + auto& vm = benchmarkBuilder.vectorMaker(); |
| 36 | + |
| 37 | + auto createSet = |
| 38 | + [&](const TypePtr& type, bool withNulls, const VectorPtr& constantInput) { |
| 39 | + VectorFuzzer::Options options; |
| 40 | + options.vectorSize = 1'000; |
| 41 | + options.nullRatio = withNulls ? 0.2 : 0.0; |
| 42 | + |
| 43 | + VectorFuzzer fuzzer(options, pool); |
| 44 | + std::vector<VectorPtr> columns; |
| 45 | + columns.push_back(fuzzer.fuzzFlat(type)); |
| 46 | + columns.push_back(fuzzer.fuzzFlat(type)); |
| 47 | + columns.push_back(fuzzer.fuzzFlat(type)); |
| 48 | + columns.push_back( |
| 49 | + BaseVector::createNullConstant(type, options.vectorSize, pool)); |
| 50 | + columns.push_back( |
| 51 | + BaseVector::wrapInConstant(options.vectorSize, 0, constantInput)); |
| 52 | + |
| 53 | + auto input = vm.rowVector({"c0", "c1", "c2", "n", "c"}, columns); |
| 54 | + |
| 55 | + benchmarkBuilder |
| 56 | + .addBenchmarkSet( |
| 57 | + fmt::format( |
| 58 | + "array_constructor_{}_{}", |
| 59 | + mapTypeKindToName(type->kind()), |
| 60 | + withNulls ? "nulls" : "nullfree"), |
| 61 | + input) |
| 62 | + .addExpression("1", "array_constructor(c0)") |
| 63 | + .addExpression("2", "array_constructor(c0, c1)") |
| 64 | + .addExpression("3", "array_constructor(c0, c1, c2)") |
| 65 | + .addExpression("2_null", "array_constructor(c0, c1, n)") |
| 66 | + .addExpression("2_const", "array_constructor(c0, c1, c)"); |
| 67 | + }; |
| 68 | + |
| 69 | + auto constantInteger = BaseVector::createConstant(INTEGER(), 11, 1, pool); |
| 70 | + createSet(INTEGER(), true, constantInteger); |
| 71 | + createSet(INTEGER(), false, constantInteger); |
| 72 | + |
| 73 | + auto constantRow = vm.rowVector({ |
| 74 | + BaseVector::createConstant(INTEGER(), 11, 1, pool), |
| 75 | + BaseVector::createConstant(DOUBLE(), 1.23, 1, pool), |
| 76 | + }); |
| 77 | + createSet(ROW({INTEGER(), DOUBLE()}), true, constantRow); |
| 78 | + createSet(ROW({INTEGER(), DOUBLE()}), false, constantRow); |
| 79 | + |
| 80 | + auto constantArray = vm.arrayVector<int32_t>({{1, 2, 3, 4, 5}}); |
| 81 | + createSet(ARRAY(INTEGER()), true, constantArray); |
| 82 | + createSet(ARRAY(INTEGER()), false, constantArray); |
| 83 | + |
| 84 | + auto constantMap = vm.mapVector<int32_t, float>({{{1, 1.23}, {2, 2.34}}}); |
| 85 | + createSet(MAP(INTEGER(), REAL()), true, constantMap); |
| 86 | + createSet(MAP(INTEGER(), REAL()), false, constantMap); |
| 87 | + |
| 88 | + benchmarkBuilder.registerBenchmarks(); |
| 89 | + |
| 90 | + folly::runBenchmarks(); |
| 91 | + return 0; |
| 92 | +} |
0 commit comments