test_bsf.cpp 3.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
W
wangguibao 已提交
14

W
wangguibao 已提交
15 16
#include "predictor/unittest/test_bsf.h"
#include <vector>
W
wangguibao 已提交
17 18 19 20 21

namespace baidu {
namespace paddle_serving {
namespace unittest {

W
wangguibao 已提交
22
butil::atomic<size_t> global_id;
W
wangguibao 已提交
23 24

void TestItem::auto_gen() {
W
wangguibao 已提交
25 26 27 28 29
  id = global_id.fetch_add(1);
  char buf[128];
  snprintf(buf, sizeof(buf), "test-%d", id);
  text = buf;
  printf("id:%d,text:%s\n", id, text.c_str());
W
wangguibao 已提交
30 31
}

W
wangguibao 已提交
32 33 34 35 36 37
void work(const std::vector<TestItem>& in,
          std::vector<TestItem>& out) {  // NOLINT
  for (size_t i = 0; i < in.size(); ++i) {
    out[i] = in[i];
    usleep(50);
  }
W
wangguibao 已提交
38 39 40
}

TEST_F(TestBsf, test_single_thread) {
W
wangguibao 已提交
41 42 43 44 45 46 47 48
  // initialize TaskExecutor
  global_id.store(0, butil::memory_order_relaxed);
  im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()
      ->set_thread_callback_fn(boost::bind(&work, _1, _2));
  EXPECT_EQ(
      (im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()
           ->start(1)),
      0);
W
wangguibao 已提交
49

W
wangguibao 已提交
50 51
  std::vector<TestItem> in;
  std::vector<TestItem> out;
W
wangguibao 已提交
52

W
wangguibao 已提交
53
  TestItem::create(in, out, 5);
W
wangguibao 已提交
54

W
wangguibao 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  im::bsf::TaskManager<TestItem, TestItem> task_manager;
  task_manager.schedule(in, out);
  printf("wait for bsf finish...\n");
  task_manager.wait();
  printf("bsf executed finished\n");
  ASSERT_EQ(out.size(), 5);
  for (size_t i = 0; i < out.size(); i++) {
    char temp[128];
    snprintf(temp, sizeof(temp), "test-%d", i);
    EXPECT_EQ(i, in[i].id);
    EXPECT_EQ(i, out[i].id);
    EXPECT_STREQ(temp, in[i].text.c_str());
    EXPECT_STREQ(temp, out[i].text.c_str());
  }

  im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()->stop();
W
wangguibao 已提交
71 72 73
}

TEST_F(TestBsf, test_multi_thread) {
W
wangguibao 已提交
74 75 76 77 78 79 80 81 82 83
  // initialize TaskExecutor
  global_id.store(0, butil::memory_order_relaxed);
  im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()
      ->set_thread_callback_fn(boost::bind(&work, _1, _2));
  im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()
      ->set_batch_size(100);
  EXPECT_EQ(
      (im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()
           ->start(3)),
      0);
W
wangguibao 已提交
84

W
wangguibao 已提交
85 86 87 88 89 90
  const size_t psize = 5;
  std::unique_ptr<pthread_t*> pid;
  pid.reset(new pthread_t[psize]);
  for (size_t i = 0; i < psize; ++i) {
    pthread_create(&pid[i], NULL, &TestBsf::task_trigger, NULL);
  }
W
wangguibao 已提交
91

W
wangguibao 已提交
92 93 94
  for (size_t i = 0; i < psize; ++i) {
    pthread_join(pid[i], NULL);
  }
W
wangguibao 已提交
95

W
wangguibao 已提交
96
  im::bsf::TaskExecutor<im::bsf::Task<TestItem, TestItem>>::instance()->stop();
W
wangguibao 已提交
97
}
W
wangguibao 已提交
98 99 100
}  // namespace unittest
}  // namespace paddle_serving
}  // namespace baidu
W
wangguibao 已提交
101 102

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */