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 {beforeAll, beforeEach, describe, afterEach, afterAll, expect, it} from 'deccjsunit/index';
17import dataPreferences from '@ohos.data.preferences'
18import featureAbility from '@ohos.ability.featureAbility';
19
20const NAME = 'test_preferences';
21const BASE_COUNT = 2000;
22const BASELINE = 25000;
23const KEY_TEST_STRING_KEY = 'key_test_string';
24const KEY_TEST_STRING_VAL = '0123456789';
25
26var mPreference;
27var context;
28
29const TAG = '[SUB_DDM_PERF_PreferencesOperationSyncJsPref]'
30
31describe("PreferencesOperationSyncJsPref", async function () {
32    beforeAll(async function () {
33        console.info(`${TAG}beforeAll`)
34        context = featureAbility.getContext()
35        mPreference = await dataPreferences.getPreferences(context, NAME);
36    })
37
38    beforeEach(async function () {
39        console.info(`${TAG}beforeEach`);
40    })
41
42    afterEach(async function () {
43        console.info(`${TAG}afterEach`);
44    })
45
46    afterAll(async function () {
47        console.info(`${TAG}afterAll`)
48        await dataPreferences.deletePreferences(context, NAME);
49    })
50
51    /**
52     * @tc.desc PreferencesOperationSyncJsPref_PutSync
53     */
54    it("putSync_0001", 0, async function () {
55        let startTime = new Date().getTime(); // time unit is mm
56        for (let index = 0; index < BASE_COUNT; index++) {
57            mPreference.putSync(KEY_TEST_STRING_KEY, KEY_TEST_STRING_VAL);
58        }
59        let endTime = new Date().getTime();
60        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
61        console.info(`${TAG}putSync_0001 averageTime: ${averageTime} us`);
62        expect(averageTime < BASELINE).assertTrue();
63    })
64
65    /**
66     * @tc.desc PreferencesOperationSyncJsPref_GetSync
67     */
68    it("getSync_0001", 0, async function () {
69        let startTime = new Date().getTime(); // time unit is mm
70        for (let index = 0; index < BASE_COUNT; index++) {
71            mPreference.getSync(KEY_TEST_STRING_KEY, KEY_TEST_STRING_VAL);
72        }
73        let endTime = new Date().getTime();
74        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
75        console.info(`${TAG}getSync_0001 averageTime: ${averageTime} us`);
76        expect(averageTime < BASELINE).assertTrue();
77    })
78
79    /**
80     * @tc.desc PreferencesOperationSyncJsPref_HasSync
81     */
82    it("hasSync_0001", 0, async function () {
83        let startTime = new Date().getTime(); // time unit is mm
84        for (let index = 0; index < BASE_COUNT; index++) {
85            mPreference.hasSync(KEY_TEST_STRING_KEY);
86        }
87        let endTime = new Date().getTime();
88        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
89        console.info(`${TAG}hasSync_0001 averageTime: ${averageTime} us`);
90        expect(averageTime < BASELINE).assertTrue();
91
92    })
93
94    /**
95     * @tc.desc PreferencesOperationSyncJsPref_GetAllSync
96     */
97    it("getAllSync_0001", 0, async function () {
98        let startTime = new Date().getTime(); // time unit is mm
99        for (let index = 0; index < BASE_COUNT; index++) {
100            mPreference.getAllSync();
101        }
102        let endTime = new Date().getTime();
103        let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT;
104        console.info(`${TAG}getAllSync_0001 averageTime: ${averageTime} us`);
105        expect(averageTime < BASELINE).assertTrue();
106    })
107})