server.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef TEST_QPS_SERVER_H
  19. #define TEST_QPS_SERVER_H
  20. #include <vector>
  21. #include <grpc/support/cpu.h>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/channel.h>
  24. #include <grpcpp/resource_quota.h>
  25. #include <grpcpp/security/server_credentials.h>
  26. #include <grpcpp/server_builder.h>
  27. #include "src/cpp/util/core_stats.h"
  28. #include "src/proto/grpc/testing/control.pb.h"
  29. #include "src/proto/grpc/testing/messages.pb.h"
  30. #include "test/core/end2end/data/ssl_test_data.h"
  31. #include "test/core/util/port.h"
  32. #include "test/cpp/qps/usage_timer.h"
  33. #include "test/cpp/util/test_credentials_provider.h"
  34. namespace grpc {
  35. namespace testing {
  36. class Server {
  37. public:
  38. explicit Server(const ServerConfig& config)
  39. : timer_(new UsageTimer), last_reset_poll_count_(0) {
  40. cores_ = gpr_cpu_num_cores();
  41. if (config.port()) { // positive for a fixed port, negative for inproc
  42. port_ = config.port();
  43. } else { // zero for dynamic port
  44. port_ = grpc_pick_unused_port_or_die();
  45. }
  46. }
  47. virtual ~Server() {}
  48. ServerStats Mark(bool reset) {
  49. UsageTimer::Result timer_result;
  50. int cur_poll_count = GetPollCount();
  51. int poll_count = cur_poll_count - last_reset_poll_count_;
  52. if (reset) {
  53. std::unique_ptr<UsageTimer> timer(new UsageTimer);
  54. timer.swap(timer_);
  55. timer_result = timer->Mark();
  56. last_reset_poll_count_ = cur_poll_count;
  57. } else {
  58. timer_result = timer_->Mark();
  59. }
  60. grpc_stats_data core_stats;
  61. grpc_stats_collect(&core_stats);
  62. ServerStats stats;
  63. stats.set_time_elapsed(timer_result.wall);
  64. stats.set_time_system(timer_result.system);
  65. stats.set_time_user(timer_result.user);
  66. stats.set_total_cpu_time(timer_result.total_cpu_time);
  67. stats.set_idle_cpu_time(timer_result.idle_cpu_time);
  68. stats.set_cq_poll_count(poll_count);
  69. CoreStatsToProto(core_stats, stats.mutable_core_stats());
  70. return stats;
  71. }
  72. static bool SetPayload(PayloadType type, int size, Payload* payload) {
  73. // TODO(yangg): Support UNCOMPRESSABLE payload.
  74. if (type != PayloadType::COMPRESSABLE) {
  75. return false;
  76. }
  77. payload->set_type(type);
  78. // Don't waste time creating a new payload of identical size.
  79. if (payload->body().length() != static_cast<size_t>(size)) {
  80. std::unique_ptr<char[]> body(new char[size]());
  81. payload->set_body(body.get(), size);
  82. }
  83. return true;
  84. }
  85. int port() const { return port_; }
  86. int cores() const { return cores_; }
  87. static std::shared_ptr<ServerCredentials> CreateServerCredentials(
  88. const ServerConfig& config) {
  89. if (config.has_security_params()) {
  90. std::string type;
  91. if (config.security_params().cred_type().empty()) {
  92. type = kTlsCredentialsType;
  93. } else {
  94. type = config.security_params().cred_type();
  95. }
  96. return GetCredentialsProvider()->GetServerCredentials(type);
  97. } else {
  98. return InsecureServerCredentials();
  99. }
  100. }
  101. virtual int GetPollCount() {
  102. // For sync server.
  103. return 0;
  104. }
  105. virtual std::shared_ptr<Channel> InProcessChannel(
  106. const ChannelArguments& args) = 0;
  107. protected:
  108. static void ApplyConfigToBuilder(const ServerConfig& config,
  109. ServerBuilder* builder) {
  110. if (config.resource_quota_size() > 0) {
  111. builder->SetResourceQuota(ResourceQuota("AsyncQpsServerTest")
  112. .Resize(config.resource_quota_size()));
  113. }
  114. for (const auto& channel_arg : config.channel_args()) {
  115. switch (channel_arg.value_case()) {
  116. case ChannelArg::kStrValue:
  117. builder->AddChannelArgument(channel_arg.name(),
  118. channel_arg.str_value());
  119. break;
  120. case ChannelArg::kIntValue:
  121. builder->AddChannelArgument(channel_arg.name(),
  122. channel_arg.int_value());
  123. break;
  124. case ChannelArg::VALUE_NOT_SET:
  125. gpr_log(GPR_ERROR, "Channel arg '%s' does not have a value",
  126. channel_arg.name().c_str());
  127. break;
  128. }
  129. }
  130. }
  131. private:
  132. int port_;
  133. int cores_;
  134. std::unique_ptr<UsageTimer> timer_;
  135. int last_reset_poll_count_;
  136. };
  137. std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config);
  138. std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config);
  139. std::unique_ptr<Server> CreateAsyncGenericServer(const ServerConfig& config);
  140. std::unique_ptr<Server> CreateCallbackServer(const ServerConfig& config);
  141. } // namespace testing
  142. } // namespace grpc
  143. #endif