resource_pool_unittest.cpp 10 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Jul 13 15:04:18 CST 2014

#include <gtest/gtest.h>
6 7 8
#include "butil/time.h"
#include "butil/macros.h"
#include "butil/fast_rand.h"
gejun's avatar
gejun committed
9 10

#define BAIDU_CLEAR_RESOURCE_POOL_AFTER_ALL_THREADS_QUIT
11
#include "butil/resource_pool.h"
gejun's avatar
gejun committed
12 13 14 15 16 17 18

namespace {
struct MyObject {};

int nfoo_dtor = 0;
struct Foo {
    Foo() {
19
        x = butil::fast_rand() % 2;
gejun's avatar
gejun committed
20 21 22 23 24 25 26 27
    }
    ~Foo() {
        ++nfoo_dtor;
    }
    int x;
};
}

28
namespace butil {
gejun's avatar
gejun committed
29 30 31 32 33 34 35 36 37
template <> struct ResourcePoolBlockMaxSize<MyObject> {
    static const size_t value = 128;
};

template <> struct ResourcePoolBlockMaxItem<MyObject> {
    static const size_t value = 3;
};

template <> struct ResourcePoolFreeChunkMaxItem<MyObject> {
gejun's avatar
gejun committed
38
    static size_t value() { return 5; }
gejun's avatar
gejun committed
39 40 41 42 43 44 45 46 47 48 49
};

template <> struct ResourcePoolValidator<Foo> {
    static bool validate(const Foo* foo) {
        return foo->x != 0;
    }
};

}

namespace {
50
using namespace butil;
gejun's avatar
gejun committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

class ResourcePoolTest : public ::testing::Test{
protected:
    ResourcePoolTest(){
    };
    virtual ~ResourcePoolTest(){};
    virtual void SetUp() {
        srand(time(0));
    };
    virtual void TearDown() {
    };
};

TEST_F(ResourcePoolTest, atomic_array_init) {
    for (int i = 0; i < 2; ++i) {
        if (i == 0) {
67
            butil::atomic<int> a[2];
gejun's avatar
gejun committed
68 69 70 71 72
            a[0] = 1;
            // The folowing will cause compile error with gcc3.4.5 and the
            // reason is unknown
            // a[1] = 2;
        } else if (i == 2) {
73
            butil::atomic<int> a[2];
gejun's avatar
gejun committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            ASSERT_EQ(0, a[0]);
            ASSERT_EQ(0, a[1]);
        }
    }
}

int nc = 0;
int nd = 0;
std::set<void*> ptr_set;
struct YellObj {
    YellObj() {
        ++nc;
        ptr_set.insert(this);
        printf("Created %p\n", this);
    }
    ~YellObj() {
        ++nd;
        ptr_set.erase(this);
        printf("Destroyed %p\n", this);
    }
    char _dummy[96];
};

TEST_F(ResourcePoolTest, change_config) {
    int a[2];
    printf("%lu\n", ARRAY_SIZE(a));
    
    ResourcePoolInfo info = describe_resources<MyObject>();
gejun's avatar
gejun committed
102
    ResourcePoolInfo zero_info = { 0, 0, 0, 0, 3, 3, 0 };
gejun's avatar
gejun committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));

    ResourceId<MyObject> id = { 0 };
    get_resource<MyObject>(&id);
    std::cout << describe_resources<MyObject>() << std::endl;
    return_resource(id);
    std::cout << describe_resources<MyObject>() << std::endl;
}

struct NonDefaultCtorObject {
    explicit NonDefaultCtorObject(int value) : _value(value) {}
    NonDefaultCtorObject(int value, int dummy) : _value(value + dummy) {}

    int _value;
};

