1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef OMIT_MULTI_VER
16 #include <gtest/gtest.h>
17
18 #include "distributeddb_data_generator.h"
19 #include "distributed_test_tools.h"
20 #include "distributed_crud_transaction_tools.h"
21 #include "kv_store_delegate.h"
22 #include "kv_store_delegate_manager.h"
23
24 using namespace std;
25 using namespace testing;
26 #if defined TESTCASES_USING_GTEST_EXT
27 using namespace testing::ext;
28 #endif
29 using namespace DistributedDB;
30 using namespace DistributedDBDataGenerator;
31
32 namespace DistributeddbKvTransactionPerf {
33 const bool IS_LOCAL = false;
34 const int PUT_TIMES = 10;
35 const int KEY_LENGTH = 16;
36 const int VALUE_LENGTH = 100;
37
38 class DistributeddbKvTransactionPerfTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 };
45
46 KvStoreDelegate *g_transactionDelegate = nullptr; // the delegate used in this suit.
47 KvStoreDelegateManager *g_transactionManager = nullptr;
SetUpTestCase(void)48 void DistributeddbKvTransactionPerfTest::SetUpTestCase(void)
49 {
50 }
51
TearDownTestCase(void)52 void DistributeddbKvTransactionPerfTest::TearDownTestCase(void)
53 {
54 }
55
SetUp(void)56 void DistributeddbKvTransactionPerfTest::SetUp(void)
57 {
58 MST_LOG("SetUpTestCase before all cases local[%d].", IS_LOCAL);
59 RemoveDir(DIRECTOR);
60
61 UnitTest *test = UnitTest::GetInstance();
62 ASSERT_NE(test, nullptr);
63 const TestInfo *testinfo = test->current_test_info();
64 ASSERT_NE(testinfo, nullptr);
65 string testCaseName = string(testinfo->name());
66 MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
67
68 g_transactionDelegate = DistributedTestTools::GetDelegateSuccess(g_transactionManager,
69 g_kvdbParameter1, g_kvOption);
70 ASSERT_TRUE(g_transactionManager != nullptr && g_transactionDelegate != nullptr);
71 }
72
TearDown(void)73 void DistributeddbKvTransactionPerfTest::TearDown(void)
74 {
75 EXPECT_EQ(g_transactionManager->CloseKvStore(g_transactionDelegate), OK);
76 g_transactionDelegate = nullptr;
77 DBStatus status = g_transactionManager->DeleteKvStore(STORE_ID_1);
78 EXPECT_EQ(status, DistributedDB::DBStatus::OK) << "fail to delete exist kvdb";
79 delete g_transactionManager;
80 g_transactionManager = nullptr;
81 RemoveDir(DIRECTOR);
82 }
83
84 /*
85 * @tc.name: Performance 001
86 * @tc.desc: Compare between inserting many records in one transaction and one record in each transaction.
87 * @tc.type: Performance
88 * @tc.require: SR000BUH3J
89 * @tc.author: luqianfu
90 */
91 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance001, TestSize.Level3)
92 {
93 /**
94 * @tc.steps: step1.get the putDuration of putBatch(keys,values) by transaction.
95 * @tc.expected: step1. the putDuration is smaller than 1ms/k.
96 */
97 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, false, IS_LOCAL);
98 double transactionInsert, singleInsert, batchInsert;
99
100 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
101 transactionInsert = performance.putDuration;
102 /**
103 * @tc.steps: step2.get the putDuration of put(k,v) singlely.
104 * @tc.expected: step2. the putDuration is smaller than 1ms/k and bigger than step1.
105 */
106 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
107 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
108 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
109 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
110 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
111 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
112 singleInsert = performance.putDuration;
113 /**
114 * @tc.steps: step3.get the putDuration of putBatch(keys,value) without transaction.
115 * @tc.expected: step3. the putDuration is smaller than 1ms/k.
116 */
117 performance.putBatch = true;
118 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
119 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
120 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
121 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
122 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
123 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
124 batchInsert = performance.putDuration;
125
126 if (transactionInsert <= batchInsert && batchInsert <= singleInsert) {
127 MST_LOG("the put performance is ok!");
128 }
129 }
130
131 /*
132 * @tc.name: Performance 002
133 * @tc.desc: Compare between updating many records in one transaction and one record in each transaction.
134 * @tc.type: Performance
135 * @tc.require: SR000BUH3J
136 * @tc.author: luqianfu
137 */
138 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance002, TestSize.Level3)
139 {
140 /**
141 * @tc.steps: step1.get the putDuration of updateBatch(keys,values) by transaction.
142 * @tc.expected: step1. the putDuration is smaller than 1ms/k.
143 */
144 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, false, IS_LOCAL);
145 double transactionUpdate, singleUpdate, batchUpdate;
146
147 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
148 transactionUpdate = performance.updateDuration;
149 /**
150 * @tc.steps: step2.get the putDuration of update(k,v) singlely.
151 * @tc.expected: step2. the putDuration is smaller than 1ms/k and is bigger than step1.
152 */
153 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
154 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
155 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
156 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
157 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
158 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
159 singleUpdate = performance.updateDuration;
160 /**
161 * @tc.steps: step3.get the putDuration of putBatch(keys,value) without transaction.
162 * @tc.expected: step3. the putDuration is smaller than 1ms/k.
163 */
164 performance.putBatch = true;
165 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
166 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
167 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
168 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
169 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
170 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
171 batchUpdate = performance.updateDuration;
172
173 if (transactionUpdate <= batchUpdate && batchUpdate <= singleUpdate) {
174 MST_LOG("the update performance is ok!");
175 }
176 }
177
178 /*
179 * @tc.name: Performance 003
180 * @tc.desc: Compare between deleting many records in one transaction and one record in each transaction.
181 * @tc.type: Performance
182 * @tc.require: SR000BUH3J
183 * @tc.author: luqianfu
184 */
185 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance003, TestSize.Level3)
186 {
187 /**
188 * @tc.steps: step1.get the putDuration of deleteBatch(keys,values) by transaction.
189 * @tc.expected: step1. the putDuration is smaller than 1ms/k.
190 */
191 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, false, IS_LOCAL);
192 double transactionDel, singleDel, batchDel;
193
194 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
195 transactionDel = performance.deleteDuration;
196 /**
197 * @tc.steps: step2.get the putDuration of delete(k,v) singlely.
198 * @tc.expected: step2. the putDuration is smaller than 1ms/k and is bigger than step1.
199 */
200 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
201 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
202 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
203 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
204 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
205 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
206 singleDel = performance.deleteDuration;
207 /**
208 * @tc.steps: step3.get the putDuration of deleteBatch(keys,value) without transaction.
209 * @tc.expected: step3. the putDuration is smaller than 1ms/k.
210 */
211 performance.putBatch = true;
212 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
213 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
214 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
215 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
216 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
217 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
218 batchDel = performance.deleteDuration;
219
220 if (transactionDel <= batchDel && batchDel <= singleDel) {
221 MST_LOG("the delete performance is ok!");
222 }
223 }
224
225 /*
226 * @tc.name: Performance 004
227 * @tc.desc: system info of inserting many records in one transaction and one record in each transaction.
228 * @tc.type: Performance
229 * @tc.require: SR000BUH3J
230 * @tc.author: luqianfu
231 */
232 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance004, TestSize.Level3)
233 {
234 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, true, IS_LOCAL);
235 /**
236 * @tc.steps: step1.get the sys info of putBatch(keys,values) by transaction.
237 * @tc.expected: step1. the sys info is normal.
238 */
239 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
240 /**
241 * @tc.steps: step2.get the sys info of put(k,v) singlely.
242 * @tc.expected: step2. the sys info is normal.
243 */
244 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
245 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
246 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
247 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
248 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
249 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
250 /**
251 * @tc.steps: step3.get the sys info of putBatch(keys,values) without transaction.
252 * @tc.expected: step3. the sys info is normal.
253 */
254 performance.putBatch = true;
255 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
256 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
257 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
258 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
259 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
260 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
261 }
262
263 /*
264 * @tc.name: Performance 005
265 * @tc.desc: system info of updating many records in one transaction and one record in each transaction.
266 * @tc.type: Pressure
267 * @tc.require: SR000BUH3J
268 * @tc.author: luqianfu
269 */
270 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance005, TestSize.Level3)
271 {
272 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, true, IS_LOCAL);
273
274 /**
275 * @tc.steps: step1.get the sys info of updateBatch(keys,values) by transaction.
276 * @tc.expected: step1. the sys info is normal.
277 */
278 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
279 /**
280 * @tc.steps: step2.get the sys info of update(k,v) singlely.
281 * @tc.expected: step2. the sys info is normal.
282 */
283 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
284 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
285 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
286 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
287 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
288 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
289 /**
290 * @tc.steps: step3.get the sys info of updateBatch(keys,values) without transaction.
291 * @tc.expected: step3. the sys info is normal.
292 */
293 performance.putBatch = true;
294 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
295 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
296 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
297 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
298 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
299 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
300 }
301
302 /*
303 * @tc.name: Performance 006
304 * @tc.desc: system info of deleting many records in one transaction and one record in each transaction.
305 * @tc.type: Pressure
306 * @tc.require: SR000BUH3J
307 * @tc.author: luqianfu
308 */
309 HWTEST_F(DistributeddbKvTransactionPerfTest, Performance006, TestSize.Level3)
310 {
311 PerformanceData performance(PUT_TIMES, KEY_LENGTH, VALUE_LENGTH, false, false, false, true, IS_LOCAL);
312
313 /**
314 * @tc.steps: step1.get the sys info of deleteBatch(keys,values) by transaction.
315 * @tc.expected: step1. the sys info is normal.
316 */
317 EXPECT_TRUE(DistributedTestTools::CalculateTransactionPerformance(performance));
318 /**
319 * @tc.steps: step2.get the sys info of delete(k,v) singlely.
320 * @tc.expected: step2. the sys info is normal.
321 */
322 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
323 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
324 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
325 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
326 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
327 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
328 /**
329 * @tc.steps: step3.get the sys info of deleteBatch(keys,values) without transaction.
330 * @tc.expected: step3. the sys info is normal.
331 */
332 performance.putBatch = true;
333 EXPECT_TRUE(DistributedTestTools::CalculateOpenPerformance(performance));
334 EXPECT_TRUE(DistributedTestTools::CalculateInsertPerformance(performance));
335 EXPECT_TRUE(DistributedTestTools::CalculateGetPutPerformance(performance));
336 EXPECT_TRUE(DistributedTestTools::CalculateUpdatePerformance(performance));
337 EXPECT_TRUE(DistributedTestTools::CalculateGetUpdatePerformance(performance));
338 EXPECT_TRUE(DistributedTestTools::CalculateUseClearPerformance(performance));
339 }
340 }
341 #endif // OMIT_MULTI_VER