test_request_ctrl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <future>
  22. #include <limits>
  23. #include <memory>
  24. #include <random>
  25. #include <set>
  26. #include <string>
  27. #include <utility>
  28. #include "core/request_ctrl.h"
  29. #include "core/session.h"
  30. namespace infer_server {
  31. namespace {
  32. std::random_device rd;
  33. std::mt19937 gen(rd());
  34. std::uniform_int_distribution<int64_t> request_id_dis(0, std::numeric_limits<int64_t>::max());
  35. std::uniform_int_distribution<uint32_t> data_num_dis(1, 100);
  36. TEST(InferServerCore, RequestCtrl) {
  37. int repeat_times = 10;
  38. while (repeat_times--) {
  39. int64_t request_id = request_id_dis(gen);
  40. uint32_t data_num = data_num_dis(gen);
  41. std::string tag = "test ctrl" + std::to_string(request_id);
  42. PackagePtr out;
  43. Status status;
  44. bool response_done_flag = false;
  45. bool notify_flag = false;
  46. auto response = [&status, &out, &response_done_flag](Status s, PackagePtr pack) {
  47. out = std::move(pack);
  48. status = s;
  49. response_done_flag = true;
  50. };
  51. auto notifier = [&notify_flag](const RequestControl* caller) { notify_flag = true; };
  52. std::unique_ptr<RequestControl> ctrl(new RequestControl(response, notifier, tag, request_id, data_num));
  53. std::future<void> flag = ctrl->ResponseDonePromise();
  54. EXPECT_EQ(tag, ctrl->Tag());
  55. EXPECT_EQ(request_id, ctrl->RequestId());
  56. ASSERT_EQ(data_num, ctrl->DataNum());
  57. ASSERT_FALSE(ctrl->IsDiscarded());
  58. ASSERT_FALSE(ctrl->IsProcessFinished());
  59. ASSERT_TRUE(ctrl->IsSuccess());
  60. for (uint32_t idx = 0; idx < data_num; ++idx) {
  61. ASSERT_FALSE(ctrl->IsProcessFinished());
  62. ASSERT_FALSE(notify_flag);
  63. ASSERT_TRUE(ctrl->IsSuccess());
  64. ctrl->ProcessDone(Status::SUCCESS, nullptr, idx, {});
  65. }
  66. ASSERT_TRUE(notify_flag);
  67. ASSERT_TRUE(ctrl->IsProcessFinished());
  68. ASSERT_TRUE(ctrl->IsSuccess());
  69. ASSERT_EQ(flag.wait_for(std::chrono::milliseconds(1)), std::future_status::timeout);
  70. ASSERT_FALSE(response_done_flag);
  71. ctrl->Response();
  72. ASSERT_TRUE(response_done_flag);
  73. ctrl.reset();
  74. ASSERT_NE(flag.wait_for(std::chrono::milliseconds(1)), std::future_status::timeout);
  75. flag.get();
  76. ASSERT_TRUE(out);
  77. ASSERT_EQ(status, Status::SUCCESS);
  78. ASSERT_EQ(out->data.size(), data_num);
  79. EXPECT_EQ(out->tag, tag);
  80. }
  81. }
  82. auto empty_response_func = [](Status s, PackagePtr pack) {};
  83. auto empty_notifier_func = [](const RequestControl*) {};
  84. inline Status GenError() {
  85. std::uniform_int_distribution<int> err_dis(1, static_cast<int>(Status::STATUS_COUNT));
  86. return static_cast<Status>(err_dis(gen));
  87. }
  88. TEST(InferServerCore, RequestCtrlFailed) {
  89. int repeat_times = 10;
  90. while (repeat_times--) {
  91. int64_t request_id = request_id_dis(gen);
  92. uint32_t data_num = data_num_dis(gen);
  93. std::uniform_int_distribution<uint32_t> error_idx_dis(0, data_num - 1);
  94. std::set<uint32_t> err_idxs;
  95. uint32_t err_num = error_idx_dis(gen) / 2 + 1;
  96. while (err_idxs.size() < err_num) {
  97. err_idxs.insert(error_idx_dis(gen));
  98. }
  99. std::string tag = "test ctrl" + std::to_string(request_id);
  100. PackagePtr out;
  101. Status status;
  102. auto response = [&status, &out](Status s, PackagePtr pack) {
  103. out = std::move(pack);
  104. status = s;
  105. };
  106. bool notify_flag = false;
  107. auto notifier = [&notify_flag](const RequestControl* caller) { notify_flag = true; };
  108. std::unique_ptr<RequestControl> ctrl(new RequestControl(response, notifier, tag, request_id, data_num));
  109. std::future<void> flag = ctrl->ResponseDonePromise();
  110. ASSERT_FALSE(ctrl->IsDiscarded());
  111. ASSERT_FALSE(ctrl->IsProcessFinished());
  112. ASSERT_TRUE(ctrl->IsSuccess());
  113. Status first_err = Status::SUCCESS;
  114. for (uint32_t idx = 0; idx < data_num; ++idx) {
  115. if (err_idxs.count(idx)) {
  116. Status err = GenError();
  117. ASSERT_NE(err, Status::SUCCESS);
  118. if (first_err == Status::SUCCESS) first_err = err;
  119. ctrl->ProcessFailed(err);
  120. EXPECT_FALSE(ctrl->IsSuccess());
  121. } else {
  122. ctrl->ProcessDone(Status::SUCCESS, nullptr, idx, {});
  123. }
  124. }
  125. ASSERT_TRUE(notify_flag);
  126. ASSERT_TRUE(ctrl->IsProcessFinished());
  127. ASSERT_FALSE(ctrl->IsSuccess());
  128. ctrl->Response();
  129. ctrl.reset();
  130. flag.get();
  131. ASSERT_EQ(first_err, status);
  132. ASSERT_TRUE(out);
  133. ASSERT_EQ(out->data.size(), data_num);
  134. }
  135. }
  136. TEST(InferServerCore, RequestCtrlDiscard) {
  137. std::unique_ptr<RequestControl> ctrl(new RequestControl(empty_response_func, empty_notifier_func, "", 0, 4u));
  138. ASSERT_FALSE(ctrl->IsDiscarded());
  139. ctrl->Discard();
  140. ASSERT_TRUE(ctrl->IsDiscarded());
  141. }
  142. #ifdef CNIS_RECORD_PERF
  143. TEST(InferServerCore, RequestCtrlPerf) {
  144. PackagePtr out;
  145. Status status;
  146. auto response = [&status, &out](Status s, PackagePtr pack) {
  147. out = std::move(pack);
  148. status = s;
  149. };
  150. uint32_t data_num = 4;
  151. std::unique_ptr<RequestControl> ctrl(new RequestControl(response, empty_notifier_func, "", 0, data_num));
  152. ctrl->BeginRecord();
  153. std::uniform_real_distribution<float> perf_dis(0, 10000);
  154. float a_sum = 0, b_sum = 0;
  155. for (uint32_t idx = 0; idx < data_num; ++idx) {
  156. float a = perf_dis(gen);
  157. float b = perf_dis(gen);
  158. a_sum += a;
  159. b_sum += b;
  160. ctrl->ProcessDone(Status::SUCCESS, nullptr, idx, {{"a", a}, {"b", b}});
  161. }
  162. auto perf = ctrl->Performance();
  163. ASSERT_EQ(perf.size(), 2u);
  164. ASSERT_NO_THROW(perf.at("a"));
  165. ASSERT_NO_THROW(perf.at("b"));
  166. EXPECT_FLOAT_EQ(a_sum, perf["a"]);
  167. EXPECT_FLOAT_EQ(b_sum, perf["b"]);
  168. ctrl->Response();
  169. ASSERT_EQ(status, Status::SUCCESS);
  170. ASSERT_TRUE(out);
  171. EXPECT_EQ(out->perf, perf);
  172. EXPECT_EQ(out->data.size(), data_num);
  173. }
  174. #endif
  175. } // namespace
  176. } // namespace infer_server