object_pool_unittest.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #define BAIDU_CLEAR_OBJECT_POOL_AFTER_ALL_THREADS_QUIT
  21. #include "butil/object_pool.h"
  22. namespace {
  23. struct MyObject {};
  24. int nfoo_dtor = 0;
  25. struct Foo {
  26. Foo() {
  27. x = rand() % 2;
  28. }
  29. ~Foo() {
  30. ++nfoo_dtor;
  31. }
  32. int x;
  33. };
  34. }
  35. namespace butil {
  36. template <> struct ObjectPoolBlockMaxSize<MyObject> {
  37. static const size_t value = 128;
  38. };
  39. template <> struct ObjectPoolBlockMaxItem<MyObject> {
  40. static const size_t value = 3;
  41. };
  42. template <> struct ObjectPoolFreeChunkMaxItem<MyObject> {
  43. static size_t value() { return 5; }
  44. };
  45. template <> struct ObjectPoolValidator<Foo> {
  46. static bool validate(const Foo* foo) {
  47. return foo->x != 0;
  48. }
  49. };
  50. }
  51. namespace {
  52. using namespace butil;
  53. class ObjectPoolTest : public ::testing::Test{
  54. protected:
  55. ObjectPoolTest(){
  56. };
  57. virtual ~ObjectPoolTest(){};
  58. virtual void SetUp() {
  59. srand(time(0));
  60. };
  61. virtual void TearDown() {
  62. };
  63. };
  64. int nc = 0;
  65. int nd = 0;
  66. std::set<void*> ptr_set;
  67. struct YellObj {
  68. YellObj() {
  69. ++nc;
  70. ptr_set.insert(this);
  71. printf("Created %p\n", this);
  72. }
  73. ~YellObj() {
  74. ++nd;
  75. ptr_set.erase(this);
  76. printf("Destroyed %p\n", this);
  77. }
  78. char _dummy[96];
  79. };
  80. TEST_F(ObjectPoolTest, change_config) {
  81. int a[2];
  82. printf("%lu\n", ARRAY_SIZE(a));
  83. ObjectPoolInfo info = describe_objects<MyObject>();
  84. ObjectPoolInfo zero_info = { 0, 0, 0, 0, 3, 3, 0 };
  85. ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));
  86. MyObject* p = get_object<MyObject>();
  87. std::cout << describe_objects<MyObject>() << std::endl;
  88. return_object(p);
  89. std::cout << describe_objects<MyObject>() << std::endl;
  90. }
  91. struct NonDefaultCtorObject {
  92. explicit NonDefaultCtorObject(int value) : _value(value) {}
  93. NonDefaultCtorObject(int value, int dummy) : _value(value + dummy) {}
  94. int _value;
  95. };
  96. TEST_F(ObjectPoolTest, sanity) {
  97. ptr_set.clear();
  98. NonDefaultCtorObject* p1 = get_object<NonDefaultCtorObject>(10);
  99. ASSERT_EQ(10, p1->_value);
  100. NonDefaultCtorObject* p2 = get_object<NonDefaultCtorObject>(100, 30);
  101. ASSERT_EQ(130, p2->_value);
  102. printf("BLOCK_NITEM=%lu\n", ObjectPool<YellObj>::BLOCK_NITEM);
  103. nc = 0;
  104. nd = 0;
  105. {
  106. YellObj* o1 = get_object<YellObj>();
  107. ASSERT_TRUE(o1);
  108. ASSERT_EQ(1, nc);
  109. ASSERT_EQ(0, nd);
  110. YellObj* o2 = get_object<YellObj>();
  111. ASSERT_TRUE(o2);
  112. ASSERT_EQ(2, nc);
  113. ASSERT_EQ(0, nd);
  114. return_object(o1);
  115. ASSERT_EQ(2, nc);
  116. ASSERT_EQ(0, nd);
  117. return_object(o2);
  118. ASSERT_EQ(2, nc);
  119. ASSERT_EQ(0, nd);
  120. }
  121. ASSERT_EQ(0, nd);
  122. clear_objects<YellObj>();
  123. ASSERT_EQ(2, nd);
  124. ASSERT_TRUE(ptr_set.empty()) << ptr_set.size();
  125. }
  126. TEST_F(ObjectPoolTest, validator) {
  127. nfoo_dtor = 0;
  128. int nfoo = 0;
  129. for (int i = 0; i < 100; ++i) {
  130. Foo* foo = get_object<Foo>();
  131. if (foo) {
  132. ASSERT_EQ(1, foo->x);
  133. ++nfoo;
  134. }
  135. }
  136. ASSERT_EQ(nfoo + nfoo_dtor, 100);
  137. ASSERT_EQ((size_t)nfoo, describe_objects<Foo>().item_num);
  138. }
  139. TEST_F(ObjectPoolTest, get_int) {
  140. clear_objects<int>();
  141. // Perf of this test is affected by previous case.
  142. const size_t N = 100000;
  143. butil::Timer tm;
  144. // warm up
  145. int* p = get_object<int>();
  146. *p = 0;
  147. return_object(p);
  148. delete (new int);
  149. tm.start();
  150. for (size_t i = 0; i < N; ++i) {
  151. *get_object<int>() = i;
  152. }
  153. tm.stop();
  154. printf("get a int takes %.1fns\n", tm.n_elapsed()/(double)N);
  155. tm.start();
  156. for (size_t i = 0; i < N; ++i) {
  157. *(new int) = i;
  158. }
  159. tm.stop();
  160. printf("new a int takes %" PRId64 "ns\n", tm.n_elapsed()/N);
  161. std::cout << describe_objects<int>() << std::endl;
  162. clear_objects<int>();
  163. std::cout << describe_objects<int>() << std::endl;
  164. }
  165. struct SilentObj {
  166. char buf[sizeof(YellObj)];
  167. };
  168. TEST_F(ObjectPoolTest, get_perf) {
  169. const size_t N = 10000;
  170. std::vector<SilentObj*> new_list;
  171. new_list.reserve(N);
  172. butil::Timer tm1, tm2;
  173. // warm up
  174. return_object(get_object<SilentObj>());
  175. delete (new SilentObj);
  176. // Run twice, the second time will be must faster.
  177. for (size_t j = 0; j < 2; ++j) {
  178. tm1.start();
  179. for (size_t i = 0; i < N; ++i) {
  180. get_object<SilentObj>();
  181. }
  182. tm1.stop();
  183. printf("get a SilentObj takes %" PRId64 "ns\n", tm1.n_elapsed()/N);
  184. //clear_objects<SilentObj>(); // free all blocks
  185. tm2.start();
  186. for (size_t i = 0; i < N; ++i) {
  187. new_list.push_back(new SilentObj);
  188. }
  189. tm2.stop();
  190. printf("new a SilentObj takes %" PRId64 "ns\n", tm2.n_elapsed()/N);
  191. for (size_t i = 0; i < new_list.size(); ++i) {
  192. delete new_list[i];
  193. }
  194. new_list.clear();
  195. }
  196. std::cout << describe_objects<SilentObj>() << std::endl;
  197. }
  198. struct D { int val[1]; };
  199. void* get_and_return_int(void*) {
  200. // Perf of this test is affected by previous case.
  201. const size_t N = 100000;
  202. std::vector<D*> v;
  203. v.reserve(N);
  204. butil::Timer tm0, tm1, tm2;
  205. D tmp = D();
  206. int sr = 0;
  207. // warm up
  208. tm0.start();
  209. return_object(get_object<D>());
  210. tm0.stop();
  211. printf("[%lu] warmup=%" PRId64 "\n", (size_t)pthread_self(), tm0.n_elapsed());
  212. for (int j = 0; j < 5; ++j) {
  213. v.clear();
  214. sr = 0;
  215. tm1.start();
  216. for (size_t i = 0; i < N; ++i) {
  217. D* p = get_object<D>();
  218. *p = tmp;
  219. v.push_back(p);
  220. }
  221. tm1.stop();
  222. std::random_shuffle(v.begin(), v.end());
  223. tm2.start();
  224. for (size_t i = 0; i < v.size(); ++i) {
  225. sr += return_object(v[i]);
  226. }
  227. tm2.stop();
  228. if (0 != sr) {
  229. printf("%d return_object failed\n", sr);
  230. }
  231. printf("[%lu:%d] get<D>=%.1f return<D>=%.1f\n",
  232. (size_t)pthread_self(), j, tm1.n_elapsed()/(double)N,
  233. tm2.n_elapsed()/(double)N);
  234. }
  235. return NULL;
  236. }
  237. void* new_and_delete_int(void*) {
  238. const size_t N = 100000;
  239. std::vector<D*> v2;
  240. v2.reserve(N);
  241. butil::Timer tm0, tm1, tm2;
  242. D tmp = D();
  243. for (int j = 0; j < 3; ++j) {
  244. v2.clear();
  245. // warm up
  246. delete (new D);
  247. tm1.start();
  248. for (size_t i = 0; i < N; ++i) {
  249. D *p = new D;
  250. *p = tmp;
  251. v2.push_back(p);
  252. }
  253. tm1.stop();
  254. std::random_shuffle(v2.begin(), v2.end());
  255. tm2.start();
  256. for (size_t i = 0; i < v2.size(); ++i) {
  257. delete v2[i];
  258. }
  259. tm2.stop();
  260. printf("[%lu:%d] new<D>=%.1f delete<D>=%.1f\n",
  261. (size_t)pthread_self(), j, tm1.n_elapsed()/(double)N,
  262. tm2.n_elapsed()/(double)N);
  263. }
  264. return NULL;
  265. }
  266. TEST_F(ObjectPoolTest, get_and_return_int_single_thread) {
  267. get_and_return_int(NULL);
  268. new_and_delete_int(NULL);
  269. }
  270. TEST_F(ObjectPoolTest, get_and_return_int_multiple_threads) {
  271. pthread_t tid[16];
  272. for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
  273. ASSERT_EQ(0, pthread_create(&tid[i], NULL, get_and_return_int, NULL));
  274. }
  275. for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
  276. pthread_join(tid[i], NULL);
  277. }
  278. pthread_t tid2[16];
  279. for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
  280. ASSERT_EQ(0, pthread_create(&tid2[i], NULL, new_and_delete_int, NULL));
  281. }
  282. for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
  283. pthread_join(tid2[i], NULL);
  284. }
  285. std::cout << describe_objects<D>() << std::endl;
  286. clear_objects<D>();
  287. ObjectPoolInfo info = describe_objects<D>();
  288. ObjectPoolInfo zero_info = { 0, 0, 0, 0, ObjectPoolBlockMaxItem<D>::value,
  289. ObjectPoolBlockMaxItem<D>::value, 0 };
  290. ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));
  291. }
  292. TEST_F(ObjectPoolTest, verify_get) {
  293. clear_objects<int>();
  294. std::cout << describe_objects<int>() << std::endl;
  295. std::vector<int*> v;
  296. v.reserve(100000);
  297. for (int i = 0; (size_t)i < v.capacity(); ++i) {
  298. int* p = get_object<int>();
  299. *p = i;
  300. v.push_back(p);
  301. }
  302. int i;
  303. for (i = 0; (size_t)i < v.size() && *v[i] == i; ++i);
  304. ASSERT_EQ(v.size(), (size_t)i) << "i=" << i << ", " << *v[i];
  305. clear_objects<int>();
  306. }
  307. } // namespace