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; 23 24var context; 25 26const TAG = '[SUB_DDM_PERF_PreferencesInstanceSyncJsPref]' 27 28describe("PreferencesInstanceSyncJsPref", async function () { 29 beforeAll(async function () { 30 console.info(`${TAG}beforeAll`) 31 context = featureAbility.getContext() 32 }) 33 34 beforeEach(async function () { 35 console.info(`${TAG}beforeEach`); 36 }) 37 38 afterEach(async function () { 39 console.info(`${TAG}afterEach`); 40 }) 41 42 afterAll(async function () { 43 console.info(`${TAG}afterAll`) 44 }) 45 46 /** 47 * @tc.desc PreferencesInstanceSyncJsPref_ GetPreferencesSync to obtain the same preferences instance 48 */ 49 it("getPreferencesSync_0001", 0, function () { 50 let startTime = new Date().getTime(); // time unit is ms 51 for (let index = 0; index < BASE_COUNT; index++) { 52 dataPreferences.getPreferencesSync(context, { name: NAME }); 53 } 54 let endTime = new Date().getTime(); 55 let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT; 56 console.info(`${TAG}getPreferencesSync_0001 averageTime: ${averageTime} us`); 57 expect(averageTime < BASELINE).assertTrue(); 58 }) 59 60 /** 61 * @tc.desc PreferencesInstanceSyncJsPref_ GetPreferencesSync to obtain different instances of preferences 62 */ 63 it("getPreferencesSync_0002", 0, function () { 64 let startTime = new Date().getTime(); // time unit is ms 65 for (let index = 0; index < BASE_COUNT; index++) { 66 dataPreferences.getPreferencesSync(context, { name: `${NAME}${index}` }); 67 } 68 let endTime = new Date().getTime(); 69 let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT; 70 console.info(`${TAG}getPreferencesSync_0002 averageTime: ${averageTime} us`); 71 expect(averageTime < BASELINE).assertTrue(); 72 }) 73 74 /** 75 * @tc.desc PreferencesInstanceSyncJsPref_RemovePreferencesFromCacheSync with repeating one pref 76 */ 77 it("removePreferencesFromCacheSync_0003", 0, function () { 78 let startTime = new Date().getTime(); // time unit is ms 79 for (let index = 0; index < BASE_COUNT; index++) { 80 dataPreferences.removePreferencesFromCacheSync(context, { name: NAME }); 81 } 82 let endTime = new Date().getTime(); 83 let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT; 84 console.info(`${TAG}removePreferencesFromCacheSync_0003 averageTime: ${averageTime} us`); 85 expect(averageTime < BASELINE).assertTrue(); 86 }) 87 88 /** 89 * @tc.desc PreferencesInstanceSyncJsPref_RemovePreferencesFromCacheSync with diff pref 90 */ 91 it("removePreferencesFromCacheSync_0004", 0, function () { 92 for (let index = 0; index < BASE_COUNT; index++) { 93 dataPreferences.getPreferencesSync(context, { name: `${NAME}${index}` }); // put prefs into cache 94 } 95 let startTime = new Date().getTime(); // time unit is ms 96 for (let index = 0; index < BASE_COUNT; index++) { 97 dataPreferences.removePreferencesFromCacheSync(context, { name: `${NAME}${index}` }); 98 } 99 let endTime = new Date().getTime(); 100 let averageTime = ((endTime - startTime) * 1000) / BASE_COUNT; 101 console.info(`${TAG}removePreferencesFromCacheSync_0004 averageTime: ${averageTime} us`); 102 expect(averageTime < BASELINE).assertTrue(); 103 }) 104})