test_batcher.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <list>
  23. #include <random>
  24. #include <set>
  25. #include <thread>
  26. #include <vector>
  27. #include "test_base.h"
  28. #include "util/batcher.h"
  29. namespace infer_server {
  30. TEST(InferServerUtil, Batcher) {
  31. constexpr uint32_t set_size = 100;
  32. std::random_device rd;
  33. std::mt19937 gen(rd());
  34. std::uniform_int_distribution<int> dis(0, 10000);
  35. std::set<int> int_set;
  36. while (int_set.size() < set_size) {
  37. int_set.insert(dis(gen));
  38. }
  39. std::vector<int> batch_out;
  40. batch_out.reserve(set_size);
  41. std::cout << "test without timeout\n";
  42. for (int time = 0; time < 100; ++time) {
  43. constexpr uint32_t timeout = 0;
  44. std::uniform_int_distribution<uint32_t> bs_dis(1, 50);
  45. const uint32_t batch_size = bs_dis(gen);
  46. auto notifier = [&batch_out](std::vector<int>&& out) { batch_out.insert(batch_out.end(), out.begin(), out.end()); };
  47. Batcher<int> b(notifier, timeout, batch_size);
  48. uint32_t index = 0;
  49. for (auto& it : int_set) {
  50. EXPECT_EQ(b.Size(), index++);
  51. b.AddItem(it);
  52. index %= batch_size;
  53. }
  54. EXPECT_EQ(batch_out.size(), set_size - set_size % batch_size);
  55. b.Emit();
  56. EXPECT_EQ(batch_out.size(), set_size);
  57. index = 0;
  58. for (auto& it : int_set) {
  59. EXPECT_EQ(batch_out[index++], it);
  60. }
  61. batch_out.clear();
  62. }
  63. std::cout << "test with timeout\n";
  64. {
  65. constexpr uint32_t batch_size = 12;
  66. constexpr uint32_t send_number_before_timeout = 10;
  67. constexpr uint32_t timeout = 50;
  68. std::promise<void> notify_flag;
  69. auto notifier = [&batch_out, &notify_flag, send_number_before_timeout](std::vector<int>&& out) {
  70. EXPECT_EQ(out.size(), send_number_before_timeout);
  71. batch_out.insert(batch_out.end(), out.begin(), out.end());
  72. notify_flag.set_value();
  73. };
  74. Batcher<int> b(notifier, timeout, batch_size);
  75. uint32_t index = 0;
  76. for (auto& it : int_set) {
  77. b.AddItem(it);
  78. EXPECT_EQ(b.Size(), ++index);
  79. if (index == send_number_before_timeout) {
  80. index = 0;
  81. notify_flag.get_future().get();
  82. notify_flag = std::promise<void>();
  83. }
  84. }
  85. b.Emit();
  86. EXPECT_EQ(batch_out.size(), set_size);
  87. index = 0;
  88. for (auto& it : int_set) {
  89. EXPECT_EQ(batch_out[index++], it);
  90. }
  91. batch_out.clear();
  92. }
  93. }
  94. } // namespace infer_server