1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc223305Sopenharmony_ci * You may obtain a copy of the License at
6fc223305Sopenharmony_ci *
7fc223305Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc223305Sopenharmony_ci *
9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and
13fc223305Sopenharmony_ci * limitations under the License.
14fc223305Sopenharmony_ci */
15fc223305Sopenharmony_ci
16fc223305Sopenharmony_ciimport {beforeAll, beforeEach, describe, afterEach, afterAll, expect, it} from 'deccjsunit/index';
17fc223305Sopenharmony_ciimport dataPreferences from '@ohos.data.preferences'
18fc223305Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility';
19fc223305Sopenharmony_ci
20fc223305Sopenharmony_ciconst NAME = 'test_preferences';
21fc223305Sopenharmony_ciconst BASE_COUNT = 2000;
22fc223305Sopenharmony_ciconst BASELINE = 25000;
23fc223305Sopenharmony_ciconst KEY_TEST_STRING_KEY = 'key_test_string';
24fc223305Sopenharmony_ciconst KEY_TEST_STRING_VAL = '0123456789';
25fc223305Sopenharmony_ci
26fc223305Sopenharmony_civar mPreference;
27fc223305Sopenharmony_civar context;
28fc223305Sopenharmony_ci
29fc223305Sopenharmony_ciconst TAG = '[SUB_DDM_PERF_PreferencesOperationSyncJsPref]'
30fc223305Sopenharmony_ci
31fc223305Sopenharmony_cidescribe("PreferencesOperationSyncJsPref", async function () {
32fc223305Sopenharmony_ci    beforeAll(async function () {
33fc223305Sopenharmony_ci        console.info(`${TAG}beforeAll`)
34fc223305Sopenharmony_ci        context = featureAbility.getContext()
35fc223305Sopenharmony_ci        mPreference = await dataPreferences.getPreferences(context, NAME);
36fc223305Sopenharmony_ci    })
37fc223305Sopenharmony_ci
38fc223305Sopenharmony_ci    beforeEach(async function () {
39fc223305Sopenharmony_ci        console.info(`${TAG}beforeEach`);
40fc223305Sopenharmony_ci    })
41fc223305Sopenharmony_ci
42fc223305Sopenharmony_ci    afterEach(async function () {
43fc223305Sopenharmony_ci        console.info(`${TAG}afterEach`);
44fc223305Sopenharmony_ci    })
45fc223305Sopenharmony_ci
46fc223305Sopenharmony_ci    afterAll(async function () {
47fc223305Sopenharmony_ci        console.info(`${TAG}afterAll`)
48fc223305Sopenharmony_ci        await dataPreferences.deletePreferences(context, NAME);
49fc223305Sopenharmony_ci    })
50fc223305Sopenharmony_ci
51fc223305Sopenharmony_ci    /**
52fc223305Sopenharmony_ci     * @tc.desc PreferencesOperationSyncJsPref_PutSync
53fc223305Sopenharmony_ci     */
54fc223305Sopenharmony_ci    it("putSync_0001", 0, async function () {
55fc223305Sopenharmony_ci        let startTime = new Date().getTime(); // time unit is mm
56fc223305Sopenharmony_ci        for (let index = 0; index < BASE_COUNT; index++) {
57fc223305Sopenharmony_ci            mPreference.putSync(KEY_TEST_STRING_KEY, KEY_TEST_STRING_VAL);
58fc223305Sopenharmony_ci        }
59fc223305Sopenharmony_ci        let endTime = new Date().getTime();
60fc223305Sopenharmony_ci        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
61fc223305Sopenharmony_ci        console.info(`${TAG}putSync_0001 averageTime: ${averageTime} us`);
62fc223305Sopenharmony_ci        expect(averageTime < BASELINE).assertTrue();
63fc223305Sopenharmony_ci    })
64fc223305Sopenharmony_ci
65fc223305Sopenharmony_ci    /**
66fc223305Sopenharmony_ci     * @tc.desc PreferencesOperationSyncJsPref_GetSync
67fc223305Sopenharmony_ci     */
68fc223305Sopenharmony_ci    it("getSync_0001", 0, async function () {
69fc223305Sopenharmony_ci        let startTime = new Date().getTime(); // time unit is mm
70fc223305Sopenharmony_ci        for (let index = 0; index < BASE_COUNT; index++) {
71fc223305Sopenharmony_ci            mPreference.getSync(KEY_TEST_STRING_KEY, KEY_TEST_STRING_VAL);
72fc223305Sopenharmony_ci        }
73fc223305Sopenharmony_ci        let endTime = new Date().getTime();
74fc223305Sopenharmony_ci        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
75fc223305Sopenharmony_ci        console.info(`${TAG}getSync_0001 averageTime: ${averageTime} us`);
76fc223305Sopenharmony_ci        expect(averageTime < BASELINE).assertTrue();
77fc223305Sopenharmony_ci    })
78fc223305Sopenharmony_ci
79fc223305Sopenharmony_ci    /**
80fc223305Sopenharmony_ci     * @tc.desc PreferencesOperationSyncJsPref_HasSync
81fc223305Sopenharmony_ci     */
82fc223305Sopenharmony_ci    it("hasSync_0001", 0, async function () {
83fc223305Sopenharmony_ci        let startTime = new Date().getTime(); // time unit is mm
84fc223305Sopenharmony_ci        for (let index = 0; index < BASE_COUNT; index++) {
85fc223305Sopenharmony_ci            mPreference.hasSync(KEY_TEST_STRING_KEY);
86fc223305Sopenharmony_ci        }
87fc223305Sopenharmony_ci        let endTime = new Date().getTime();
88fc223305Sopenharmony_ci        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
89fc223305Sopenharmony_ci        console.info(`${TAG}hasSync_0001 averageTime: ${averageTime} us`);
90fc223305Sopenharmony_ci        expect(averageTime < BASELINE).assertTrue();
91fc223305Sopenharmony_ci
92fc223305Sopenharmony_ci    })
93fc223305Sopenharmony_ci
94fc223305Sopenharmony_ci    /**
95fc223305Sopenharmony_ci     * @tc.desc PreferencesOperationSyncJsPref_GetAllSync
96fc223305Sopenharmony_ci     */
97fc223305Sopenharmony_ci    it("getAllSync_0001", 0, async function () {
98fc223305Sopenharmony_ci        let startTime = new Date().getTime(); // time unit is mm
99fc223305Sopenharmony_ci        for (let index = 0; index < BASE_COUNT; index++) {
100fc223305Sopenharmony_ci            mPreference.getAllSync();
101fc223305Sopenharmony_ci        }
102fc223305Sopenharmony_ci        let endTime = new Date().getTime();
103fc223305Sopenharmony_ci        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
104fc223305Sopenharmony_ci        console.info(`${TAG}getAllSync_0001 averageTime: ${averageTime} us`);
105fc223305Sopenharmony_ci        expect(averageTime < BASELINE).assertTrue();
106fc223305Sopenharmony_ci    })
107fc223305Sopenharmony_ci})