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
|
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <string>
#include <atomic>
#include <chrono>
#include "benchmark_pb.srpc.h"
#include "benchmark_thrift.srpc.h"
using namespace srpc;
#define TEST_SECOND 20
#define GET_CURRENT_NS std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()
std::atomic<int> query_count(0);
std::atomic<int> success_count(0);
std::atomic<int> error_count(0);
std::atomic<int64_t> latency_sum(0);
volatile bool stop_flag = false;
int PARALLEL_NUMBER;
std::string request_msg;
template<class CLIENT>
static void do_echo_pb(CLIENT *client)
{
FixLengthPBMsg req;
req.set_msg(request_msg);
int64_t ns_st = GET_CURRENT_NS;
++query_count;
client->echo_pb(&req, [client, ns_st](EmptyPBMsg *response, RPCContext *ctx) {
if (ctx->success())
{
//printf("%s\n", ctx->get_remote_ip().c_str());
latency_sum += GET_CURRENT_NS - ns_st;
++success_count;
}
else
{
printf("status[%d] error[%d] errmsg:%s\n",
ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
++error_count;
}
if (!stop_flag)
do_echo_pb<CLIENT>(client);
//printf("echo done. seq_id=%d\n", ctx->get_task_seq());
});
}
template<class CLIENT>
static void do_echo_thrift(CLIENT *client)
{
BenchmarkThrift::echo_thriftRequest req;
req.msg = request_msg;
int64_t ns_st = GET_CURRENT_NS;
++query_count;
client->echo_thrift(&req, [client, ns_st](BenchmarkThrift::echo_thriftResponse *response, RPCContext *ctx) {
if (ctx->success())
{
//printf("%s\n", ctx->get_remote_ip().c_str());
latency_sum += GET_CURRENT_NS - ns_st;
++success_count;
}
else
{
printf("status[%d] error[%d] errmsg:%s \n",
ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
++error_count;
}
if (!stop_flag)
do_echo_thrift<CLIENT>(client);
//printf("echo done. seq_id=%d\n", ctx->get_task_seq());
});
}
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 7)
{
fprintf(stderr, "Usage: %s <IP> <PORT> <srpc|brpc|thrift> <pb|thrift> <PARALLEL_NUMBER> <REQUEST_BYTES>\n", argv[0]);
abort();
}
WFGlobalSettings setting = GLOBAL_SETTINGS_DEFAULT;
setting.endpoint_params.max_connections = 2048;
setting.poller_threads = 16;
setting.handler_threads = 16;
WORKFLOW_library_init(&setting);
RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
client_params.task_params.keep_alive_timeout = -1;
client_params.host = argv[1];
client_params.port = atoi(argv[2]);
std::string server_type = argv[3];
std::string idl_type = argv[4];
PARALLEL_NUMBER = atoi(argv[5]);
int REQUEST_BYTES = atoi(argv[6]);
request_msg.resize(REQUEST_BYTES, 'r');
//for (int i = 0; i < REQUEST_BYTES; i++)
// request_msg[i] = (unsigned char)(rand() % 256);
int64_t start = GET_CURRENT_MS();
if (server_type == "srpc")
{
if (idl_type == "pb")
{
auto *client = new BenchmarkPB::SRPCClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
do_echo_pb(client);
}
else if (idl_type == "thrift")
{
auto *client = new BenchmarkThrift::SRPCClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
do_echo_thrift(client);
}
else
abort();
}
else if (server_type == "brpc")
{
auto *client = new BenchmarkPB::BRPCClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
{
if (idl_type == "pb")
do_echo_pb(client);
else if (idl_type == "thrift")
abort();
else
abort();
}
}
else if (server_type == "thrift")
{
auto *client = new BenchmarkThrift::ThriftClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
{
if (idl_type == "pb")
abort();
else if (idl_type == "thrift")
do_echo_thrift(client);
else
abort();
}
}
else if (server_type == "srpc_http")
{
if (idl_type == "pb")
{
auto *client = new BenchmarkPB::SRPCHttpClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
do_echo_pb(client);
}
else if (idl_type == "thrift")
{
auto *client = new BenchmarkThrift::SRPCHttpClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
do_echo_thrift(client);
}
else
abort();
}
else if (server_type == "thrift_http")
{
auto *client = new BenchmarkThrift::ThriftHttpClient(&client_params);
for (int i = 0; i < PARALLEL_NUMBER; i++)
{
if (idl_type == "pb")
abort();
else if (idl_type == "thrift")
do_echo_thrift(client);
else
abort();
}
}
else
abort();
std::this_thread::sleep_for(std::chrono::seconds(TEST_SECOND));
stop_flag = true;
int64_t end = GET_CURRENT_MS();
int tot = query_count;
int s = success_count;
int e = error_count;
int64_t l = latency_sum;
fprintf(stderr, "\nquery\t%d\ttimes, %d success, %d error.\n", tot, s, e);
fprintf(stderr, "total\t%.3lf\tseconds\n", (end - start) / 1000.0);
fprintf(stderr, "qps=%.0lf\n", tot * 1000.0 / (end - start));
fprintf(stderr, "latency=%.0lfus\n", s > 0 ? l * 1.0 / s / 1000 : 0);
std::this_thread::sleep_for(std::chrono::seconds(1));
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
|