TEST_F(ResourcePoolTest, sanity) {
    ptr_set.clear();
    ResourceId<NonDefaultCtorObject> id0 = { 0 };
    get_resource<NonDefaultCtorObject>(&id0, 10);
    ASSERT_EQ(10, address_resource(id0)->_value);
    get_resource<NonDefaultCtorObject>(&id0, 100, 30);
    ASSERT_EQ(130, address_resource(id0)->_value);
    
    printf("BLOCK_NITEM=%lu\n", ResourcePool<YellObj>::BLOCK_NITEM);
    
    nc = 0;
    nd = 0;
     {
        ResourceId<YellObj> id1;
        YellObj* o1 = get_resource(&id1);
        ASSERT_TRUE(o1);
        ASSERT_EQ(o1, address_resource(id1));
        
        ASSERT_EQ(1, nc);
        ASSERT_EQ(0, nd);

        ResourceId<YellObj> id2;
        YellObj* o2 = get_resource(&id2);
        ASSERT_TRUE(o2);
        ASSERT_EQ(o2, address_resource(id2));

        ASSERT_EQ(2, nc);
        ASSERT_EQ(0, nd);
        
        return_resource(id1);
        ASSERT_EQ(2, nc);
        ASSERT_EQ(0, nd);

        return_resource(id2);
        ASSERT_EQ(2, nc);
        ASSERT_EQ(0, nd);
    }
    ASSERT_EQ(0, nd);

    clear_resources<YellObj>();
    ASSERT_EQ(2, nd);
    ASSERT_TRUE(ptr_set.empty()) << ptr_set.size();
}

TEST_F(ResourcePoolTest, validator) {
    nfoo_dtor = 0;
    int nfoo = 0;
    for (int i = 0; i < 100; ++i) {
        ResourceId<Foo> id = { 0 };
        Foo* foo = get_resource<Foo>(&id);
        if (foo) {
            ASSERT_EQ(1, foo->x);
            ++nfoo;
        }
    }
    ASSERT_EQ(nfoo + nfoo_dtor, 100);
    ASSERT_EQ((size_t)nfoo, describe_resources<Foo>().item_num);
}

TEST_F(ResourcePoolTest, get_int) {
    clear_resources<int>();
    
    // Perf of this test is affected by previous case.
    const size_t N = 100000;
    
184
    butil::Timer tm;
gejun's avatar
gejun committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    ResourceId<int> id;

    // warm up
    if (get_resource(&id)) {
        return_resource(id);
    }
    ASSERT_EQ(0UL, id);
    delete (new int);
    
    tm.start();
    for (size_t i = 0; i < N; ++i) {
        *get_resource(&id) = i;
    }
    tm.stop();
    printf("get a int takes %.1fns\n", tm.n_elapsed()/(double)N);
    
    tm.start();
    for (size_t i = 0; i < N; ++i) {
        *(new int) = i;
    }    
    tm.stop();
    printf("new a int takes %luns\n", tm.n_elapsed()/N);

    tm.start();
    for (size_t i = 0; i < N; ++i) {
        id.value = i;
        *ResourcePool<int>::unsafe_address_resource(id) = i;
    }
    tm.stop();
    printf("unsafe_address a int takes %.1fns\n", tm.n_elapsed()/(double)N);
    
    tm.start();
    for (size_t i = 0; i < N; ++i) {
        id.value = i;
        *address_resource(id) = i;
    }
    tm.stop();
    printf("address a int takes %.1fns\n", tm.n_elapsed()/(double)N);

    std::cout << describe_resources<int>() << std::endl;
    clear_resources<int>();
    std::cout << describe_resources<int>() << std::endl;
}


struct SilentObj {
    char buf[sizeof(YellObj)];
};

TEST_F(ResourcePoolTest, get_perf) {
    const size_t N = 10000;
    std::vector<SilentObj*> new_list;
    new_list.reserve(N);
    ResourceId<SilentObj> id;
    
240
    butil::Timer tm1, tm2;
gejun's avatar
gejun committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    // warm up
    if (get_resource(&id)) {
        return_resource(id);
    }
    delete (new SilentObj);

    // Run twice, the second time will be must faster.
    for (size_t j = 0; j < 2; ++j) {

        tm1.start();
        for (size_t i = 0; i < N; ++i) {
            get_resource(&id);
        }
        tm1.stop();
        printf("get a SilentObj takes %luns\n", tm1.n_elapsed()/N);
        //clear_resources<SilentObj>(); // free all blocks
        
        tm2.start();
        for (size_t i = 0; i < N; ++i) {
            new_list.push_back(new SilentObj);
        }    
        tm2.stop();
        printf("new a SilentObj takes %luns\n", tm2.n_elapsed()/N);
        for (size_t i = 0; i < new_list.size(); ++i) {
            delete new_list[i];
        }
        new_list.clear();
    }

    std::cout << describe_resources<SilentObj>() << std::endl;
}

struct D { int val[1]; };

