resource_pool_unittest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include "butil/time.h"
  19. #include "butil/macros.h"
  20. #include "butil/fast_rand.h"
  21. #define BAIDU_CLEAR_RESOURCE_POOL_AFTER_ALL_THREADS_QUIT
  22. #include "butil/resource_pool.h"
  23. namespace {
  24. struct MyObject {};
  25. int nfoo_dtor = 0;
  26. struct Foo {
  27. Foo() {
  28. x = butil::fast_rand() % 2;
  29. }
  30. ~Foo() {
  31. ++nfoo_dtor;
  32. }
  33. int x;
  34. };
  35. }
  36. namespace butil {
  37. template <> struct ResourcePoolBlockMaxSize<MyObject> {
  38. static const size_t value = 128;
  39. };
  40. template <> struct ResourcePoolBlockMaxItem<MyObject> {
  41. static const size_t value = 3;
  42. };
  43. template <> struct ResourcePoolFreeChunkMaxItem<MyObject> {
  44. static size_t value() { return 5; }
  45. };
  46. template <> struct ResourcePoolValidator<Foo> {
  47. static bool validate(const Foo* foo) {
  48. return foo->x != 0;
  49. }
  50. };
  51. }
  52. namespace {
  53. using namespace butil;
  54. class ResourcePoolTest : public ::testing::Test{
  55. protected:
  56. ResourcePoolTest(){
  57. };
  58. virtual ~ResourcePoolTest(){};
  59. virtual void SetUp() {
  60. srand(time(0));
  61. };
  62. virtual void TearDown() {
  63. };
  64. };
  65. TEST_F(ResourcePoolTest, atomic_array_init) {
  66. for (int i = 0; i < 2; ++i) {
  67. if (i == 0) {
  68. butil::atomic<int> a[2];
  69. a[0] = 1;
  70. // The folowing will cause compile error with gcc3.4.5 and the
  71. // reason is unknown
  72. // a[1] = 2;
  73. } else if (i == 2) {
  74. butil::atomic<int> a[2];
  75. ASSERT_EQ(0, a[0]);
  76. ASSERT_EQ(0, a[1]);
  77. }
  78. }
  79. }
  80. int nc = 0;
  81. int nd = 0;
  82. std::set<void*> ptr_set;
  83. struct YellObj {
  84. YellObj() {
  85. ++nc;
  86. ptr_set.insert(this);
  87. printf("Created %p\n", this);
  88. }
  89. ~YellObj() {
  90. ++nd;
  91. ptr_set.erase(this);
  92. printf("Destroyed %p\n", this);
  93. }
  94. char _dummy[96];
  95. };
  96. TEST_F(ResourcePoolTest, change_config) {
  97. int a[2];
  98. printf("%lu\n", ARRAY_SIZE(a));
  99. ResourcePoolInfo info = describe_resources<MyObject>();
  100. ResourcePoolInfo zero_info = { 0, 0, 0, 0, 3, 3, 0 };
  101. ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));
  102. ResourceId<MyObject> id = { 0 };
  103. get_resource<MyObject>(&id);
  104. std::cout << describe_resources<MyObject>() << std::endl;
  105. return_resource(id);
  106. std::cout << describe_resources<MyObject>() << std::endl;
  107. }
  108. struct NonDefaultCtorObject {
  109. explicit NonDefaultCtorObject(int value) : _value(value) {}
  110. NonDefaultCtorObject(int value, int dummy) : _value(value + dummy) {}
  111. int _value;
  112. };
  113. TEST_F(ResourcePoolTest, sanity) {
  114. ptr_set.clear();
  115. ResourceId<NonDefaultCtorObject> id0 = { 0 };
  116. get_resource<NonDefaultCtorObject>(&id0, 10);
  117. ASSERT_EQ(10, address_resource(id0)->_value);
  118. get_resource<NonDefaultCtorObject>(&id0, 100, 30);
  119. ASSERT_EQ(130, address_resource(id0)->_value);
  120. printf("BLOCK_NITEM=%lu\n", ResourcePool<YellObj>::BLOCK_NITEM);
  121. nc = 0;
  122. nd = 0;
  123. {
  124. ResourceId<YellObj> id1;
  125. YellObj* o1 = get_resource(&id1);
  126. ASSERT_TRUE(o1);
  127. ASSERT_EQ(o1, address_resource(id1));
  128. ASSERT_EQ(1, nc);
  129. ASSERT_EQ(0, nd);
  130. ResourceId<YellObj> id2;
  131. YellObj* o2 = get_resource(&id2);
  132. ASSERT_TRUE(o2);
  133. ASSERT_EQ(o2, address_resource(id2));
  134. ASSERT_EQ(2, nc);
  135. ASSERT_EQ(0, nd);
  136. return_resource(id1);
  137. ASSERT_EQ(2, nc);
  138. ASSERT_EQ(0, nd);
  139. return_resource(id2);
  140. ASSERT_EQ(2, nc);
  141. ASSERT_EQ(0, nd);
  142. }
  143. ASSERT_EQ(0, nd);
  144. clear_resources<YellObj>();
  145. ASSERT_EQ(2, nd);
  146. ASSERT_TRUE(ptr_set.empty()) << ptr_set.size();
  147. }
  148. TEST_F(ResourcePoolTest, validator) {
  149. nfoo_dtor = 0;
  150. int nfoo = 0;
  151. for (int i = 0; i < 100; ++i) {
  152. ResourceId<Foo> id = { 0 };
  153. Foo* foo = get_resource<Foo>(&id);
  154. if (foo) {
  155. ASSERT_EQ(1, foo->x);
  156. ++nfoo;
  157. }
  158. }
  159. ASSERT_EQ(nfoo + nfoo_dtor, 100);
  160. ASSERT_EQ((size_t)nfoo, describe_resources<Foo>().item_num);
  161. }
  162. TEST_F(ResourcePoolTest, get_int) {
  163. clear_resources<int>();
  164. // Perf of this test is affected by previous case.
  165. const size_t N = 100000;
  166. butil::Timer tm;
  167. ResourceId<int> id;
  168. // warm up
  169. if (get_resource(&id)) {
  170. return_resource(id);
  171. }
  172. ASSERT_EQ(0UL, id);
  173. delete (new int);
  174. tm.start();
  175. for (size_t i = 0; i < N; ++i) {
  176. *get_resource(&id) = i;
  177. }
  178. tm.stop();
  179. printf("get a int takes %.1fns\n", tm.n_elapsed()/(double)N);
  180. tm.start();
  181. for (size_t i = 0; i < N; ++i) {
  182. *(new int) = i;
  183. }
  184. tm.stop();
  185. printf("new a int takes %luns\n", tm.n_elapsed()/N);
  186. tm.start();
  187. for (size_t i = 0; i < N; ++i) {
  188. id.value = i;
  189. *ResourcePool<int>::unsafe_address_resource(id) = i;
  190. }
  191. tm.stop();
  192. printf("unsafe_address a int takes %.1fns\n", tm.n_elapsed()/(double)N);
  193. tm.start();
  194. for (size_t i = 0; i < N; ++i) {
  195. id.value = i;
  196. *address_resource(id) = i;
  197. }
  198. tm.stop();
  199. printf("address a int takes %.1fns\n", tm.n_elapsed()/(double)N);
  200. std::cout << describe_resources<int>() << std::endl;
  201. clear_resources<int>();
  202. std::cout << describe_resources<int>() << std::endl;
  203. }
  204. struct SilentObj {
  205. char buf[sizeof(YellObj)];
  206. };
  207. TEST_F(ResourcePoolTest, get_perf) {
  208. const size_t N = 10000;
  209. std::vector<SilentObj*> new_list;
  210. new_list.reserve(N);
  211. ResourceId<SilentObj> id;
  212. butil::Timer tm1, tm2;
  213. // warm up
  214. if (get_resource(&id)) {
  215. return_resource(id);
  216. }
  217. delete (new SilentObj);
  218. // Run twice, the second time will be must faster.
  219. for (size_t j = 0; j < 2; ++j) {
  220. tm1.start();
  221. for (size_t i = 0; i < N; ++i) {
  222. get_resource(&id);
  223. }
  224. tm1.stop();
  225. printf("get a SilentObj takes %luns\n", tm1.n_elapsed()/N);
  226. //clear_resources<SilentObj>(); // free all blocks
  227. tm2.start();
  228. for (size_t i = 0; i < N; ++i) {
  229. new_list.push_back(new SilentObj);
  230. }
  231. tm2.stop();
  232. printf("new a SilentObj takes %luns\n", tm2.n_elapsed()/N);
  233. for (size_t i = 0; i < new_list.size(); ++i) {
  234. delete new_list[i];
  235. }
  236. new_list.clear();
  237. }
  238. std::cout << describe_resources<SilentObj>() << std::endl;
  239. }
  240. struct D { int val[1]; };
  241. void* get_and_return_int(void*) {
  242. // Perf of this test is affected by previous case.
  243. const size_t N = 100000;
  244. std::vector<ResourceId<D> > v;
  245. v.reserve(N);
  246. butil::Timer tm0, tm1, tm2;
  247. ResourceId<D> id = {0};
  248. D tmp = D();
  249. int sr = 0;
  250. // warm up
  251. tm0.start();
  252. if (get_resource(&id)) {
  253. return_resource(id);
  254. }
  255. tm0.stop();
  256. printf("[%lu] warmup=%lu\n", pthread_self(), tm0.n_elapsed());
  257. for (int j = 0; j < 5; ++j) {
  258. v.clear();
  259. sr = 0;
  260. tm1.start();
  261. for (size_t i = 0; i < N; ++i) {
  262. *get_resource(&id) = tmp;
  263. v.push_back(id);
  264. }
  265. tm1.stop();
  266. std::random_shuffle(v.begin(), v.end());
  267. tm2.start();
  268. for (size_t i = 0; i < v.size(); ++i) {
  269. sr += return_resource(v[i]);
  270. }
  271. tm2.stop();
  272. if (0 != sr) {
  273. printf("%d return_resource failed\n", sr);
  274. }
  275. printf("[%lu:%d] get<D>=%.1f return<D>=%.1f\n",
  276. pthread_self(), j, tm1.n_elapsed()/(double)N, tm2.n_elapsed()/(double)N);
  277. }
  278. return NULL;
  279. }
  280. void* new_and_delete_int(void*) {
  281. const size_t N = 100000;
  282. std::vector<D*> v2;
  283. v2.reserve(N);
  284. butil::Timer tm0, tm1, tm2;
  285. D tmp = D();
  286. for (int j = 0; j < 3; ++j) {
  287. v2.clear();
  288. // warm up
  289. delete (new D);
  290. tm1.start();
  291. for (size_t i = 0; i < N; ++i) {
  292. D *p = new D;
  293. *p = tmp;
  294. v2.push_back(p);
  295. }
  296. tm1.stop();
  297. std::random_shuffle(v2.begin(), v2.end());
  298. tm2.start();
  299. for (size_t i = 0; i < v2.size(); ++i) {
  300. delete v2[i];
  301. }
  302. tm2.stop();
  303. printf("[%lu:%d] new<D>=%.1f delete<D>=%.1f\n",
  304. pthread_self(), j, tm1.n_elapsed()/(double)N, tm2.n_elapsed()/(double)N);
  305. }
  306. return NULL;
  307. }
  308. TEST_F(ResourcePoolTest, get_and_return_int_single_thread) {
  309. get_and_return_int(NULL);
  310. new_and_delete_int(NULL);
  311. }
  312. TEST_F(ResourcePoolTest, get_and_return_int_multiple_threads) {
  313. pthread_t tid[16];
  314. for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
  315. ASSERT_EQ(0, pthread_create(&tid[i], NULL, get_and_return_int, NULL));
  316. }
  317. for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
  318. pthread_join(tid[i], NULL);
  319. }
  320. pthread_t tid2[16];
  321. for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
  322. ASSERT_EQ(0, pthread_create(&tid2[i], NULL, new_and_delete_int, NULL));
  323. }
  324. for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
  325. pthread_join(tid2[i], NULL);
  326. }
  327. std::cout << describe_resources<D>() << std::endl;
  328. clear_resources<D>();
  329. ResourcePoolInfo info = describe_resources<D>();
  330. ResourcePoolInfo zero_info = { 0, 0, 0, 0,
  331. ResourcePoolBlockMaxItem<D>::value,
  332. ResourcePoolBlockMaxItem<D>::value, 0};
  333. ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));
  334. }
  335. TEST_F(ResourcePoolTest, verify_get) {
  336. clear_resources<int>();
  337. std::cout << describe_resources<int>() << std::endl;
  338. std::vector<std::pair<int*, ResourceId<int> > > v;
  339. v.reserve(100000);
  340. ResourceId<int> id = { 0 };
  341. for (int i = 0; (size_t)i < v.capacity(); ++i) {
  342. int* p = get_resource(&id);
  343. *p = i;
  344. v.push_back(std::make_pair(p, id));
  345. }
  346. int i;
  347. for (i = 0; (size_t)i < v.size() && *v[i].first == i; ++i);
  348. ASSERT_EQ(v.size(), (size_t)i);
  349. for (i = 0; (size_t)i < v.size() && v[i].second == (size_t)i; ++i);
  350. ASSERT_EQ(v.size(), (size_t)i) << "i=" << i << ", " << v[i].second;
  351. clear_resources<int>();
  352. }
  353. } // namespace