test_thread_pool.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <chrono>
  22. #include <iostream>
  23. #include <memory>
  24. #include <thread>
  25. #include <vector>
  26. #include "cnis/infer_server.h"
  27. #include "util/thread_pool.h"
  28. TEST(InferServerUtil, EqualityThreadPool) {
  29. infer_server::EqualityThreadPool tp(nullptr, 5);
  30. EXPECT_EQ(5u, tp.Size());
  31. tp.Resize(10);
  32. EXPECT_EQ(10u, tp.Size());
  33. tp.Resize(3);
  34. EXPECT_EQ(3u, tp.Size());
  35. tp.Stop();
  36. EXPECT_EQ(0u, tp.Size());
  37. infer_server::EqualityThreadPool p(nullptr, 10);
  38. std::vector<int> res;
  39. std::mutex m_lock;
  40. std::vector<std::future<void>> ret;
  41. for (int i = 3; i < 13; ++i) {
  42. ret.emplace_back(p.Push(
  43. 0,
  44. [&m_lock, &res](int n) {
  45. std::lock_guard<std::mutex> lk(m_lock);
  46. res.push_back(n);
  47. },
  48. i));
  49. }
  50. for (auto& it : ret) {
  51. it.get();
  52. }
  53. for (int i = 3; i < 13; i++) {
  54. auto it = std::find(res.begin(), res.end(), i);
  55. EXPECT_NE(it, res.end());
  56. }
  57. }
  58. TEST(InferServerUtil, ThreadPoolStable) {
  59. std::unique_ptr<infer_server::PriorityThreadPool> main_pool;
  60. std::unique_ptr<infer_server::EqualityThreadPool> pre_pool;
  61. std::unique_ptr<infer_server::EqualityThreadPool> post_pool;
  62. main_pool.reset(new infer_server::PriorityThreadPool(nullptr));
  63. {
  64. main_pool->Resize(8);
  65. pre_pool.reset(new infer_server::EqualityThreadPool(nullptr));
  66. pre_pool->Resize(4);
  67. pre_pool->Resize(8);
  68. post_pool.reset(new infer_server::EqualityThreadPool(nullptr));
  69. post_pool->Resize(4);
  70. post_pool->Resize(8);
  71. std::promise<void> end;
  72. std::function<void(infer_server::any, int)> task = [&main_pool, &pre_pool, &post_pool, &end](infer_server::any next,
  73. int i) {
  74. if (i == 2) {
  75. auto fut = post_pool->Push(0, []() {
  76. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  77. return true;
  78. });
  79. fut.get();
  80. } else if (i == 0) {
  81. auto fut = pre_pool->Push(0, []() {
  82. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  83. return true;
  84. });
  85. fut.get();
  86. }
  87. if (i < 4) {
  88. ++i;
  89. main_pool->VoidPush(i, infer_server::any_cast<std::function<void(infer_server::any, int)>&>(next), next, i);
  90. } else {
  91. end.set_value();
  92. }
  93. };
  94. main_pool->VoidPush(0, task, infer_server::any(task), 0);
  95. end.get_future().get();
  96. post_pool->Resize(4);
  97. post_pool->Stop(true);
  98. post_pool.reset();
  99. main_pool->Resize(0);
  100. }
  101. }