| Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * 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 | |
| Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame^] | 37 | #include <vector> |
| vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 38 | #include <grpc/support/cpu.h> |
| vjpai | 780a7f2 | 2015-11-04 00:19:09 -0800 | [diff] [blame] | 39 | #include <grpc++/security/server_credentials.h> |
| vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 40 | |
| vjpai | 780a7f2 | 2015-11-04 00:19:09 -0800 | [diff] [blame] | 41 | #include "test/core/end2end/data/ssl_test_data.h" |
| vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 42 | #include "test/core/util/port.h" |
| Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame^] | 43 | #include "test/cpp/qps/coresched.h" |
| Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 44 | #include "test/cpp/qps/timer.h" |
| Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 45 | #include "src/proto/grpc/testing/messages.grpc.pb.h" |
| 46 | #include "src/proto/grpc/testing/control.grpc.pb.h" |
| Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 47 | |
| 48 | namespace grpc { |
| 49 | namespace testing { |
| 50 | |
| 51 | class Server { |
| 52 | public: |
| vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 53 | explicit Server(const ServerConfig& config) : timer_(new Timer) { |
| Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame^] | 54 | int clsize = config.core_list_size(); |
| 55 | if (clsize > 0) { |
| 56 | std::vector<int> core_list; |
| 57 | for (int i = 0; i < clsize; i++) { |
| 58 | core_list.push_back(config.core_list(i)); |
| 59 | } |
| 60 | cores_ = LimitCores(core_list); |
| 61 | } else { |
| 62 | cores_ = gpr_cpu_num_cores(); |
| 63 | } |
| vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 64 | if (config.port()) { |
| 65 | port_ = config.port(); |
| 66 | } else { |
| 67 | port_ = grpc_pick_unused_port_or_die(); |
| 68 | } |
| 69 | } |
| Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 70 | virtual ~Server() {} |
| Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 71 | |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 72 | ServerStats Mark(bool reset) { |
| 73 | Timer::Result timer_result; |
| 74 | if (reset) { |
| 75 | std::unique_ptr<Timer> timer(new Timer); |
| 76 | timer.swap(timer_); |
| 77 | timer_result = timer->Mark(); |
| 78 | } else { |
| 79 | timer_result = timer_->Mark(); |
| 80 | } |
| Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 81 | |
| 82 | ServerStats stats; |
| 83 | stats.set_time_elapsed(timer_result.wall); |
| 84 | stats.set_time_system(timer_result.system); |
| 85 | stats.set_time_user(timer_result.user); |
| 86 | return stats; |
| 87 | } |
| 88 | |
| Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 89 | static bool SetPayload(PayloadType type, int size, Payload* payload) { |
| 90 | PayloadType response_type = type; |
| 91 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 92 | if (type != PayloadType::COMPRESSABLE) { |
| 93 | return false; |
| 94 | } |
| 95 | payload->set_type(response_type); |
| 96 | std::unique_ptr<char[]> body(new char[size]()); |
| 97 | payload->set_body(body.get(), size); |
| 98 | return true; |
| 99 | } |
| Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 100 | |
| vjpai | fba20c9 | 2015-11-04 14:49:17 -0800 | [diff] [blame] | 101 | int port() const { return port_; } |
| Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame^] | 102 | int cores() const { return cores_;} |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 103 | static std::shared_ptr<ServerCredentials> CreateServerCredentials( |
| 104 | const ServerConfig& config) { |
| vjpai | 780a7f2 | 2015-11-04 00:19:09 -0800 | [diff] [blame] | 105 | if (config.has_security_params()) { |
| 106 | SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key, |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 107 | test_server1_cert}; |
| vjpai | 780a7f2 | 2015-11-04 00:19:09 -0800 | [diff] [blame] | 108 | SslServerCredentialsOptions ssl_opts; |
| 109 | ssl_opts.pem_root_certs = ""; |
| 110 | ssl_opts.pem_key_cert_pairs.push_back(pkcp); |
| 111 | return SslServerCredentials(ssl_opts); |
| 112 | } else { |
| 113 | return InsecureServerCredentials(); |
| 114 | } |
| 115 | } |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 116 | |
| Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 117 | private: |
| vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 118 | int port_; |
| Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame^] | 119 | int cores_; |
| Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 120 | std::unique_ptr<Timer> timer_; |
| Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 123 | std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config); |
| 124 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config); |
| Craig Tiller | 26598a3 | 2015-03-02 16:16:00 -0800 | [diff] [blame] | 125 | |
| 126 | } // namespace testing |
| 127 | } // namespace grpc |
| 128 | |
| 129 | #endif |