test_resource_mgr.cpp 6.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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.

Y
youny626 已提交
18 19
#include <gtest/gtest.h>
#include "scheduler/ResourceMgr.h"
20 21
#include "scheduler/resource/CpuResource.h"
#include "scheduler/resource/DiskResource.h"
Y
youny626 已提交
22
#include "scheduler/resource/GpuResource.h"
23 24
#include "scheduler/resource/TestResource.h"
#include "scheduler/task/TestTask.h"
W
wxyu 已提交
25
#include "scheduler/tasklabel/DefaultLabel.h"
S
starlord 已提交
26

27
namespace milvus {
W
wxyu 已提交
28
namespace scheduler {
29 30 31

/************ ResourceMgrBaseTest ************/
class ResourceMgrBaseTest : public testing::Test {
S
starlord 已提交
32
 protected:
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    void
    SetUp() override {
        empty_mgr_ = std::make_shared<ResourceMgr>();
        mgr1_ = std::make_shared<ResourceMgr>();
        disk_res = std::make_shared<DiskResource>("disk", 0, true, false);
        cpu_res = std::make_shared<CpuResource>("cpu", 1, true, false);
        gpu_res = std::make_shared<GpuResource>("gpu", 2, true, true);
        mgr1_->Add(ResourcePtr(disk_res));
        mgr1_->Add(ResourcePtr(cpu_res));
        mgr1_->Add(ResourcePtr(gpu_res));
    }

    void
    TearDown() override {
    }

