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_PROMISE]";
22
23const DB_NAME = "rdbPromise.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('rdbHelperPromisePerf', 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_Promise_001', 0, async function (done) {
53    let averageTime = 0;
54    let startTime = new Date().getTime();
55    for (var i = 0; i < BASE_COUNT; i++) {
56      await dataRdb.getRdbStore(context, STORE_CONFIG, 1);
57    }
58    let endTime = new Date().getTime();
59    averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
60    console.info(TAG + " the getRdbStore_Promise average time is: " + averageTime + " μs");
61    expect(averageTime < BASE_LINE).assertTrue();
62    done();
63  })
64
65  it('SUB_DDM_PERF_RDB_deleteRdbStore_Promise_001', 0, async function (done) {
66    let averageTime = 0;
67    let startTime = new Date().getTime();
68    for (var i = 0; i < BASE_COUNT; i++) {
69      await dataRdb.deleteRdbStore(context, DB_NAME, 1);
70    }
71    let endTime = new Date().getTime();
72    averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
73    console.info(TAG + " the deleteRdbStore_Promise average time is: " + averageTime + " μs");
74    expect(averageTime < BASE_LINE).assertTrue();
75    done();
76    console.info(TAG + "*************Unit Test End*************")
77  })
78})