MCPcopy Create free account
hub / github.com/apache/brpc / ThreadKeyPerfTest

Function ThreadKeyPerfTest

test/thread_key_unittest.cpp:359–414  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

357
358
359void ThreadKeyPerfTest(int thread_num, bool test_pthread_key) {
360 g_started = false;
361 g_stopped = false;
362 pthread_key_t pthread_key;
363 butil::ThreadKey thread_key;
364 if (test_pthread_key) {
365 ASSERT_EQ(0, pthread_key_create(&pthread_key, NULL));
366 } else {
367 ASSERT_EQ(0, butil::thread_key_create(thread_key, NULL));
368 }
369 pthread_t threads[thread_num];
370 std::vector<ThreadKeyPerfArgs> args(thread_num);
371 for (int i = 0; i < thread_num; ++i) {
372 if (test_pthread_key) {
373 args[i].pthread_key = pthread_key;
374 args[i].is_pthread_key = true;
375 } else {
376 args[i].thread_key = &thread_key;
377 args[i].is_pthread_key = false;
378 }
379 ASSERT_EQ(0, pthread_create(&threads[i], NULL, ThreadKeyPerfFunc, &args[i]));
380 }
381 while (true) {
382 bool all_ready = true;
383 for (int i = 0; i < thread_num; ++i) {
384 if (!args[i].ready) {
385 all_ready = false;
386 break;
387 }
388 }
389 if (all_ready) {
390 break;
391 }
392 usleep(1000);
393 }
394 g_started = true;
395 int64_t run_ms = 5 * 1000;
396 usleep(run_ms * 1000);
397 g_stopped = true;
398 int64_t wait_time = 0;
399 int64_t count = 0;
400 for (int i = 0; i < thread_num; ++i) {
401 pthread_join(threads[i], NULL);
402 wait_time += args[i].elapse_ns;
403 count += args[i].counter;
404 }
405 if (test_pthread_key) {
406 ASSERT_EQ(0, pthread_key_delete(pthread_key));
407 } else {
408 ASSERT_EQ(0, butil::thread_key_delete(thread_key));
409 }
410 LOG(INFO) << (test_pthread_key ? "pthread_key" : "thread_key")
411 << " thread_num=" << thread_num
412 << " count=" << count
413 << " average_time=" << wait_time / (double)count;
414}
415
416struct BAIDU_CACHELINE_ALIGNMENT ThreadLocalPerfArgs {

Callers 1

TESTFunction · 0.85

Calls 2

thread_key_createFunction · 0.85
thread_key_deleteFunction · 0.85

Tested by

no test coverage detected