    ResourceMgrPtr empty_mgr_;
    ResourceMgrPtr mgr1_;
    ResourcePtr disk_res;
    ResourcePtr cpu_res;
    ResourcePtr gpu_res;
};

S
starlord 已提交
56
TEST_F(ResourceMgrBaseTest, ADD) {
57 58 59 60 61
    auto resource = std::make_shared<TestResource>("test", 0, true, true);
    auto ret = empty_mgr_->Add(ResourcePtr(resource));
    ASSERT_EQ(ret.lock(), resource);
}

S
starlord 已提交
62
TEST_F(ResourceMgrBaseTest, ADD_DISK) {
63 64 65 66 67
    auto resource = std::make_shared<DiskResource>("disk", 0, true, true);
    auto ret = empty_mgr_->Add(ResourcePtr(resource));
    ASSERT_EQ(ret.lock(), resource);
}

S
starlord 已提交
68
TEST_F(ResourceMgrBaseTest, CONNECT) {
69 70 71 72 73 74 75 76
    auto resource1 = std::make_shared<TestResource>("resource1", 0, true, true);
    auto resource2 = std::make_shared<TestResource>("resource2", 2, true, true);
    empty_mgr_->Add(resource1);
    empty_mgr_->Add(resource2);
    Connection io("io", 500.0);
    ASSERT_TRUE(empty_mgr_->Connect("resource1", "resource2", io));
}

S
starlord 已提交
77
TEST_F(ResourceMgrBaseTest, INVALID_CONNECT) {
78 79 80 81 82 83 84 85
    auto resource1 = std::make_shared<TestResource>("resource1", 0, true, true);
    auto resource2 = std::make_shared<TestResource>("resource2", 2, true, true);
    empty_mgr_->Add(resource1);
    empty_mgr_->Add(resource2);
    Connection io("io", 500.0);
    ASSERT_FALSE(empty_mgr_->Connect("xx", "yy", io));
}

S
starlord 已提交
86
TEST_F(ResourceMgrBaseTest, CLEAR) {
87 88 89 90 91
    ASSERT_EQ(mgr1_->GetNumOfResource(), 3);
    mgr1_->Clear();
    ASSERT_EQ(mgr1_->GetNumOfResource(), 0);
}

S
starlord 已提交
92
TEST_F(ResourceMgrBaseTest, GET_DISK_RESOURCES) {
93 94 95 96 97
    auto disks = mgr1_->GetDiskResources();
    ASSERT_EQ(disks.size(), 1);
    ASSERT_EQ(disks[0].lock(), disk_res);
}

S
starlord 已提交
98
TEST_F(ResourceMgrBaseTest, GET_ALL_RESOURCES) {
99 100 101
    bool disk = false, cpu = false, gpu = false;
    auto resources = mgr1_->GetAllResources();
    ASSERT_EQ(resources.size(), 3);
Y
youny626 已提交
102 103 104 105 106 107 108
    for (auto& res : resources) {
        if (res->type() == ResourceType::DISK)
            disk = true;
        if (res->type() == ResourceType::CPU)
            cpu = true;
        if (res->type() == ResourceType::GPU)
            gpu = true;
109 110 111 112 113 114 115
    }

    ASSERT_TRUE(disk);
    ASSERT_TRUE(cpu);
    ASSERT_TRUE(gpu);
}

S
starlord 已提交
116
TEST_F(ResourceMgrBaseTest, GET_COMPUTE_RESOURCES) {
117 118 119 120 121
    auto compute_resources = mgr1_->GetComputeResources();
    ASSERT_EQ(compute_resources.size(), 1);
    ASSERT_EQ(compute_resources[0], gpu_res);
}

S
starlord 已提交
122
TEST_F(ResourceMgrBaseTest, GET_RESOURCE_BY_TYPE_AND_DEVICEID) {
123 124 125 126 127 128 129
    auto cpu = mgr1_->GetResource(ResourceType::CPU, 1);
    ASSERT_EQ(cpu, cpu_res);

    auto invalid = mgr1_->GetResource(ResourceType::GPU, 1);
    ASSERT_EQ(invalid, nullptr);
}

S
starlord 已提交
130
TEST_F(ResourceMgrBaseTest, GET_RESOURCE_BY_NAME) {
131 132 133 134 135 136 137
    auto disk = mgr1_->GetResource("disk");
    ASSERT_EQ(disk, disk_res);

    auto invalid = mgr1_->GetResource("invalid");
    ASSERT_EQ(invalid, nullptr);
}

S
starlord 已提交
138
TEST_F(ResourceMgrBaseTest, GET_NUM_OF_RESOURCE) {
139 140 141 142
    ASSERT_EQ(empty_mgr_->GetNumOfResource(), 0);
    ASSERT_EQ(mgr1_->GetNumOfResource(), 3);
}

S
starlord 已提交
143
TEST_F(ResourceMgrBaseTest, GET_NUM_OF_COMPUTE_RESOURCE) {
144 145 146 147
    ASSERT_EQ(empty_mgr_->GetNumOfComputeResource(), 0);
    ASSERT_EQ(mgr1_->GetNumOfComputeResource(), 1);
}

S
starlord 已提交
148
TEST_F(ResourceMgrBaseTest, GET_NUM_OF_GPU_RESOURCE) {
149 150 151 152
    ASSERT_EQ(empty_mgr_->GetNumGpuResource(), 0);
    ASSERT_EQ(mgr1_->GetNumGpuResource(), 1);
}

S
starlord 已提交
153
TEST_F(ResourceMgrBaseTest, DUMP) {
154 155 156
    ASSERT_FALSE(mgr1_->Dump().empty());
}

S
starlord 已提交
157
TEST_F(ResourceMgrBaseTest, DUMP_TASKTABLES) {
158 159 160 161 162 163
    ASSERT_FALSE(mgr1_->DumpTaskTables().empty());
}

/************ ResourceMgrAdvanceTest ************/

class ResourceMgrAdvanceTest : public testing::Test {
S
starlord 已提交
164
 protected:
165 166 167 168
    void
    SetUp() override {
        mgr1_ = std::make_shared<ResourceMgr>();
        disk_res = std::make_shared<DiskResource>("disk", 0, true, false);
W
wxyu 已提交
169
        cpu_res = std::make_shared<CpuResource>("cpu", 0, true, true);
170
        mgr1_->Add(ResourcePtr(disk_res));
W
wxyu 已提交
171
        mgr1_->Add(ResourcePtr(cpu_res));
172 173 174 175 176 177 178 179 180 181
        mgr1_->Start();
    }

    void
    TearDown() override {
        mgr1_->Stop();
    }

    ResourceMgrPtr mgr1_;
    ResourcePtr disk_res;
W
wxyu 已提交
182
    ResourcePtr cpu_res;
183 184
};

S
starlord 已提交
185
TEST_F(ResourceMgrAdvanceTest, REGISTER_SUBSCRIBER) {
186
    bool flag = false;
Y
youny626 已提交
187
    auto callback = [&](EventPtr event) { flag = true; };
188
    mgr1_->RegisterSubscriber(callback);
W
wxyu 已提交
189
    TableFileSchemaPtr dummy = nullptr;
W
wxyu 已提交
190 191
    auto label = std::make_shared<DefaultLabel>();
    disk_res->task_table().Put(std::make_shared<TestTask>(dummy, label));
192 193 194 195
    sleep(1);
    ASSERT_TRUE(flag);
}

Y
youny626 已提交
196 197
}  // namespace scheduler
}  // namespace milvus