/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: bsl_thread_hash.cpp,v 1.2 2008/11/12 04:13:35 xiaowei Exp $ * **************************************************************************/ /** * @file bsl_thread_hash.cpp * @author xiaowei(com@baidu.com) * @date 2008/08/26 11:45:39 * @version $Revision: 1.2 $ * @brief * **/ #include #include #include #include #include #include #include #include #include template struct type_t { char data[size]; }; struct point_t { size_t hash_bucket; size_t hash_datanum; int thread_num; int value_size; long query_loop; int insert_speed; std::string hash_type; public: point_t() { hash_bucket = 0; hash_datanum = 0; thread_num = 0; value_size = 0; query_loop = 0; insert_speed = 0; hash_type = ""; } }; int **g_randsize = 0; int g_size = 0; pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; int g_pos = 0; int g_num = 0; void randint(int num, int data) { g_randsize = (int **) malloc (sizeof(int *) * num); for (int i=0; i= g_num) g_pos = 0; int *val = g_randsize[g_pos]; ++ g_pos; pthread_mutex_unlock(&g_lock); return val; } template struct test_query_t { hash_t & _1; Slotime _2; point_t & _3; public: test_query_t(hash_t & h, point_t &p) : _1(h), _2(p.thread_num), _3(p) {} }; template void * test_insert_r(void *param) { test_query_t *fd = (test_query_t *)param; timeval s, e; gettimeofday(&s, NULL); for (int i=0; i_3.hash_datanum; ++i) { fd->_1.set(rand(), value_type()); if ((i%fd->_3.insert_speed) == fd->_3.insert_speed - 1) { gettimeofday(&e, NULL); long used = XTIMEDIFF(s, e); if (used < 1000) { timespec t = { (1000-used) / 1000, (1000-used) * 1000000 }; timespec e; nanosleep(&t, &e); } gettimeofday(&s, NULL); } } return NULL; } template void * test_query_r(void *arg) { test_query_t *fd = (test_query_t *)arg; type_t v; int *val = getrand(); long loop = fd->_3.query_loop / (long)fd->_3.thread_num; fd->_2.start(); for (long i=0; i_1.get(val[i%g_size], &v); } fd->_2.stop(); return NULL; } unsigned int ans_hash_fcnt(const long & v) { return v; } template void test(hash_t &hash, point_t &p) { for (int i=0; i<(int)((float)p.hash_datanum/(1.5f)); ++i) { hash.set(rand(), stype_t()); } test_query_t qval(hash, p); test_query_t uval(hash, p); xthread_t ptr[] = { {test_query_r, &qval, p.thread_num}, {test_insert_r, &uval, 1}, }; run_thread(ptr, 2); double speed = (double)(p.query_loop) / ((float)(qval._2.cost()) + 1e-10); char flag[1024]; snprintf(flag, sizeof(flag), "speed(%s)", p.hash_type.c_str()); //std::cout< void testcase(point_t &p) { //ans::hashmap { std::cout<<"ans_hashmap "< > anshash1_t; anshash1_t hash; hash.create(p.hash_bucket, ans_hash_fcnt); p.hash_type = "ans_hashmap"; test >(hash, p); std::cout< > bslhash1_t; bslhash1_t hash; hash.create(p.hash_bucket); p.hash_type = "bsl_phashmap"; test >(hash, p); std::cout<("hash_bucket"); p.hash_datanum = pb::getopt("hash_datanum"); p.thread_num = pb::getopt("thread_num"); p.value_size = pb::getopt("value_size"); p.query_loop = pb::getopt("query_loop"); p.query_loop *= 1000000; p.insert_speed = pb::getopt("insert_speed"); if (p.value_size <= 4) { testcase (p); } else if (p.value_size <= 8) { testcase (p); } else if (p.value_size < 32) { testcase (p); } else { testcase (p); } return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 */