abtest.h 2.6 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#pragma once
W
wangguibao 已提交
16
#include <google/protobuf/message.h>
W
wangguibao 已提交
17 18
#include <string>
#include <vector>
G
guru4elephant 已提交
19 20 21
#include "core/sdk-cpp/include/common.h"
#include "core/sdk-cpp/include/factory.h"
#include "core/sdk-cpp/include/stub.h"
W
sdk-cpp  
wangguibao 已提交
22 23 24 25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace sdk_cpp {

class Stub;
class Variant;

W
wangguibao 已提交
30
static const char* WEIGHT_SEPERATOR = "|";
W
sdk-cpp  
wangguibao 已提交
31 32

class EndpointRouterBase {
W
wangguibao 已提交
33 34
 public:
  typedef std::vector<Variant*> VariantList;
W
sdk-cpp  
wangguibao 已提交
35

W
wangguibao 已提交
36
  virtual ~EndpointRouterBase() {}
W
sdk-cpp  
wangguibao 已提交
37

W
wangguibao 已提交
38
  virtual int initialize(const google::protobuf::Message& conf) = 0;
W
sdk-cpp  
wangguibao 已提交
39

W
wangguibao 已提交
40
  virtual Variant* route(const VariantList&) = 0;
W
sdk-cpp  
wangguibao 已提交
41

W
wangguibao 已提交
42
  virtual Variant* route(const VariantList&, const void*) = 0;
W
sdk-cpp  
wangguibao 已提交
43 44 45
};

class WeightedRandomRender : public EndpointRouterBase {
W
wangguibao 已提交
46 47
 public:
  static int register_self() {
48 49 50 51 52
    // INLINE_REGIST_OBJECT(WeightedRandomRender, EndpointRouterBase, -1);

    Factory<WeightedRandomRender, EndpointRouterBase>* factory =
        new (std::nothrow) Factory<WeightedRandomRender, EndpointRouterBase>();
    if (factory == NULL) {
T
TeslaZhao 已提交
53 54
      RAW_LOG(ERROR,
              "Failed regist factory: WeightedRandomRender->EndpointRouterBase \
55
          in macro!");
56 57 58 59 60 61 62 63 64
      return -1;
    }

    // When two clients are created in the same process, two
    // "WeightedRandomRender" factory objects are registered.
    // But in fact, the two clients can use one factory object
    // together.
    if (FactoryPool<EndpointRouterBase>::instance().register_factory(
            "WeightedRandomRender", factory) != 0) {
T
TeslaZhao 已提交
65 66
      RAW_LOG(INFO,
              "Factory has been registed: \
67
              WeightedRandomRender->EndpointRouterBase.");
68 69
    }

W
wangguibao 已提交
70 71
    return 0;
  }
W
sdk-cpp  
wangguibao 已提交
72

W
wangguibao 已提交
73
  WeightedRandomRender() : _normalized_sum(0) {}
W
sdk-cpp  
wangguibao 已提交
74

W
wangguibao 已提交
75
  ~WeightedRandomRender() {}
W
sdk-cpp  
wangguibao 已提交
76

W
wangguibao 已提交
77
  int initialize(const google::protobuf::Message& conf);
W
sdk-cpp  
wangguibao 已提交
78

W
wangguibao 已提交
79
  Variant* route(const VariantList&);
W
sdk-cpp  
wangguibao 已提交
80

W
wangguibao 已提交
81
  Variant* route(const VariantList&, const void*);
W
sdk-cpp  
wangguibao 已提交
82

W
wangguibao 已提交
83 84 85
 private:
  std::vector<uint32_t> _variant_weight_list;
  uint32_t _normalized_sum;
W
sdk-cpp  
wangguibao 已提交
86 87
};

W
wangguibao 已提交
88 89 90
}  // namespace sdk_cpp
}  // namespace paddle_serving
}  // namespace baidu