2
* Copyright (c) 2021 OceanBase
3
* OceanBase CE is licensed under Mulan PubL v2.
4
* You can use this software according to the terms and conditions of the Mulan PubL v2.
5
* You may obtain a copy of Mulan PubL v2 at:
6
* http://license.coscl.org.cn/MulanPubL-2.0
7
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
* See the Mulan PubL v2 for more details.
13
#include <gtest/gtest.h>
14
#include "ob_concurrent_seq_queue.h"
16
#include "share/ob_define.h"
17
#include "lib/time/ob_time_utility.h"
18
#include "lib/atomic/ob_atomic.h"
24
class TestConSeqQueue : public ::testing::Test
27
static const int64_t RUN_TIME = 1L * 60L * 60L * 1000L * 1000L;
28
static const int64_t THREAD_NUM = 20;
29
static const int64_t STAT_INTERVAL = 5 * 1000 * 1000;
36
ASSERT_EQ(0, queue_.init(1024, ObMemAttr(OB_SERVER_TENANT_ID, ObNewModIds::TEST)));
39
consume_thread_counter_ = 0;
40
consume_task_count_ = 0;
42
last_consume_task_count_ = 0;
45
virtual void TearDown()
49
static void *produce_thread_func(void *args);
50
static void *consume_thread_func(void *args);
55
pthread_t produce_threads_[THREAD_NUM];
56
pthread_t consume_threads_[THREAD_NUM];
57
int64_t consume_thread_counter_;
58
ObConcurrentSeqQueue queue_;
59
int64_t produce_seq_ CACHE_ALIGNED;
60
int64_t consume_seq_ CACHE_ALIGNED;
61
int64_t consume_task_count_ CACHE_ALIGNED;
62
int64_t last_consume_task_count_ CACHE_ALIGNED;
63
int64_t last_stat_time_ CACHE_ALIGNED;
65
volatile bool stop_flag_ CACHE_ALIGNED;
68
TEST_F(TestConSeqQueue, basic)
70
ObConcurrentSeqQueue queue;
73
EXPECT_EQ(0, queue.init(1024, ObMemAttr(OB_SERVER_TENANT_ID, ObNewModIds::TEST)));
75
EXPECT_EQ(0, queue.push((void*)0, 0, 0));
76
EXPECT_EQ(0, queue.push((void*)1, 1, 0));
77
EXPECT_EQ(0, queue.push((void*)2, 2, 0));
79
EXPECT_EQ(0, queue.pop(data, 0, 0));
80
EXPECT_EQ(0, (int64_t)data);
81
EXPECT_EQ(0, queue.pop(data, 1, 0));
82
EXPECT_EQ(1, (int64_t)data);
83
EXPECT_EQ(0, queue.pop(data, 2, 0));
84
EXPECT_EQ(2, (int64_t)data);
86
// Failed to push and pop elements with the same serial number again
87
EXPECT_NE(0, queue.push((void*)0, 0, 0));
88
EXPECT_NE(0, queue.push((void*)1, 1, 0));
89
EXPECT_NE(0, queue.push((void*)2, 2, 0));
90
EXPECT_NE(0, queue.pop(data, 0, 0));
91
EXPECT_NE(0, queue.pop(data, 1, 0));
92
EXPECT_NE(0, queue.pop(data, 2, 0));
95
void *TestConSeqQueue::produce_thread_func(void *args)
98
((TestConSeqQueue *)args)->run_produce();
104
void TestConSeqQueue::run_produce()
106
int ret = OB_SUCCESS;
107
int64_t batch_count = 1000;
109
while (OB_SUCCESS == ret && ! stop_flag_) {
110
for (int64_t index = 0; OB_SUCCESS == ret && index < batch_count; index++) {
111
int64_t seq = ATOMIC_FAA(&produce_seq_, 1);
112
while (! stop_flag_ && OB_TIMEOUT == (ret = queue_.push((void*)seq, seq, 1 * 1000 * 1000)));
114
EXPECT_EQ(OB_SUCCESS, ret);
120
void *TestConSeqQueue::consume_thread_func(void *args)
123
((TestConSeqQueue *)args)->run_consume();
129
void TestConSeqQueue::run_consume()
131
int ret = OB_SUCCESS;
132
int64_t batch_count = 1000;
133
int64_t end_time = ObTimeUtility::current_time();
135
int64_t thread_index = ATOMIC_FAA(&consume_thread_counter_, 0);
137
while (OB_SUCCESS == ret && !stop_flag_) {
138
for (int64_t index = 0; OB_SUCCESS == ret && index < batch_count; index++) {
139
int64_t seq = ATOMIC_FAA(&consume_seq_, 1);
141
while (! stop_flag_ && OB_TIMEOUT == (ret = queue_.pop(data, seq, 1 * 1000 * 1000)));
143
EXPECT_EQ(OB_SUCCESS, ret);
144
EXPECT_EQ(seq, (int64_t)data);
145
ATOMIC_INC(&consume_task_count_);
149
int64_t cur_time = ObTimeUtility::current_time();
150
if (OB_UNLIKELY(0 == thread_index) && cur_time - last_stat_time_ > STAT_INTERVAL) {
151
int64_t task_count = ATOMIC_LOAD(&consume_task_count_);
152
int64_t consume_seq = ATOMIC_LOAD(&consume_seq_);
153
int64_t produce_seq = ATOMIC_LOAD(&produce_seq_);
154
if (0 != last_stat_time_) {
155
int64_t delta_task_count = task_count - last_consume_task_count_;
156
int64_t delta_time_sec = (cur_time - last_stat_time_)/1000000;
157
LIB_LOG(INFO, "STAT", "POP_TPS", delta_task_count/delta_time_sec, K(delta_task_count),
158
K(consume_seq), K(produce_seq), K(INT32_MAX));
161
last_stat_time_ = cur_time;
162
last_consume_task_count_ = task_count;
165
if (end_time - cur_time <= 0) {
171
TEST_F(TestConSeqQueue, thread)
173
for (int64_t index = 0; index < THREAD_NUM; index++) {
174
ASSERT_EQ(0, pthread_create(produce_threads_ + index, NULL, produce_thread_func, this));
176
for (int64_t index = 0; index < THREAD_NUM; index++) {
177
ASSERT_EQ(0, pthread_create(consume_threads_ + index, NULL, consume_thread_func, this));
179
for (int64_t index = 0; index < THREAD_NUM; index++) {
180
pthread_join(produce_threads_[index], NULL);
181
produce_threads_[index] = 0;
183
for (int64_t index = 0; index < THREAD_NUM; index++) {
184
pthread_join(consume_threads_[index], NULL);
185
consume_threads_[index] = 0;
192
int main(int argc, char **argv)
194
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
195
OB_LOGGER.set_log_level("INFO");
196
testing::InitGoogleTest(&argc, argv);
197
return RUN_ALL_TESTS();