bvar_sampler_unittest.cpp 3.37 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4

#include <limits>                           //std::numeric_limits
#include "bvar/detail/sampler.h"
5 6
#include "butil/time.h"
#include "butil/logging.h"
gejun's avatar
gejun committed
7 8 9 10 11
#include <gtest/gtest.h>

namespace {

TEST(SamplerTest, linked_list) {
12
    butil::LinkNode<bvar::detail::Sampler> n1, n2;
gejun's avatar
gejun committed
13 14 15 16 17 18
    n1.InsertBeforeAsList(&n2);
    ASSERT_EQ(n1.next(), &n2);
    ASSERT_EQ(n1.previous(), &n2);
    ASSERT_EQ(n2.next(), &n1);
    ASSERT_EQ(n2.previous(), &n1);

19
    butil::LinkNode<bvar::detail::Sampler> n3, n4;
gejun's avatar
gejun committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    n3.InsertBeforeAsList(&n4);
    ASSERT_EQ(n3.next(), &n4);
    ASSERT_EQ(n3.previous(), &n4);
    ASSERT_EQ(n4.next(), &n3);
    ASSERT_EQ(n4.previous(), &n3);

    n1.InsertBeforeAsList(&n3);
    ASSERT_EQ(n1.next(), &n2);
    ASSERT_EQ(n2.next(), &n3);
    ASSERT_EQ(n3.next(), &n4);
    ASSERT_EQ(n4.next(), &n1);
    ASSERT_EQ(&n1, n2.previous());
    ASSERT_EQ(&n2, n3.previous());
    ASSERT_EQ(&n3, n4.previous());
    ASSERT_EQ(&n4, n1.previous());
}

class DebugSampler : public bvar::detail::Sampler {
public:
    DebugSampler() : _ncalled(0) {}
    ~DebugSampler() {
        ++_s_ndestroy;
    }
    void take_sample() {
        ++_ncalled;
    }
    int called_count() const { return _ncalled; }
private:
    int _ncalled;
    static int _s_ndestroy;
};
int DebugSampler::_s_ndestroy = 0;

TEST(SamplerTest, single_threaded) {
54
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
55 56
    logging::StringSink log_str;
    logging::LogSink* old_sink = logging::SetLogSink(&log_str);
57
#endif
gejun's avatar
gejun committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    const int N = 100;
    DebugSampler* s[N];
    for (int i = 0; i < N; ++i) {
        s[i] = new DebugSampler;
        s[i]->schedule();
    }
    usleep(1010000);
    for (int i = 0; i < N; ++i) {
        ASSERT_EQ(1, s[i]->called_count()) << "i=" << i;
    }
    EXPECT_EQ(0, DebugSampler::_s_ndestroy);
    for (int i = 0; i < N; ++i) {
        s[i]->destroy();
    }
    usleep(1010000);
    EXPECT_EQ(N, DebugSampler::_s_ndestroy);
74
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
75 76 77 78 79
    ASSERT_EQ(&log_str, logging::SetLogSink(old_sink));
    if (log_str.find("Removed ") != std::string::npos) {
        ASSERT_NE(std::string::npos, log_str.find("Removed 0, sampled 100"));
        ASSERT_NE(std::string::npos, log_str.find("Removed 100, sampled 0"));
    }
80
#endif
gejun's avatar
gejun committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

void* check(void*) {
    const int N = 100;
    DebugSampler* s[N];
    for (int i = 0; i < N; ++i) {
        s[i] = new DebugSampler;
        s[i]->schedule();
    }
    usleep(1010000);
    for (int i = 0; i < N; ++i) {
        EXPECT_EQ(1, s[i]->called_count()) << "i=" << i;
    }
    for (int i = 0; i < N; ++i) {
        s[i]->destroy();
    }
    return NULL;
}

TEST(SamplerTest, multi_threaded) {
101
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
102 103
    logging::StringSink log_str;
    logging::LogSink* old_sink = logging::SetLogSink(&log_str);
104
#endif
gejun's avatar
gejun committed
105 106 107 108 109 110 111 112 113 114
    pthread_t th[10];
    DebugSampler::_s_ndestroy = 0;
    for (size_t i = 0; i < arraysize(th); ++i) {
        ASSERT_EQ(0, pthread_create(&th[i], NULL, check, NULL));
    }
    for (size_t i = 0; i < arraysize(th); ++i) {
        ASSERT_EQ(0, pthread_join(th[i], NULL));
    }
    sleep(1);
    EXPECT_EQ(100 * arraysize(th), (size_t)DebugSampler::_s_ndestroy);
115
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
116 117 118 119 120
    ASSERT_EQ(&log_str, logging::SetLogSink(old_sink));
    if (log_str.find("Removed ") != std::string::npos) {
        ASSERT_NE(std::string::npos, log_str.find("Removed 0, sampled 1000"));
        ASSERT_NE(std::string::npos, log_str.find("Removed 1000, sampled 0"));
    }
121
#endif
gejun's avatar
gejun committed
122 123
}
} // namespace