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