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 #include <chrono>
16 #include <gtest/gtest.h>
17 #include <random>
18 #include <string>
19 #include <thread>
20
21 #include "distributed_test_tools.h"
22 #include "distributeddb_data_generator.h"
23 #include "distributeddb_nb_test_tools.h"
24 #include "distributeddb_schema_test_tools.h"
25 #include "kv_store_delegate.h"
26 #include "kv_store_delegate_manager.h"
27 #include "kv_store_nb_delegate.h"
28
29 using namespace std;
30 using namespace chrono;
31 using namespace testing;
32 #if defined TESTCASES_USING_GTEST_EXT
33 using namespace testing::ext;
34 #endif
35 using namespace DistributedDB;
36 using namespace DistributedDBDataGenerator;
37
38 namespace DistributeddbNbCrudPower {
39 KvStoreNbDelegate *g_nbDelegate = nullptr;
40 KvStoreDelegateManager *g_manager = nullptr;
41
42 class DistributeddbNbCrudPowerTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48 private:
49 };
50
SetUpTestCase(void)51 void DistributeddbNbCrudPowerTest::SetUpTestCase(void)
52 {
53 }
54
TearDownTestCase(void)55 void DistributeddbNbCrudPowerTest::TearDownTestCase(void)
56 {
57 }
58
SetUp(void)59 void DistributeddbNbCrudPowerTest::SetUp(void)
60 {
61 RemoveDir(DistributedDBConstant::NB_DIRECTOR);
62
63 UnitTest *test = UnitTest::GetInstance();
64 ASSERT_NE(test, nullptr);
65 const TestInfo *testinfo = test->current_test_info();
66 ASSERT_NE(testinfo, nullptr);
67 string testCaseName = string(testinfo->name());
68 MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
69
70 Option option;
71 option.isMemoryDb = false;
72 option.schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, PERF_SCHEMA_DEFINE,
73 PERF_SCHEMA_SIX_INDEXES, SKIP_SIZE);
74 g_nbDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(g_manager, g_dbParameter1, option);
75 ASSERT_TRUE(g_manager != nullptr && g_nbDelegate != nullptr);
76 }
77
TearDown(void)78 void DistributeddbNbCrudPowerTest::TearDown(void)
79 {
80 MST_LOG("TearDownTestCase after case.");
81 ASSERT_NE(g_manager, nullptr);
82 EXPECT_TRUE(EndCaseDeleteDB(g_manager, g_nbDelegate, STORE_ID_1, false));
83 RemoveDir(DistributedDBConstant::NB_DIRECTOR);
84 }
85
86 #ifndef LOW_LEVEL_MEM_DEV
RepeatExeCrud(const vector<Entry> & putEntry,const vector<Entry> & updateEntry)87 void RepeatExeCrud(const vector<Entry> &putEntry, const vector<Entry> &updateEntry)
88 {
89 ASSERT_TRUE(g_nbDelegate != nullptr);
90 EXPECT_EQ(DistributedDBNbTestTools::PutBatch(*g_nbDelegate, putEntry), OK);
91 vector<Entry> entries;
92 for (int cnt = 0; cnt < 5; cnt++) { // the loop times are 5.
93 EXPECT_EQ(g_nbDelegate->GetEntries({'p'}, entries), OK);
94 entries.clear();
95 }
96 EXPECT_EQ(DistributedDBNbTestTools::PutBatch(*g_nbDelegate, updateEntry), OK);
97 for (int cnt = 0; cnt < 5; cnt++) { // the loop times are 5.
98 EXPECT_EQ(g_nbDelegate->GetEntries({'u'}, entries), OK);
99 entries.clear();
100 }
101 for (int cnt = 0; cnt < 2; cnt++) { // the loop times are 2.
102 EXPECT_EQ(g_nbDelegate->GetEntries(KEY_EMPTY, entries), OK);
103 entries.clear();
104 }
105 for (int cnt = 0; cnt < 200; cnt++) { // the delete records are 200.
106 EXPECT_EQ(g_nbDelegate->Delete(updateEntry[cnt].key), OK);
107 }
108 }
109
110 /*
111 * @tc.name: PowerConsume 001
112 * @tc.desc: Test power consume of CRUD.
113 * @tc.type: Power
114 * @tc.require: AR000DR9K2
115 * @tc.author: fengxiaoyun
116 */
117 #ifdef NB_CRUD_POWER
118 HWTEST_F(DistributeddbNbCrudPowerTest, PowerConsume001, TestSize.Level4)
119 {
120 vector<Entry> entriesBatch;
121 vector<Key> allKeys;
122 EntrySize entrySize = {VALUE_ONE_HUNDRED_BYTE, TWO_POINT_FOUR_LONG};
123 /**
124 * @tc.steps: step1-2. preset 10000 items of (keys,values) then CRUD frequently.
125 * @tc.expected: step1-2. operate successfully.
126 */
127 entriesBatch = DistributedDBSchemaTestTools::GenerateFixedJsonSchemaRecords(TEN_THOUSAND_RECORDS,
128 entrySize, 'k', '0', allKeys);
129 EXPECT_EQ(DistributedDBNbTestTools::PutBatch(*g_nbDelegate, entriesBatch), OK);
130 for (int cnt = 0; cnt < 2; cnt++) { // the loop times are 2.
131 for (const auto &iter : allKeys) {
132 Value valueGot;
133 EXPECT_EQ(g_nbDelegate->Get(iter, valueGot), OK);
134 }
135 }
136 for (int cnt = 0; cnt < 10; cnt++) { // the loop times are 10.
137 int queryValue = GetRandInt(1, 9000); // get rand value between 1 and 9000.
138 Query query = Query::Select().GreaterThanOrEqualTo("$.field1", queryValue).And().LessThanOrEqualTo("$.field1",
139 queryValue + 1000); // query the 1000 records.
140 vector<DistributedDB::Entry> entriesGot;
141 EXPECT_EQ(g_nbDelegate->GetEntries(query, entriesGot), OK);
142 entriesGot.clear();
143 }
144 vector<Entry> entriesPut, entriesUpdate;
145 vector<Key> allKeys1, allKeys2;
146 entriesPut = DistributedDBSchemaTestTools::GenerateFixedJsonSchemaRecords(TEN_THOUSAND_RECORDS,
147 entrySize, 'p', 't', allKeys1);
148 entriesUpdate = DistributedDBSchemaTestTools::GenerateFixedJsonSchemaRecords(TEN_THOUSAND_RECORDS,
149 entrySize, 'u', 'e', allKeys2);
150 for (int cnt = 0; cnt < 10; cnt++) { // the loop times are 10.
151 RepeatExeCrud(entriesPut, entriesUpdate);
152 std::this_thread::sleep_for(std::chrono::seconds(TWENTY_SECONDS));
153 }
154 }
155 #endif
156 #endif
157 }
158