void* get_and_return_int(void*) {
    // Perf of this test is affected by previous case.
    const size_t N = 100000;
    std::vector<ResourceId<D> > v;
    v.reserve(N);
281
    butil::Timer tm0, tm1, tm2;
gejun's avatar
gejun committed
282
    ResourceId<D> id = {0};
gejun's avatar
gejun committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    D tmp = D();
    int sr = 0;

    // warm up
    tm0.start();
    if (get_resource(&id)) {
        return_resource(id);
    }
    tm0.stop();

    printf("[%lu] warmup=%lu\n", pthread_self(), tm0.n_elapsed());

    for (int j = 0; j < 5; ++j) {
        v.clear();
        sr = 0;
        
        tm1.start();
        for (size_t i = 0; i < N; ++i) {
            *get_resource(&id) = tmp;
            v.push_back(id);
        }
        tm1.stop();

        std::random_shuffle(v.begin(), v.end());

        tm2.start();
        for (size_t i = 0; i < v.size(); ++i) {
            sr += return_resource(v[i]);
        }
        tm2.stop();

        if (0 != sr) {
            printf("%d return_resource failed\n", sr);
        }
        
        printf("[%lu:%d] get<D>=%.1f return<D>=%.1f\n",
               pthread_self(), j, tm1.n_elapsed()/(double)N, tm2.n_elapsed()/(double)N);
    }
    return NULL;
}

void* new_and_delete_int(void*) {
    const size_t N = 100000;
    std::vector<D*> v2;
    v2.reserve(N);
328
    butil::Timer tm0, tm1, tm2;
gejun's avatar
gejun committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    D tmp = D();

    for (int j = 0; j < 3; ++j) {
        v2.clear();
        
        // warm up
        delete (new D);

        tm1.start();
        for (size_t i = 0; i < N; ++i) {
            D *p = new D;
            *p = tmp;
            v2.push_back(p);
        }
        tm1.stop();

        std::random_shuffle(v2.begin(), v2.end());

        tm2.start();
        for (size_t i = 0; i < v2.size(); ++i) {
            delete v2[i];
        }
        tm2.stop();
        
        printf("[%lu:%d] new<D>=%.1f delete<D>=%.1f\n",
               pthread_self(), j, tm1.n_elapsed()/(double)N, tm2.n_elapsed()/(double)N);
    }
    
    return NULL;
}

TEST_F(ResourcePoolTest, get_and_return_int_single_thread) {
    get_and_return_int(NULL);
    new_and_delete_int(NULL);
}

TEST_F(ResourcePoolTest, get_and_return_int_multiple_threads) {
    pthread_t tid[16];
    for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
        ASSERT_EQ(0, pthread_create(&tid[i], NULL, get_and_return_int, NULL));
    }
    for (size_t i = 0; i < ARRAY_SIZE(tid); ++i) {
        pthread_join(tid[i], NULL);
    }

    pthread_t tid2[16];
    for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
        ASSERT_EQ(0, pthread_create(&tid2[i], NULL, new_and_delete_int, NULL));
    }
    for (size_t i = 0; i < ARRAY_SIZE(tid2); ++i) {
        pthread_join(tid2[i], NULL);
    }

    std::cout << describe_resources<D>() << std::endl;
    clear_resources<D>();
    ResourcePoolInfo info = describe_resources<D>();
    ResourcePoolInfo zero_info = { 0, 0, 0, 0,
                                   ResourcePoolBlockMaxItem<D>::value,
                                   ResourcePoolBlockMaxItem<D>::value, 0};
    ASSERT_EQ(0, memcmp(&info, &zero_info, sizeof(info)));
}

TEST_F(ResourcePoolTest, verify_get) {
    clear_resources<int>();
    std::cout << describe_resources<int>() << std::endl;
                              
    std::vector<std::pair<int*, ResourceId<int> > > v;
    v.reserve(100000);
    ResourceId<int> id = { 0 };
    for (int i = 0; (size_t)i < v.capacity(); ++i)  {
        int* p = get_resource(&id);
        *p = i;
        v.push_back(std::make_pair(p, id));
    }
    int i;
    for (i = 0; (size_t)i < v.size() && *v[i].first == i; ++i);
    ASSERT_EQ(v.size(), (size_t)i);
    for (i = 0; (size_t)i < v.size() && v[i].second == (size_t)i; ++i);
    ASSERT_EQ(v.size(), (size_t)i) << "i=" << i << ", " << v[i].second;

    clear_resources<int>();
}
} // namespace