resource_test.cpp 2.6 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

#include "scheduler/resource/Resource.h"
#include "scheduler/resource/DiskResource.h"
#include "scheduler/resource/CpuResource.h"
#include "scheduler/resource/GpuResource.h"
#include "scheduler/task/Task.h"
#include "scheduler/task/TestTask.h"
#include "scheduler/ResourceFactory.h"
#include <gtest/gtest.h>


namespace zilliz {
namespace milvus {
namespace engine {

class ResourceTest : public testing::Test {
protected:
    void
    SetUp() override {
        disk_resource_ = ResourceFactory::Create("disk");
        cpu_resource_ = ResourceFactory::Create("cpu");
        gpu_resource_ = ResourceFactory::Create("gpu");
        flag_ = false;

W
wxyu 已提交
30
        auto subscriber = [&](EventPtr  event) {
W
wxyu 已提交
31
            std::unique_lock<std::mutex> lock(mutex_);
W
wxyu 已提交
32 33 34 35
            if (event->Type() == EventType::COPY_COMPLETED || event->Type() == EventType::FINISH_TASK) {
                flag_ = true;
                cv_.notify_one();
            }
W
wxyu 已提交
36 37 38 39 40
        };

        disk_resource_->RegisterSubscriber(subscriber);
        cpu_resource_->RegisterSubscriber(subscriber);
        gpu_resource_->RegisterSubscriber(subscriber);
W
wxyu 已提交
41 42 43 44 45 46 47 48 49 50 51

        disk_resource_->Start();
        cpu_resource_->Start();
        gpu_resource_->Start();
    }

    void
    TearDown() override {
        disk_resource_->Stop();
        cpu_resource_->Stop();
        gpu_resource_->Stop();
W
wxyu 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    }

    void
    Wait() {
        std::unique_lock<std::mutex> lock(mutex_);
        cv_.wait(lock, [&] { return flag_; });
    }

    ResourcePtr disk_resource_;
    ResourcePtr cpu_resource_;
    ResourcePtr gpu_resource_;
    bool flag_;
    std::mutex mutex_;
    std::condition_variable cv_;
};


TEST_F(ResourceTest, cpu_resource_test) {
    auto task = std::make_shared<TestTask>();
    cpu_resource_->task_table().Put(task);
    cpu_resource_->WakeupLoader();
    Wait();
    ASSERT_EQ(task->load_count_, 1);
    flag_ = false;
    cpu_resource_->WakeupExecutor();
    Wait();
    ASSERT_EQ(task->exec_count_, 1);
}

TEST_F(ResourceTest, gpu_resource_test) {
    auto task = std::make_shared<TestTask>();
    gpu_resource_->task_table().Put(task);
    gpu_resource_->WakeupLoader();
    Wait();
    ASSERT_EQ(task->load_count_, 1);
    flag_ = false;
    gpu_resource_->WakeupExecutor();
    Wait();
    ASSERT_EQ(task->exec_count_, 1);
}


}
}
}