1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include <stdio.h>
#include <signal.h>
#include "benchmark_pb.srpc.h"
#include "benchmark_thrift.srpc.h"
#include "workflow/WFFacilities.h"
#ifdef _WIN32
#include "workflow/PlatformSocket.h"
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
using namespace srpc;
std::atomic<int> query_count(0); // per_second
std::atomic<long long> last_timestamp(0L);
//volatile bool stop_flag = false;
int max_qps = 0;
long long total_count = 0;
WFFacilities::WaitGroup wait_group(1);
inline void collect_qps()
{
int64_t ms_timestamp = GET_CURRENT_MS();
++query_count;
if (ms_timestamp / 1000 > last_timestamp)
{
last_timestamp = ms_timestamp / 1000;
int count = query_count;
query_count = 0;
total_count += count;
if (count > max_qps)
max_qps = count;
long long ts = ms_timestamp;
fprintf(stdout, "TIMESTAMP(ms) = %llu QPS = %d\n", ts, count);
}
}
class BenchmarkPBServiceImpl : public BenchmarkPB::Service
{
public:
void echo_pb(FixLengthPBMsg *request, EmptyPBMsg *response,
RPCContext *ctx) override
{
collect_qps();
}
void slow_pb(FixLengthPBMsg *request, EmptyPBMsg *response,
RPCContext *ctx) override
{
auto *task = WFTaskFactory::create_timer_task(15000, nullptr);
ctx->get_series()->push_back(task);
}
};
class BenchmarkThriftServiceImpl : public BenchmarkThrift::Service
{
public:
void echo_thrift(BenchmarkThrift::echo_thriftRequest *request,
BenchmarkThrift::echo_thriftResponse *response,
RPCContext *ctx) override
{
collect_qps();
}
void slow_thrift(BenchmarkThrift::slow_thriftRequest *request,
BenchmarkThrift::slow_thriftResponse *response,
RPCContext *ctx) override
{
auto *task = WFTaskFactory::create_timer_task(15000, nullptr);
ctx->get_series()->push_back(task);
}
};
static void sig_handler(int signo)
{
wait_group.done();
}
static inline int create_bind_socket(unsigned short port)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd >= 0)
{
struct sockaddr_in sin = { };
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr *)&sin, sizeof sin) >= 0)
return sockfd;
close(sockfd);
}
return -1;
}
static void run_srpc_server(unsigned short port, int proc_num)
{
RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
params.max_connections = 2048;
SRPCServer server(¶ms);
BenchmarkPBServiceImpl pb_impl;
BenchmarkThriftServiceImpl thrift_impl;
server.add_service(&pb_impl);
server.add_service(&thrift_impl);
int sockfd = create_bind_socket(port);
if (sockfd < 0)
{
perror("create socket");
exit(1);
}
while ((proc_num /= 2) != 0)
fork();
if (server.serve(sockfd) == 0)
{
wait_group.wait();
server.stop();
}
else
perror("server start");
close(sockfd);
}
template<class SERVER>
static void run_pb_server(unsigned short port, int proc_num)
{
RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
params.max_connections = 2048;
SERVER server(¶ms);
BenchmarkPBServiceImpl pb_impl;
server.add_service(&pb_impl);
int sockfd = create_bind_socket(port);
if (sockfd < 0)
{
perror("create socket");
exit(1);
}
while ((proc_num /= 2) != 0)
fork();
if (server.serve(sockfd) == 0)
{
wait_group.wait();
server.stop();
}
else
perror("server start");
close(sockfd);
}
template<class SERVER>
static void run_thrift_server(unsigned short port, int proc_num)
{
RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
params.max_connections = 2048;
SERVER server(¶ms);
BenchmarkThriftServiceImpl thrift_impl;
server.add_service(&thrift_impl);
int sockfd = create_bind_socket(port);
if (sockfd < 0)
{
perror("create socket");
exit(1);
}
while ((proc_num /= 2) != 0)
fork();
if (server.serve(sockfd) == 0)
{
wait_group.wait();
server.stop();
}
else
perror("server start");
close(sockfd);
}
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
int proc_num = 1;
if (argc == 4)
{
proc_num = atoi(argv[3]);
if (proc_num != 1 && proc_num != 2 && proc_num != 4 && proc_num != 8 && proc_num != 16)
{
fprintf(stderr, "Usage: %s <PORT> <srpc|brpc|thrift> [proc num (1/2/4/8/16)]\n", argv[0]);
abort();
}
}
else if (argc != 3)
{
fprintf(stderr, "Usage: %s <PORT> <srpc|brpc|thrift> [proc num (1/2/4/8/16)]\n", argv[0]);
abort();
}
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
WFGlobalSettings my = GLOBAL_SETTINGS_DEFAULT;
my.poller_threads = 16;
my.handler_threads = 16;
WORKFLOW_library_init(&my);
unsigned short port = atoi(argv[1]);
std::string server_type = argv[2];
if (server_type == "srpc")
run_srpc_server(port, proc_num);
else if (server_type == "brpc")
run_pb_server<BRPCServer>(port, proc_num);
else if (server_type == "thrift")
run_thrift_server<ThriftServer>(port, proc_num);
else if (server_type == "srpc_http")
run_pb_server<SRPCHttpServer>(port, proc_num);
else if (server_type == "thrift_http")
run_thrift_server<ThriftHttpServer>(port, proc_num);
else
abort();
fprintf(stdout, "\nTotal query: %llu max QPS: %d\n", total_count, max_qps);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
|