|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 <gmock/gmock.h> |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include "dwio/nimble/velox/SchemaBuilder.h" |
| 20 | +#include "dwio/nimble/velox/StreamData.h" |
| 21 | +#include "dwio/nimble/velox/VeloxWriterUtils.h" |
| 22 | +#include "velox/common/memory/Memory.h" |
| 23 | +#include "velox/common/memory/MemoryArbitrator.h" |
| 24 | +#include "velox/common/memory/SharedArbitrator.h" |
| 25 | + |
| 26 | +using namespace ::testing; |
| 27 | + |
| 28 | +namespace facebook::nimble { |
| 29 | + |
| 30 | +class VeloxWriterUtilsTest : public ::testing::Test { |
| 31 | + protected: |
| 32 | + static void SetUpTestCase() { |
| 33 | + velox::memory::SharedArbitrator::registerFactory(); |
| 34 | + velox::memory::MemoryManager::Options options; |
| 35 | + options.arbitratorKind = "SHARED"; |
| 36 | + velox::memory::MemoryManager::testingSetInstance(options); |
| 37 | + } |
| 38 | + |
| 39 | + void SetUp() override { |
| 40 | + rootPool_ = velox::memory::memoryManager()->addRootPool("default_root"); |
| 41 | + leafPool_ = rootPool_->addLeafChild("default_leaf"); |
| 42 | + schemaBuilder_ = std::make_unique<SchemaBuilder>(); |
| 43 | + inputBufferGrowthPolicy_ = |
| 44 | + DefaultInputBufferGrowthPolicy::withDefaultRanges(); |
| 45 | + } |
| 46 | + |
| 47 | + std::shared_ptr<velox::memory::MemoryPool> rootPool_; |
| 48 | + std::shared_ptr<velox::memory::MemoryPool> leafPool_; |
| 49 | + std::unique_ptr<SchemaBuilder> schemaBuilder_; |
| 50 | + std::unique_ptr<InputBufferGrowthPolicy> inputBufferGrowthPolicy_; |
| 51 | +}; |
| 52 | + |
| 53 | +TEST_F(VeloxWriterUtilsTest, getStreamIndicesByMemoryUsageTest) { |
| 54 | + // Test 1: Empty streams vector |
| 55 | + { |
| 56 | + std::vector<std::unique_ptr<StreamData>> streams; |
| 57 | + auto result = getStreamIndicesByMemoryUsage(streams); |
| 58 | + EXPECT_TRUE(result.empty()); |
| 59 | + } |
| 60 | + |
| 61 | + // Test 2: Single stream |
| 62 | + { |
| 63 | + std::vector<std::unique_ptr<StreamData>> streams; |
| 64 | + auto scalarTypeBuilder = |
| 65 | + schemaBuilder_->createScalarTypeBuilder(ScalarKind::Int32); |
| 66 | + auto descriptor = &scalarTypeBuilder->scalarDescriptor(); |
| 67 | + streams.push_back( |
| 68 | + std::make_unique<ContentStreamData<int32_t>>( |
| 69 | + *leafPool_, *descriptor, *inputBufferGrowthPolicy_)); |
| 70 | + |
| 71 | + auto result = getStreamIndicesByMemoryUsage(streams); |
| 72 | + EXPECT_THAT(result, ElementsAre(0)); |
| 73 | + } |
| 74 | + |
| 75 | + // Test 3: Multiple streams with different memory usage |
| 76 | + // Should be sorted in descending order by memory usage |
| 77 | + { |
| 78 | + std::vector<std::unique_ptr<StreamData>> streams; |
| 79 | + auto scalarTypeBuilder = |
| 80 | + schemaBuilder_->createScalarTypeBuilder(ScalarKind::Int32); |
| 81 | + auto descriptor = &scalarTypeBuilder->scalarDescriptor(); |
| 82 | + |
| 83 | + // Stream 0: Small memory usage (empty) |
| 84 | + streams.push_back( |
| 85 | + std::make_unique<ContentStreamData<int32_t>>( |
| 86 | + *leafPool_, *descriptor, *inputBufferGrowthPolicy_)); |
| 87 | + |
| 88 | + // Stream 1: Medium memory usage |
| 89 | + auto stream1 = std::make_unique<ContentStreamData<int32_t>>( |
| 90 | + *leafPool_, *descriptor, *inputBufferGrowthPolicy_); |
| 91 | + std::vector<int32_t> data1 = {1, 2, 3, 4, 5}; |
| 92 | + auto& mutableData1 = stream1->mutableData(); |
| 93 | + for (const auto& item : data1) { |
| 94 | + mutableData1.push_back(item); |
| 95 | + } |
| 96 | + streams.push_back(std::move(stream1)); |
| 97 | + |
| 98 | + // Stream 2: Large memory usage |
| 99 | + auto stream2 = std::make_unique<ContentStreamData<int32_t>>( |
| 100 | + *leafPool_, *descriptor, *inputBufferGrowthPolicy_); |
| 101 | + std::vector<int32_t> data2(1000, 42); |
| 102 | + auto& mutableData2 = stream2->mutableData(); |
| 103 | + for (const auto& item : data2) { |
| 104 | + mutableData2.push_back(item); |
| 105 | + } |
| 106 | + streams.push_back(std::move(stream2)); |
| 107 | + |
| 108 | + // Stream 3: Large memory usage. Should map to same bucket as stream 2 |
| 109 | + auto stream3 = std::make_unique<ContentStreamData<int32_t>>( |
| 110 | + *leafPool_, *descriptor, *inputBufferGrowthPolicy_); |
| 111 | + std::vector<int32_t> data3(900, 42); |
| 112 | + auto& mutableData3 = stream3->mutableData(); |
| 113 | + for (const auto& item : data3) { |
| 114 | + mutableData3.push_back(item); |
| 115 | + } |
| 116 | + streams.push_back(std::move(stream3)); |
| 117 | + |
| 118 | + auto result = getStreamIndicesByMemoryUsage(streams); |
| 119 | + |
| 120 | + // Stream 2 should come first (largest), then stream 1, then stream 0 |
| 121 | + EXPECT_THAT(result, ElementsAre(2, 3, 1, 0)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace facebook::nimble |
0 commit comments