blob: 8fbf37a0957869868fec4b68c75c0823f21e1b39 [file] [log] [blame]
Craig Tiller26598a32015-03-02 16:16:00 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller26598a32015-03-02 16:16:00 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef TEST_QPS_SERVER_H
35#define TEST_QPS_SERVER_H
36
vjpai780a7f22015-11-04 00:19:09 -080037#include <grpc++/security/server_credentials.h>
Vijay Pai3448c272016-01-13 11:17:13 -080038#include <grpc/support/cpu.h>
39#include <vector>
vjpaid08a7382015-11-02 16:45:08 -080040
yang-gd0084c22017-03-06 11:23:35 -080041#include "src/proto/grpc/testing/control.pb.h"
42#include "src/proto/grpc/testing/messages.pb.h"
vjpai780a7f22015-11-04 00:19:09 -080043#include "test/core/end2end/data/ssl_test_data.h"
vjpai72a63322015-10-29 02:23:11 -070044#include "test/core/util/port.h"
Craig Tiller732a8752016-02-22 15:59:19 -080045#include "test/cpp/qps/usage_timer.h"
Craig Tiller26598a32015-03-02 16:16:00 -080046
47namespace grpc {
48namespace testing {
49
50class Server {
51 public:
vjpaie2d39e02016-02-24 12:36:15 -080052 explicit Server(const ServerConfig& config) : timer_(new UsageTimer) {
Alexander Polcyn8a0ef632017-01-11 13:52:01 -080053 cores_ = gpr_cpu_num_cores();
vjpai72a63322015-10-29 02:23:11 -070054 if (config.port()) {
55 port_ = config.port();
Vijay Paic64736d2016-02-01 09:33:11 -080056
vjpai72a63322015-10-29 02:23:11 -070057 } else {
58 port_ = grpc_pick_unused_port_or_die();
59 }
60 }
Craig Tiller26598a32015-03-02 16:16:00 -080061 virtual ~Server() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080062
vjpai119c1032015-10-29 01:21:04 -070063 ServerStats Mark(bool reset) {
vjpaie2d39e02016-02-24 12:36:15 -080064 UsageTimer::Result timer_result;
vjpai119c1032015-10-29 01:21:04 -070065 if (reset) {
vjpaie2d39e02016-02-24 12:36:15 -080066 std::unique_ptr<UsageTimer> timer(new UsageTimer);
vjpai119c1032015-10-29 01:21:04 -070067 timer.swap(timer_);
68 timer_result = timer->Mark();
69 } else {
70 timer_result = timer_->Mark();
71 }
Craig Tillerd6479d62015-03-04 12:50:11 -080072
73 ServerStats stats;
74 stats.set_time_elapsed(timer_result.wall);
75 stats.set_time_system(timer_result.system);
76 stats.set_time_user(timer_result.user);
Yuxuan Life21ec92016-10-06 15:05:29 -070077 stats.set_total_cpu_time(timer_result.total_cpu_time);
78 stats.set_idle_cpu_time(timer_result.idle_cpu_time);
Craig Tillerd6479d62015-03-04 12:50:11 -080079 return stats;
80 }
81
Craig Tillera182bf12015-03-04 13:54:39 -080082 static bool SetPayload(PayloadType type, int size, Payload* payload) {
Craig Tillera182bf12015-03-04 13:54:39 -080083 // TODO(yangg): Support UNCOMPRESSABLE payload.
84 if (type != PayloadType::COMPRESSABLE) {
85 return false;
86 }
vjpaib6df94a2015-11-30 15:52:50 -080087 payload->set_type(type);
Craig Tillera182bf12015-03-04 13:54:39 -080088 std::unique_ptr<char[]> body(new char[size]());
89 payload->set_body(body.get(), size);
90 return true;
91 }
Craig Tillerd6479d62015-03-04 12:50:11 -080092
vjpaifba20c92015-11-04 14:49:17 -080093 int port() const { return port_; }
Vijay Pai3448c272016-01-13 11:17:13 -080094 int cores() const { return cores_; }
Vijay Paice846702015-11-04 00:30:12 -080095 static std::shared_ptr<ServerCredentials> CreateServerCredentials(
96 const ServerConfig& config) {
vjpai780a7f22015-11-04 00:19:09 -080097 if (config.has_security_params()) {
98 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
Vijay Paice846702015-11-04 00:30:12 -080099 test_server1_cert};
vjpai780a7f22015-11-04 00:19:09 -0800100 SslServerCredentialsOptions ssl_opts;
101 ssl_opts.pem_root_certs = "";
102 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
103 return SslServerCredentials(ssl_opts);
104 } else {
105 return InsecureServerCredentials();
106 }
107 }
Vijay Paice846702015-11-04 00:30:12 -0800108
Craig Tillerd6479d62015-03-04 12:50:11 -0800109 private:
vjpai72a63322015-10-29 02:23:11 -0700110 int port_;
Vijay Pai0e66efd2016-01-13 11:15:49 -0800111 int cores_;
vjpaie2d39e02016-02-24 12:36:15 -0800112 std::unique_ptr<UsageTimer> timer_;
Craig Tiller26598a32015-03-02 16:16:00 -0800113};
114
vjpai72a63322015-10-29 02:23:11 -0700115std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config);
116std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config);
vjpaia00f9982016-01-26 17:51:35 -0800117std::unique_ptr<Server> CreateAsyncGenericServer(const ServerConfig& config);
Craig Tiller26598a32015-03-02 16:16:00 -0800118
119} // namespace testing
120} // namespace grpc
121
122#endif