| 218 | static butil::atomic<int> g_thread_count(0); |
| 219 | |
| 220 | void RpcPress::sync_client() { |
| 221 | double req_rate = _options.test_req_rate / _options.test_thread_num; |
| 222 | //max make up time is 5 s |
| 223 | if (_msgs.empty()) { |
| 224 | LOG(ERROR) << "nothing to send!"; |
| 225 | return; |
| 226 | } |
| 227 | const int thread_index = g_thread_count.fetch_add(1, butil::memory_order_relaxed); |
| 228 | int msg_index = thread_index; |
| 229 | int64_t last_expected_time = butil::monotonic_time_ns(); |
| 230 | const int64_t interval = (int64_t) (1000000000L / req_rate); |
| 231 | // the max tolerant delay between end_time and expected_time. 10ms or 10 intervals |
| 232 | int64_t max_tolerant_delay = std::max((int64_t) 10000000L, 10 * interval); |
| 233 | while (!_stop) { |
| 234 | brpc::Controller* cntl = new brpc::Controller; |
| 235 | msg_index = (msg_index + _options.test_thread_num) % _msgs.size(); |
| 236 | Message* request = _msgs[msg_index]; |
| 237 | Message* response = _pbrpc_client->get_output_message(); |
| 238 | const int64_t start_time = butil::cpuwide_time_us(); |
| 239 | google::protobuf::Closure* done = brpc::NewCallback< |
| 240 | RpcPress, |
| 241 | RpcPress*, |
| 242 | brpc::Controller*, |
| 243 | Message*, |
| 244 | Message*, int64_t> |
| 245 | (this, &RpcPress::handle_response, cntl, request, response, start_time); |
| 246 | const brpc::CallId cid1 = cntl->call_id(); |
| 247 | _pbrpc_client->call_method(cntl, request, response, done); |
| 248 | _sent_count << 1; |
| 249 | |
| 250 | if (_options.test_req_rate <= 0) { |
| 251 | brpc::Join(cid1); |
| 252 | } else { |
| 253 | int64_t end_time = butil::monotonic_time_ns(); |
| 254 | int64_t expected_time = last_expected_time + interval; |
| 255 | if (end_time < expected_time) { |
| 256 | usleep((expected_time - end_time)/1000); |
| 257 | } |
| 258 | if (end_time - expected_time > max_tolerant_delay) { |
| 259 | expected_time = end_time; |
| 260 | } |
| 261 | last_expected_time = expected_time; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | int RpcPress::start() { |
| 267 | _ttid.resize(_options.test_thread_num); |
no test coverage detected