resource_mgr_test.cpp 5.9 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.

18 19 20 21 22 23 24 25 26 27 28 29

#include "scheduler/resource/CpuResource.h"
#include "scheduler/resource/GpuResource.h"
#include "scheduler/resource/DiskResource.h"
#include "scheduler/resource/TestResource.h"
#include "scheduler/task/TestTask.h"
#include "scheduler/ResourceMgr.h"
#include <gtest/gtest.h>


namespace zilliz {
namespace milvus {
W
wxyu 已提交
30
namespace scheduler {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


/************ ResourceMgrBaseTest ************/
class ResourceMgrBaseTest : public testing::Test {
protected:
    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 已提交
59
TEST_F(ResourceMgrBaseTest, ADD) {
60 61 62 63 64
    auto resource = std::make_shared<TestResource>("test", 0, true, true);
    auto ret = empty_mgr_->Add(ResourcePtr(resource));
    ASSERT_EQ(ret.lock(), resource);
}

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

S
starlord 已提交
71
TEST_F(ResourceMgrBaseTest, CONNECT) {
72 73 74 75 76 77 78 79 80
    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 已提交
81
TEST_F(ResourceMgrBaseTest, INVALID_CONNECT) {
82 83 84 85 86 87 88 89 90
    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 已提交
91
TEST_F(ResourceMgrBaseTest, CLEAR) {
92 93 94 95 96
    ASSERT_EQ(mgr1_->GetNumOfResource(), 3);
    mgr1_->Clear();
    ASSERT_EQ(mgr1_->GetNumOfResource(), 0);
}

S
starlord 已提交
97
TEST_F(ResourceMgrBaseTest, GET_DISK_RESOURCES) {
98 99 100 101 102
    auto disks = mgr1_->GetDiskResources();
    ASSERT_EQ(disks.size(), 1);
    ASSERT_EQ(disks[0].lock(), disk_res);
}

S
starlord 已提交
103
TEST_F(ResourceMgrBaseTest, GET_ALL_RESOURCES) {
104 105 106 107 108 109 110 111 112 113 114 115 116 117
    bool disk = false, cpu = false, gpu = false;
    auto resources = mgr1_->GetAllResources();
    ASSERT_EQ(resources.size(), 3);
    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;
    }

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

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

S
starlord 已提交
124
TEST_F(ResourceMgrBaseTest, GET_RESOURCE_BY_TYPE_AND_DEVICEID) {
125 126 127 128 129 130 131
    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 已提交
132
TEST_F(ResourceMgrBaseTest, GET_RESOURCE_BY_NAME) {
133 134 135 136 137 138 139
    auto disk = mgr1_->GetResource("disk");
    ASSERT_EQ(disk, disk_res);

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

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

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

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

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

S
starlord 已提交
159
TEST_F(ResourceMgrBaseTest, DUMP_TASKTABLES) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    ASSERT_FALSE(mgr1_->DumpTaskTables().empty());
}

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

class ResourceMgrAdvanceTest : public testing::Test {
    protected:
    void
    SetUp() override {
        mgr1_ = std::make_shared<ResourceMgr>();
        disk_res = std::make_shared<DiskResource>("disk", 0, true, false);
        mgr1_->Add(ResourcePtr(disk_res));
        mgr1_->Start();
    }

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

    ResourceMgrPtr mgr1_;
    ResourcePtr disk_res;
};

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


}
}
}