| 446 | } |
| 447 | |
| 448 | void ThreadLocalPerfTest(int thread_num) { |
| 449 | g_started = false; |
| 450 | g_stopped = false; |
| 451 | ThreadLocal<int> tl; |
| 452 | pthread_t threads[thread_num]; |
| 453 | std::vector<ThreadLocalPerfArgs> args(thread_num); |
| 454 | for (int i = 0; i < thread_num; ++i) { |
| 455 | args[i].tl = &tl; |
| 456 | ASSERT_EQ(0, pthread_create(&threads[i], NULL, ThreadLocalPerfFunc, &args[i])); |
| 457 | } |
| 458 | while (true) { |
| 459 | bool all_ready = true; |
| 460 | for (int i = 0; i < thread_num; ++i) { |
| 461 | if (!args[i].ready) { |
| 462 | all_ready = false; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | if (all_ready) { |
| 467 | break; |
| 468 | } |
| 469 | usleep(1000); |
| 470 | } |
| 471 | g_started = true; |
| 472 | int64_t run_ms = 5 * 1000; |
| 473 | usleep(run_ms * 1000); |
| 474 | g_stopped = true; |
| 475 | int64_t wait_time = 0; |
| 476 | int64_t count = 0; |
| 477 | for (int i = 0; i < thread_num; ++i) { |
| 478 | pthread_join(threads[i], NULL); |
| 479 | wait_time += args[i].elapse_ns; |
| 480 | count += args[i].counter; |
| 481 | } |
| 482 | LOG(INFO) << "ThreadLocal thread_num=" << thread_num |
| 483 | << " count=" << count |
| 484 | << " average_time=" << wait_time / (double)count; |
| 485 | } |
| 486 | |
| 487 | TEST(ThreadLocalTest, thread_key_performance) { |
| 488 | int thread_num = 1; |