test_engine.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <memory>
  22. #include <utility>
  23. #include <vector>
  24. #include "cnis/infer_server.h"
  25. #include "core/engine.h"
  26. #include "core/request_ctrl.h"
  27. #include "test_base.h"
  28. namespace infer_server {
  29. namespace {
  30. auto empty_response_func = [](Status, PackagePtr) {};
  31. auto empty_notifier_func = [](const RequestControl*) {};
  32. std::vector<std::shared_ptr<Processor>> PrepareProcessors(int device_id) {
  33. std::vector<std::shared_ptr<Processor>> processors;
  34. for (size_t idx = 0; idx < 3; ++idx) {
  35. processors.emplace_back(TestProcessor::Create());
  36. processors[idx]->Init();
  37. }
  38. return processors;
  39. }
  40. TEST(InferServerCore, EngineIdle) {
  41. int device_id = 0;
  42. auto processors = PrepareProcessors(device_id);
  43. ASSERT_EQ(processors.size(), 3u);
  44. // test idle
  45. {
  46. PriorityThreadPool tp([device_id]() -> bool { return SetCurrentDevice(device_id); });
  47. std::unique_ptr<Engine> engine(new Engine(processors, [](Engine* idle) {}, &tp));
  48. ASSERT_TRUE(engine);
  49. EXPECT_NE(engine->Fork().get(), engine.get());
  50. std::unique_ptr<RequestControl> ctrl(
  51. new RequestControl(empty_response_func, empty_notifier_func, "", 0, 3));
  52. ASSERT_TRUE(ctrl);
  53. // engine tasknode number = 3
  54. size_t idx = 0;
  55. for (; idx < 2; ++idx) {
  56. auto input = Package::Create(1);
  57. input->data[0]->ctrl = ctrl.get();
  58. input->data[0]->index = idx;
  59. ASSERT_NO_THROW(engine->Run(std::move(input)));
  60. }
  61. // task load = 2, remained capacity = 1
  62. ASSERT_TRUE(engine->IsIdle());
  63. auto input = Package::Create(1);
  64. input->data[0]->ctrl = ctrl.get();
  65. input->data[0]->index = idx;
  66. ASSERT_NO_THROW(engine->Run(std::move(input)));
  67. // have used 3 tasknode, free tasknode == 0
  68. ASSERT_FALSE(engine->IsIdle());
  69. // get all task done
  70. tp.Resize(3);
  71. while (!ctrl->IsProcessFinished()) {}
  72. }
  73. }
  74. TEST(InferServerCore, EngineProcess) {
  75. int device_id = 0;
  76. auto processors = PrepareProcessors(device_id);
  77. ASSERT_EQ(processors.size(), 3u);
  78. {
  79. PriorityThreadPool tp([device_id]() -> bool { return SetCurrentDevice(device_id); }, 3);
  80. std::promise<void> done_flag;
  81. std::unique_ptr<Engine> engine(new Engine(processors, [&done_flag](Engine* idle) { done_flag.set_value(); }, &tp));
  82. ASSERT_TRUE(engine);
  83. std::unique_ptr<RequestControl> ctrl(
  84. new RequestControl(empty_response_func, empty_notifier_func, "", 1, 1));
  85. ASSERT_TRUE(ctrl);
  86. auto input = Package::Create(1);
  87. input->data[0]->ctrl = ctrl.get();
  88. input->data[0]->index = 0;
  89. ASSERT_NO_THROW(engine->Run(std::move(input)));
  90. auto done_ret = done_flag.get_future().wait_for(std::chrono::seconds(1));
  91. ASSERT_EQ(std::future_status::ready, done_ret);
  92. }
  93. }
  94. } // namespace
  95. } // namespace infer_server