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 
16 #include <gtest/gtest.h>
17 #include <thread>
18 #include "distributeddb_tools_unit_test.h"
19 #include "kv_store_delegate_manager.h"
20 #include "kv_store_nb_delegate.h"
21 #include "log_print.h"
22 #include "store_types.h"
23 
24 using namespace testing::ext;
25 using namespace DistributedDB;
26 using namespace DistributedDBUnitTest;
27 using namespace std;
28 
29 namespace {
30     // define some variables to init a KvStoreDelegateManager object.
31     KvStoreDelegateManager g_mgr("app0", "user0");
32     string g_testDir;
33     KvStoreConfig g_config;
34     Key g_keyPrefix = {'A', 'B', 'C'};
35 
36     const int BASE_NUMBER = 100000;
37     const int INSERT_NUMBER = 100;
38     const int ENTRY_VALUE_SIZE = 3000;
39     const int BATCH_ENTRY_NUMBER = 100;
40 
41     DBStatus g_kvDelegateStatus = INVALID_ARGS;
42     KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
43 
KvStoreNbDelegateCallback(DBStatus statusSrc,KvStoreNbDelegate * kvStoreSrc,DBStatus * statusDst,KvStoreNbDelegate ** kvStoreDst)44     void KvStoreNbDelegateCallback(DBStatus statusSrc, KvStoreNbDelegate* kvStoreSrc,
45         DBStatus* statusDst, KvStoreNbDelegate** kvStoreDst)
46     {
47         *statusDst = statusSrc;
48         *kvStoreDst = kvStoreSrc;
49     }
50 
51     // the type of g_kvNbDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
52     auto g_kvNbDelegateCallback = bind(&KvStoreNbDelegateCallback, placeholders::_1,
53         placeholders::_2, &g_kvDelegateStatus, &g_kvNbDelegatePtr);
54 
InitResultSet()55     void InitResultSet()
56     {
57         Key testKey;
58         Value testValue;
59         for (int i = BASE_NUMBER; i < BASE_NUMBER + INSERT_NUMBER; i++) {
60             testKey.clear();
61             testValue.clear();
62             testKey = g_keyPrefix;
63             std::string strIndex = std::to_string(i);
64             testKey.insert(testKey.end(), strIndex.begin(), strIndex.end());
65 
66             DistributedDBToolsUnitTest::GetRandomKeyValue(testValue, ENTRY_VALUE_SIZE);
67             if ((i % BATCH_ENTRY_NUMBER) == 0) {
68                 g_kvNbDelegatePtr->StartTransaction();
69             }
70             EXPECT_EQ(g_kvNbDelegatePtr->Put(testKey, testValue), OK);
71             if (((i + 1) % BATCH_ENTRY_NUMBER) == 0) {
72                 g_kvNbDelegatePtr->Commit();
73             }
74         }
75 
76         std::this_thread::sleep_for(std::chrono::seconds(2)); // sleep 2 s for the cache.
77     }
78 }
79 class DistributedDBInterfacesNBResultsetPerfTest : public testing::Test {
80 public:
81     static void SetUpTestCase(void);
82     static void TearDownTestCase(void);
83     void SetUp();
84     void TearDown();
85 };
86 
SetUpTestCase(void)87 void DistributedDBInterfacesNBResultsetPerfTest::SetUpTestCase(void)
88 {
89     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
90     g_config.dataDir = g_testDir;
91     g_mgr.SetKvStoreConfig(g_config);
92 }
93 
TearDownTestCase(void)94 void DistributedDBInterfacesNBResultsetPerfTest::TearDownTestCase(void)
95 {
96 }
97 
SetUp(void)98 void DistributedDBInterfacesNBResultsetPerfTest::SetUp(void)
99 {
100     DistributedDBToolsUnitTest::PrintTestCaseInfo();
101     g_kvDelegateStatus = INVALID_ARGS;
102     g_kvNbDelegatePtr = nullptr;
103 }
104 
TearDown(void)105 void DistributedDBInterfacesNBResultsetPerfTest::TearDown(void)
106 {
107     g_mgr.CloseKvStore(g_kvNbDelegatePtr);
108     g_kvNbDelegatePtr = nullptr;
109 }
110 
111 /**
112   * @tc.name: ResultSetPerfTest001
113   * @tc.desc: Test the NbDelegate for result set function.
114   * @tc.type: FUNC
115   * @tc.require: AR000D08KT
116   * @tc.author: wangbingquan
117   */
118 HWTEST_F(DistributedDBInterfacesNBResultsetPerfTest, ResultSetPerfTest001, TestSize.Level4)
119 {
120     /**
121      * @tc.steps: step1. initialize result set.
122      * @tc.expected: step1. Success.
123      */
124     KvStoreNbDelegate::Option option = {true, false, false};
125     g_mgr.GetKvStore("resultset_perf_test", option, g_kvNbDelegateCallback);
126     ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
127     EXPECT_TRUE(g_kvDelegateStatus == OK);
128     InitResultSet();
129 
130     /**
131      * @tc.steps: step2. get entries using result set.
132      * @tc.expected: step2. Success.
133      */
134     LOGI("######## Before get resultset");
135     KvStoreResultSet *readResultSet = nullptr;
136     Key keyGet = g_keyPrefix;
137     keyGet.push_back('1');
138 
139     int offset = 40; // offset 40
140     LOGI("######## Query resultSet");
141     Query query = Query::Select().PrefixKey(keyGet).Limit(50, offset); // limit 50
142     EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(query, readResultSet), OK);
143     ASSERT_TRUE(readResultSet != nullptr);
144     LOGI("######## After get resultset");
145     int totalCount = readResultSet->GetCount();
146     EXPECT_EQ(totalCount, 50); // limit 50
147     LOGI("######## After get count:%d", totalCount);
148 
149     readResultSet->MoveToPosition(0);
150     LOGI("######## After move to next");
151     EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(readResultSet), OK);
152     EXPECT_TRUE(readResultSet == nullptr);
153 
154     std::this_thread::sleep_for(std::chrono::seconds(5)); // sleep 5 s
155     LOGI("######## Plain resultSet");
156     EXPECT_EQ(g_kvNbDelegatePtr->GetEntries(keyGet, readResultSet), OK);
157     ASSERT_TRUE(readResultSet != nullptr);
158     LOGI("######## After get resultset");
159     totalCount = readResultSet->GetCount();
160     EXPECT_EQ(totalCount, INSERT_NUMBER);
161     LOGI("######## After get count:%d", totalCount);
162 
163     readResultSet->MoveToPosition(offset);
164     LOGI("######## After move to next");
165     EXPECT_EQ(g_kvNbDelegatePtr->CloseResultSet(readResultSet), OK);
166     EXPECT_TRUE(readResultSet == nullptr);
167 
168     EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
169     EXPECT_EQ(g_mgr.DeleteKvStore("resultset_perf_test"), OK);
170     g_kvNbDelegatePtr = nullptr;
171 }