bvar_sampler_unittest.cpp 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
gejun's avatar
gejun committed
17 18 19

#include <limits>                           //std::numeric_limits
#include "bvar/detail/sampler.h"
20 21
#include "butil/time.h"
#include "butil/logging.h"
gejun's avatar
gejun committed
22 23 24 25 26
#include <gtest/gtest.h>

namespace {

TEST(SamplerTest, linked_list) {
27
    butil::LinkNode<bvar::detail::Sampler> n1, n2;
gejun's avatar
gejun committed
28 29 30 31 32 33
    n1.InsertBeforeAsList(&n2);
    ASSERT_EQ(n1.next(), &n2);
    ASSERT_EQ(n1.previous(), &n2);
    ASSERT_EQ(n2.next(), &n1);
    ASSERT_EQ(n2.previous(), &n1);

34
    butil::LinkNode<bvar::detail::Sampler> n3, n4;
gejun's avatar
gejun committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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) {
69
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
70 71
    logging::StringSink log_str;
    logging::LogSink* old_sink = logging::SetLogSink(&log_str);
72
#endif
gejun's avatar
gejun committed
73 74 75 76 77 78 79 80
    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) {
81 82
        // LE: called once every second, may be called more than once
        ASSERT_LE(1, s[i]->called_count()) << "i=" << i;
gejun's avatar
gejun committed
83 84 85 86 87 88 89
    }
    EXPECT_EQ(0, DebugSampler::_s_ndestroy);
    for (int i = 0; i < N; ++i) {
        s[i]->destroy();
    }
    usleep(1010000);
    EXPECT_EQ(N, DebugSampler::_s_ndestroy);
90
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
91 92 93 94 95
    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"));
    }
96
#endif
gejun's avatar
gejun committed
97 98
}

99
static void* check(void*) {
gejun's avatar
gejun committed
100 101 102 103 104 105 106 107
    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) {
108
        EXPECT_LE(1, s[i]->called_count()) << "i=" << i;
gejun's avatar
gejun committed
109 110 111 112 113 114 115 116
    }
    for (int i = 0; i < N; ++i) {
        s[i]->destroy();
    }
    return NULL;
}

TEST(SamplerTest, multi_threaded) {
117
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
118 119
    logging::StringSink log_str;
    logging::LogSink* old_sink = logging::SetLogSink(&log_str);
120
#endif
gejun's avatar
gejun committed
121 122 123 124 125 126 127 128 129 130
    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);
131
#if !BRPC_WITH_GLOG
gejun's avatar
gejun committed
132 133 134 135 136
    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"));
    }
137
#endif
gejun's avatar
gejun committed
138 139
}
} // namespace