server.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <grpc++/resource_quota.h>
  21. #include <grpc++/security/server_credentials.h>
  22. #include <grpc++/server_builder.h>
  23. #include <grpc/support/cpu.h>
  24. #include <grpc/support/log.h>
  25. #include <vector>
  26. #include "src/core/lib/surface/completion_queue.h"
  27. #include "src/proto/grpc/testing/control.pb.h"
  28. #include "src/proto/grpc/testing/messages.pb.h"
  29. #include "test/core/end2end/data/ssl_test_data.h"
  30. #include "test/core/util/port.h"
  31. #include "test/cpp/qps/usage_timer.h"
  32. namespace grpc {
  33. namespace testing {
  34. class Server {
  35. public:
  36. explicit Server(const ServerConfig& config)
  37. : timer_(new UsageTimer), last_reset_poll_count_(0) {
  38. cores_ = gpr_cpu_num_cores();
  39. if (config.port()) {
  40. port_ = config.port();
  41. } else {
  42. port_ = grpc_pick_unused_port_or_die();
  43. }
  44. }
  45. virtual ~Server() {}
  46. ServerStats Mark(bool reset) {
  47. UsageTimer::Result timer_result;
  48. int cur_poll_count = GetPollCount();
  49. int poll_count = cur_poll_count - last_reset_poll_count_;
  50. if (reset) {
  51. std::unique_ptr<UsageTimer> timer(new UsageTimer);
  52. timer.swap(timer_);
  53. timer_result = timer->Mark();
  54. last_reset_poll_count_ = cur_poll_count;
  55. } else {
  56. timer_result = timer_->Mark();
  57. }
  58. ServerStats stats;
  59. stats.set_time_elapsed(timer_result.wall);
  60. stats.set_time_system(timer_result.system);
  61. stats.set_time_user(timer_result.user);
  62. stats.set_total_cpu_time(timer_result.total_cpu_time);
  63. stats.set_idle_cpu_time(timer_result.idle_cpu_time);
  64. stats.set_cq_poll_count(poll_count);
  65. return stats;
  66. }
  67. static bool SetPayload(PayloadType type, int size, Payload* payload) {
  68. // TODO(yangg): Support UNCOMPRESSABLE payload.
  69. if (type != PayloadType::COMPRESSABLE) {
  70. return false;
  71. }
  72. payload->set_type(type);
  73. std::unique_ptr<char[]> body(new char[size]());
  74. payload->set_body(body.get(), size);
  75. return true;
  76. }
  77. int port() const { return port_; }
  78. int cores() const { return cores_; }
  79. static std::shared_ptr<ServerCredentials> CreateServerCredentials(
  80. const ServerConfig& config) {
  81. if (config.has_security_params()) {
  82. SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
  83. test_server1_cert};
  84. SslServerCredentialsOptions ssl_opts;
  85. ssl_opts.pem_root_certs = "";
  86. ssl_opts.pem_key_cert_pairs.push_back(pkcp);
  87. return SslServerCredentials(ssl_opts);
  88. } else {
  89. return InsecureServerCredentials();
  90. }
  91. }
  92. static void ApplyServerConfig(const ServerConfig& config,
  93. ServerBuilder* builder) {
  94. if (config.resource_quota_size() > 0) {
  95. builder->SetResourceQuota(
  96. ResourceQuota("QpsServerTest").Resize(config.resource_quota_size()));
  97. }
  98. for (auto arg : config.channel_args()) {
  99. switch (arg.value_case()) {
  100. case ChannelArg::kStrValue:
  101. builder->AddChannelArgument(arg.name(), arg.str_value());
  102. break;
  103. case ChannelArg::kIntValue:
  104. builder->AddChannelArgument(arg.name(), arg.int_value());
  105. break;
  106. default:
  107. gpr_log(GPR_ERROR, "Channel arg '%s' ignored due to unknown type",
  108. arg.name().c_str());
  109. }
  110. }
  111. }
  112. virtual int GetPollCount() {
  113. // For sync server.
  114. return 0;
  115. }
  116. private:
  117. int port_;
  118. int cores_;
  119. std::unique_ptr<UsageTimer> timer_;
  120. int last_reset_poll_count_;
  121. };
  122. std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config);
  123. std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config);
  124. std::unique_ptr<Server> CreateAsyncGenericServer(const ServerConfig& config);
  125. } // namespace testing
  126. } // namespace grpc
  127. #endif