Skip to content

Commit bebf491

Browse files
committed
MB-20231: Add unit test for mempool perf measurement
Change-Id: I9b87bd55256918284cc15b0df543f7988de6efb6 Reviewed-on: http://review.couchbase.org/66323 Reviewed-by: Chiyoung Seo <[email protected]> Tested-by: buildbot <[email protected]>
1 parent 77ae2ca commit bebf491

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/unit/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ add_executable(hash_test
1010
target_link_libraries(hash_test ${PTHREAD_LIB} ${LIBM} ${MALLOC_LIBRARIES}
1111
${PLATFORM_LIBRARY} ${LIBRT})
1212

13+
add_executable(mempool_test
14+
mempool_test.cc
15+
${ROOT_SRC}/memory_pool.cc
16+
${PROJECT_SOURCE_DIR}/${BREAKPAD_SRC}
17+
${GETTIMEOFDAY_VS}
18+
${ROOT_UTILS}/time_utils.cc)
19+
20+
target_link_libraries(mempool_test ${PTHREAD_LIB} ${LIBM}
21+
${ASYNC_IO_LIB} ${MALLOC_LIBRARIES}
22+
${PLATFORM_LIBRARY} ${LIBRT} ${CRYPTO_LIB}
23+
${DL_LIBRARIES} ${BREAKPAD_LIBRARIES})
24+
set_target_properties(mempool_test PROPERTIES COMPILE_FLAGS "${CB_GNU_CXX11_OPTION}")
25+
26+
1327
add_executable(bcache_test
1428
bcache_test.cc
1529
${ROOT_SRC}/avltree.cc
@@ -183,6 +197,7 @@ target_link_libraries(btree_kv_test ${LIBM} ${MALLOC_LIBRARIES}
183197

184198
# add test target
185199
add_test(hash_test hash_test)
200+
add_test(mempool_test mempool_test)
186201
add_test(bcache_test bcache_test)
187202
add_test(filemgr_test filemgr_test)
188203
add_test(btreeblock_test btreeblock_test)

tests/unit/mempool_test.cc

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+
/*
3+
* Copyright 2016 Couchbase, Inc
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <string.h>
21+
22+
#include "test.h"
23+
#include "memory_pool.h"
24+
25+
struct worker_args{
26+
int num_runs;
27+
int num_bins;
28+
size_t bin_size;
29+
MemoryPool *mp;
30+
};
31+
32+
void *basic_tester(void *args_)
33+
{
34+
TEST_INIT();
35+
struct worker_args *args = (struct worker_args *)args_;
36+
size_t bin_size = args->bin_size;
37+
for (int i = args->num_runs; i; --i) {
38+
uint8_t *buf;
39+
const int idx = args->mp->fetchBlock(&buf);
40+
TEST_CHK(idx != -1);
41+
for (int j = 100; j; --j) {
42+
buf[rand() % (bin_size - 1)] = 'X';
43+
}
44+
args->mp->returnBlock(idx);
45+
}
46+
return NULL;
47+
}
48+
49+
void basic_test(int iterations, int num_bins, size_t bin_size)
50+
{
51+
TEST_INIT();
52+
struct worker_args mpool;
53+
struct timeval ts_begin, ts_cur, ts_gap;
54+
gettimeofday(&ts_begin, NULL);
55+
56+
mpool.num_runs = iterations;
57+
mpool.num_bins = num_bins;
58+
mpool.bin_size = bin_size;
59+
mpool.mp = new MemoryPool(mpool.num_bins, mpool.bin_size);
60+
void *ret = basic_tester(&mpool);
61+
TEST_CHK(!ret);
62+
delete mpool.mp;
63+
64+
gettimeofday(&ts_cur, NULL);
65+
ts_gap = _utime_gap(ts_begin, ts_cur);
66+
char res[128];
67+
sprintf(res, "basic test with %d runs of %d bins x %" _F64 "bytes"
68+
" in %ld us",
69+
iterations, num_bins, uint64_t(bin_size),
70+
ts_gap.tv_sec*1000000 + ts_gap.tv_usec);
71+
TEST_RESULT(res);
72+
}
73+
74+
void multi_thread_test(int num_threads, int iterations, int num_bins,
75+
size_t bin_size)
76+
{
77+
TEST_INIT();
78+
thread_t *tid = alca(thread_t, num_threads);
79+
struct timeval ts_begin, ts_cur, ts_gap;
80+
gettimeofday(&ts_begin, NULL);
81+
82+
struct worker_args mpool;
83+
mpool.num_runs = iterations;
84+
mpool.num_bins = num_bins;
85+
mpool.bin_size = bin_size;
86+
mpool.mp = new MemoryPool(mpool.num_bins, mpool.bin_size);
87+
88+
for (int i = num_threads - 1; i; --i) {
89+
thread_create(&tid[i], basic_tester, &mpool);
90+
}
91+
92+
for (int i = num_threads - 1; i; --i) {
93+
void *ret;
94+
thread_join(tid[i], &ret);
95+
TEST_CHK(!ret);
96+
}
97+
98+
delete mpool.mp;
99+
100+
gettimeofday(&ts_cur, NULL);
101+
ts_gap = _utime_gap(ts_begin, ts_cur);
102+
char res[128];
103+
sprintf(res, "multi-thread test with %d threads each with "
104+
"%d runs of %d bins x %" _F64 "bytes in %ld usec",
105+
num_threads, iterations, num_bins, uint64_t(bin_size),
106+
ts_gap.tv_sec*1000000 + ts_gap.tv_usec);
107+
TEST_RESULT(res);
108+
}
109+
110+
int main()
111+
{
112+
basic_test(10000, 8, 10485760); //1000 runs of 8 x 10MB buffers
113+
multi_thread_test(8, 10000, 8, 10485760); // repeat with 8 threads
114+
return 0;
115+
}

0 commit comments

Comments
 (0)