blob: 6e81edc8ffe40e9ba612ccd1a01ffcb562ab7fd0 [file] [log] [blame]
Craig Tiller26598a32015-03-02 16:16:00 -08001/*
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
vjpaid08a7382015-11-02 16:45:08 -080037#include <grpc/support/cpu.h>
vjpai780a7f22015-11-04 00:19:09 -080038#include <grpc++/security/server_credentials.h>
vjpaid08a7382015-11-02 16:45:08 -080039
vjpai780a7f22015-11-04 00:19:09 -080040#include "test/core/end2end/data/ssl_test_data.h"
vjpai72a63322015-10-29 02:23:11 -070041#include "test/core/util/port.h"
Craig Tillerd6479d62015-03-04 12:50:11 -080042#include "test/cpp/qps/timer.h"
vjpai119c1032015-10-29 01:21:04 -070043#include "test/proto/messages.grpc.pb.h"
vjpaid08a7382015-11-02 16:45:08 -080044#include "test/proto/benchmarks/control.grpc.pb.h"
Craig Tiller26598a32015-03-02 16:16:00 -080045
46namespace grpc {
47namespace testing {
48
49class Server {
50 public:
vjpai72a63322015-10-29 02:23:11 -070051 explicit Server(const ServerConfig& config) : timer_(new Timer) {
52 if (config.port()) {
53 port_ = config.port();
54 } else {
55 port_ = grpc_pick_unused_port_or_die();
56 }
57 }
Craig Tiller26598a32015-03-02 16:16:00 -080058 virtual ~Server() {}
Craig Tiller6af9ed02015-03-02 22:42:10 -080059
vjpai119c1032015-10-29 01:21:04 -070060 ServerStats Mark(bool reset) {
61 Timer::Result timer_result;
62 if (reset) {
63 std::unique_ptr<Timer> timer(new Timer);
64 timer.swap(timer_);
65 timer_result = timer->Mark();
66 } else {
67 timer_result = timer_->Mark();
68 }
Craig Tillerd6479d62015-03-04 12:50:11 -080069
70 ServerStats stats;
71 stats.set_time_elapsed(timer_result.wall);
72 stats.set_time_system(timer_result.system);
73 stats.set_time_user(timer_result.user);
74 return stats;
75 }
76
Craig Tillera182bf12015-03-04 13:54:39 -080077 static bool SetPayload(PayloadType type, int size, Payload* payload) {
78 PayloadType response_type = type;
79 // TODO(yangg): Support UNCOMPRESSABLE payload.
80 if (type != PayloadType::COMPRESSABLE) {
81 return false;
82 }
83 payload->set_type(response_type);
84 std::unique_ptr<char[]> body(new char[size]());
85 payload->set_body(body.get(), size);
86 return true;
87 }
Craig Tillerd6479d62015-03-04 12:50:11 -080088
vjpaifba20c92015-11-04 14:49:17 -080089 int port() const { return port_; }
90 int cores() const { return gpr_cpu_num_cores(); }
Vijay Paice846702015-11-04 00:30:12 -080091 static std::shared_ptr<ServerCredentials> CreateServerCredentials(
92 const ServerConfig& config) {
vjpai780a7f22015-11-04 00:19:09 -080093 if (config.has_security_params()) {
94 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
Vijay Paice846702015-11-04 00:30:12 -080095 test_server1_cert};
vjpai780a7f22015-11-04 00:19:09 -080096 SslServerCredentialsOptions ssl_opts;
97 ssl_opts.pem_root_certs = "";
98 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
99 return SslServerCredentials(ssl_opts);
100 } else {
101 return InsecureServerCredentials();
102 }
103 }
Vijay Paice846702015-11-04 00:30:12 -0800104
Craig Tillerd6479d62015-03-04 12:50:11 -0800105 private:
vjpai72a63322015-10-29 02:23:11 -0700106 int port_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800107 std::unique_ptr<Timer> timer_;
Craig Tiller26598a32015-03-02 16:16:00 -0800108};
109
vjpai72a63322015-10-29 02:23:11 -0700110std::unique_ptr<Server> CreateSynchronousServer(const ServerConfig& config);
111std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config);
Craig Tiller26598a32015-03-02 16:16:00 -0800112
113} // namespace testing
114} // namespace grpc
115
116#endif