1/*
2 * Copyright (C) 2023 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
16import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Assert } from 'deccjsunit/index';
17import dataRdb from '@ohos.data.rdb';
18import featureAbility from '@ohos.ability.featureAbility';
19import deviceInfo from '@ohos.deviceInfo';
20
21const TAG = "[RDBHELPER_CALLBACK]";
22
23const DB_NAME = "rdbCallback.db";
24const STORE_CONFIG = {
25    name: DB_NAME,
26}
27let context = featureAbility.getContext();
28var rdbStore = undefined;
29const BASE_COUNT = 2000; // loop times
30const BASE_LINE_TABLE = 2500; // callback tablet base line
31const BASE_LINE_PHONE = 3000; // callback phone base line
32const BASE_LINE = (deviceInfo.deviceType == "tablet" || deviceInfo.deviceType == "2in1") ? BASE_LINE_TABLE : BASE_LINE_PHONE;
33
34describe('rdbHelperCallbackPerf', function () {
35    beforeAll(async function () {
36        console.info(TAG + 'beforeAll');
37    })
38    beforeEach(async function () {
39        console.info(TAG + 'beforeEach');
40    })
41    afterEach(async function () {
42        console.info(TAG + 'afterEach');
43    })
44    afterAll(async function () {
45        console.info(TAG + 'afterAll');
46        rdbStore = null
47        await dataRdb.deleteRdbStore(context, DB_NAME);
48    })
49
50    console.log(TAG + "*************Unit Test Begin*************");
51
52    it('SUB_DDM_PERF_RDB_getRdbStore_Callback_001', 0, async function (done) {
53        let averageTime = 0;
54
55        async function getRdbStoreCallBackPerf(index) {
56            dataRdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) {
57                if (index < BASE_COUNT) {
58                    getRdbStoreCallBackPerf(index + 1);
59                } else {
60                    let endTime = new Date().getTime();
61                    averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
62                    console.info(TAG + " the getRdbStore_Callback average time is: " + averageTime + " μs");
63                    expect(averageTime < BASE_LINE).assertTrue();
64                    done();
65                }
66            })
67        }
68
69        let startTime = new Date().getTime();
70        await getRdbStoreCallBackPerf(0);
71    })
72
73    it('SUB_DDM_PERF_RDB_deleteRdbStore_Callback_001', 0, async function (done) {
74        let averageTime = 0;
75
76        async function deleteRdbStoreCallBackPerf(index) {
77            dataRdb.deleteRdbStore(context, DB_NAME, function (err, data) {
78                if (index < BASE_COUNT) {
79                    deleteRdbStoreCallBackPerf(index + 1);
80                } else {
81                    let endTime = new Date().getTime();
82                    averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
83                    console.info(TAG + " the deleteRdbStore_Callback average time is: " + averageTime + " μs");
84                    expect(averageTime < BASE_LINE).assertTrue();
85                    done();
86                }
87            })
88        }
89
90        let startTime = new Date().getTime();
91        await deleteRdbStoreCallBackPerf(0);
92    })
93})