idlize

Форк
0
499 строк · 17.2 Кб
1
/*
2
 * Copyright (c) 2024 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
import { NativeModule } from "./NativeModule"
16
import { ArkButtonPeer } from "@arkoala/arkui/ArkButtonPeer"
17
import { ArkUINodeType } from "@arkoala/arkui/ArkUINodeType"
18
import { ButtonType } from '@arkoala/arkui/ArkButtonInterfaces'
19

20
import {LabelStyle,
21
        TextOverflow,
22
        ResourceStr,
23
        TextHeightAdaptivePolicy,
24
        Font,
25
        SheetOptions,
26
        SheetTitleOptions,
27
        CustomBuilder,
28
        SheetSize,
29
        Length,
30
        ResourceColor,
31
        BlurStyle,
32
        SheetType,
33
        Resource,
34
        SheetDismiss,
35
        DismissSheetAction,
36
        SpringBackAction,
37
        Dimension,
38
        EdgeWidths,
39
        LocalizedEdgeWidths,
40
        EdgeColors,
41
        LocalizedEdgeColors,
42
        BorderStyle,
43
        EdgeStyles,
44
        ShadowOptions,
45
        ShadowStyle,
46
        SheetMode,
47
        UIContext,
48
        Callback,
49
        BlurOptions } from "./dts-exports"
50

51
const testString1000 = "One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand words One Thousand";
52

53
/*
54
V8:
55
backdropBlur: 1456ms for 5000000 iteration, 291ms per 1M iterations
56
widthAttributeString: 1006ms for 5000000 iteration, 201ms per 1M iterations
57

58

59
PANDA:
60
"backdropBlur: 3518ms for 5000000 iteration, 704ms per 5000000 iterations"
61
"widthAttributeString: 1380ms for 5000000 iteration, 276ms per 5000000 iterations"
62

63
JVM:
64
backdropBlur: 284ms for 5000000 iteration, 57ms per 1M iterations
65
widthAttributeString: 502ms for 5000000 iteration, 100ms per 1M iterations
66

67
*/
68
function checkPerf(count: number) {
69
    let start = Date.now()
70
    for (let i = 0; i < count; i++) {
71
        NativeModule._TestPerfNumber(i)
72
    }
73
    let passed = Date.now() - start
74
    console.log(`NUMBER: ${passed}ms for ${count} iteration, ${Math.round(passed / count * 1000000)}ms per 1M iterations`)
75

76
    start = Date.now()
77
    for (let i = 0; i < count; i++) {
78
        let data = new byte[5]
79
        data[0] = 1
80
        data[1] = (i >> 24) as byte
81
        data[2] = (i >> 16) as byte
82
        data[3] = (i >> 8) as byte
83
        data[4] = (i >> 0) as byte
84
        NativeModule._TestPerfNumberWithArray(data, data.length)
85
    }
86
    passed = Date.now() - start
87
    console.log(`ARRAY: ${passed}ms for ${count} iteration, ${Math.round(passed / count * 1000000)}ms per 1M iterations`)
88
}
89

90
export function getNativeLog(): string {
91
    let ptr = NativeModule._GetGroupedLog(1)
92
    let length = NativeModule._StringLength(ptr)
93
    let data = new byte[length]
94
    NativeModule._StringData(ptr, data, length)
95
    NativeModule._InvokeFinalizer(ptr, NativeModule._GetStringFinalizer())
96
    // TODO: better string decoding.
97
    let result = new StringBuilder()
98
    for (let i = 0; i < length; i++) {
99
        result.append(String.fromCharCode(data[i] as number))
100
    }
101
    return result.toString()
102
}
103

104
export function checkResult(name: string, test: () => void, expected: string) {
105
    NativeModule._StartGroupedLog(1)
106
    test()
107
    NativeModule._StopGroupedLog(1)
108
    const actual = getNativeLog()
109
    if (actual != expected) {
110
        console.log(`TEST ${name} FAIL:\n  EXPECTED "${expected}"\n  ACTUAL   "${actual}"`)
111
        console.log(`output: ${actual}`)
112
    } else {
113
        console.log(`TEST ${name} PASS`)
114
    }
115
}
116

117
class LabelStyleImpl implements LabelStyle {
118
    _textOverflow: TextOverflow | undefined
119
    _maxLines: number | undefined
120
    _minFontSize: number | ResourceStr | undefined
121
    _maxFontSize: number | ResourceStr | undefined
122
    _heightAdaptivePolicy: TextHeightAdaptivePolicy | undefined
123
    _font: Font | undefined
124

125
    constructor(maxLines?: number) {
126
        this._maxLines = maxLines
127
    }
128

129
    get overflow(): TextOverflow | undefined {
130
        return this._textOverflow
131
    }
132
    set overflow(arg: TextOverflow | undefined) {
133
        this._textOverflow = arg
134
    }
135

136
    get maxLines(): number | undefined {
137
        return this._maxLines
138
    }
139
    set maxLines(arg: number | undefined) {
140
        this._maxLines = arg
141
    }
142

143
    get minFontSize(): number | ResourceStr | undefined {
144
        return this._minFontSize
145
    }
146
    set minFontSize(arg: number | ResourceStr | undefined) {
147
        this._minFontSize = arg
148
    }
149

150
    get maxFontSize(): number | ResourceStr | undefined {
151
        return this._maxFontSize
152
    }
153
    set maxFontSize(arg: number | ResourceStr | undefined) {
154
        this._maxFontSize = arg
155
    }
156

157
    get heightAdaptivePolicy(): TextHeightAdaptivePolicy | undefined {
158
        return this._heightAdaptivePolicy
159
    }
160
    set heightAdaptivePolicy(arg: TextHeightAdaptivePolicy | undefined) {
161
        this._heightAdaptivePolicy = arg
162
    }
163

164
    get font(): Font | undefined {
165
        return this._font
166
    }
167
    set font(arg: Font | undefined) {
168
        this._font = arg
169
    }
170
}
171

172
class SheetTitleOptionsImpl implements SheetTitleOptions {
173
    _title: ResourceStr
174
    _subtitle: ResourceStr | undefined
175

176
    constructor(title: ResourceStr) {
177
        this._title = title
178
    }
179

180
    get title(): ResourceStr {
181
        return this._title
182
    }
183
    set title(arg: ResourceStr) {
184
        this._title = arg
185
    }
186

187
    get subtitle(): ResourceStr | undefined {
188
        return this._subtitle
189
    }
190
    set subtitle(arg: ResourceStr | undefined) {
191
        this._subtitle = arg
192
    }
193
}
194

195
class SheetOptionsImpl implements SheetOptions {
196
    _title: SheetTitleOptions | CustomBuilder| undefined
197
    _detents: [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined
198
    _height: SheetSize | Length| undefined
199
    _dragBar: boolean| undefined
200
    _maskColor: ResourceColor| undefined
201
    _blurStyle: BlurStyle| undefined
202
    _showClose: boolean | Resource| undefined
203
    _preferType: SheetType| undefined
204
    _shouldDismiss: ((sheetDismiss: SheetDismiss) => void) | undefined
205
    _onWillDismiss: Callback<DismissSheetAction>| undefined
206
    _onWillSpringBackWhenDismiss: Callback<SpringBackAction>| undefined
207
    _enableOutsideInteractive: boolean| undefined
208
    _width: Dimension| undefined
209
    _borderWidth: Dimension | EdgeWidths | LocalizedEdgeWidths| undefined
210
    _borderColor: ResourceColor | EdgeColors | LocalizedEdgeColors| undefined
211
    _borderStyle: BorderStyle | EdgeStyles| undefined
212
    _shadow: ShadowOptions | ShadowStyle| undefined
213
    _onHeightDidChange: Callback<number>| undefined
214
    _mode: SheetMode| undefined
215
    _onDetentsDidChange: Callback<number>| undefined
216
    _onWidthDidChange: Callback<number>| undefined
217
    _onTypeDidChange: Callback<int>| undefined
218
    _uiContext: UIContext| undefined
219
    _backgroundColor: ResourceColor | undefined
220
    _onAppear: (() => void) | undefined
221
    _onDisappear: (() => void) | undefined
222
    _onWillAppear: (() => void) | undefined
223
    _onWillDisappear: (() => void) | undefined
224

225
    constructor(title?: SheetTitleOptions) {
226
        this._title = title
227
    }
228

229
    get title(): SheetTitleOptions | CustomBuilder| undefined {
230
        return this._title
231
    }
232
    set title(arg: SheetTitleOptions | CustomBuilder| undefined) {
233
        this._title = arg
234
    }
235

236
    get onWillDismiss(): Callback<DismissSheetAction>| undefined {
237
        return this._onWillDismiss
238
    }
239
    set onWillDismiss(arg: Callback<DismissSheetAction>| undefined) {
240
        this._onWillDismiss = arg
241
    }
242

243
    get detents(): [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined {
244
        return this._detents
245
    }
246
    set detents(arg: [(SheetSize | Length | undefined), (SheetSize | Length | undefined), (SheetSize | Length | undefined)] | undefined) {
247
        this._detents = arg
248
    }
249

250
    get height(): SheetSize | Length| undefined {
251
        return this._height
252
    }
253
    set height(arg: SheetSize | Length| undefined) {
254
        this._height = arg
255
    }
256

257
    get dragBar(): boolean| undefined {
258
        return this._dragBar
259
    }
260
    set dragBar(arg: boolean| undefined) {
261
        this._dragBar = arg
262
    }
263

264
    get maskColor(): ResourceColor| undefined {
265
        return this._maskColor
266
    }
267
    set maskColor(arg: ResourceColor| undefined) {
268
        this._maskColor = arg
269
    }
270

271
    get blurStyle(): BlurStyle| undefined {
272
        return this._blurStyle
273
    }
274
    set blurStyle(arg: BlurStyle| undefined) {
275
        this._blurStyle = arg
276
    }
277

278
    get showClose(): boolean | Resource| undefined {
279
        return this._showClose
280
    }
281
    set showClose(arg: boolean | Resource| undefined) {
282
        this._showClose = arg
283
    }
284

285
    get preferType(): SheetType| undefined {
286
        return this._preferType
287
    }
288
    set preferType(arg: SheetType| undefined) {
289
        this._preferType = arg
290
    }
291

292
    get shouldDismiss(): ((sheetDismiss: SheetDismiss) => void) | undefined {
293
        return this._shouldDismiss
294
    }
295
    set shouldDismiss(arg: ((sheetDismiss: SheetDismiss) => void) | undefined) {
296
        this._shouldDismiss = arg
297
    }
298

299
    get onWillSpringBackWhenDismiss(): Callback<SpringBackAction>| undefined {
300
        return this._onWillSpringBackWhenDismiss
301
    }
302
    set onWillSpringBackWhenDismiss(arg: Callback<SpringBackAction>| undefined) {
303
        this._onWillSpringBackWhenDismiss = arg
304
    }
305

306
    get enableOutsideInteractive(): boolean| undefined {
307
        return this._enableOutsideInteractive
308
    }
309
    set enableOutsideInteractive(arg: boolean| undefined) {
310
        this._enableOutsideInteractive = arg
311
    }
312

313
    get width(): Dimension| undefined {
314
        return this._width
315
    }
316
    set width(arg: Dimension| undefined) {
317
        this._width = arg
318
    }
319

320
    get borderWidth(): Dimension | EdgeWidths | LocalizedEdgeWidths| undefined {
321
        return this._borderWidth
322
    }
323
    set borderWidth(arg: Dimension | EdgeWidths | LocalizedEdgeWidths| undefined) {
324
        this._borderWidth = arg
325
    }
326

327
    get borderColor(): ResourceColor | EdgeColors | LocalizedEdgeColors| undefined {
328
        return this._borderColor
329
    }
330
    set borderColor(arg: ResourceColor | EdgeColors | LocalizedEdgeColors| undefined) {
331
        this._borderColor = arg
332
    }
333

334
    get borderStyle(): BorderStyle | EdgeStyles| undefined {
335
        return this._borderStyle
336
    }
337
    set borderStyle(arg: BorderStyle | EdgeStyles| undefined) {
338
        this._borderStyle = arg
339
    }
340

341
    get shadow(): ShadowOptions | ShadowStyle| undefined {
342
        return this._shadow
343
    }
344
    set shadow(arg: ShadowOptions | ShadowStyle| undefined) {
345
        this._shadow = arg
346
    }
347

348
    get onHeightDidChange(): Callback<number>| undefined {
349
        return this._onHeightDidChange
350
    }
351
    set onHeightDidChange(arg: Callback<number>| undefined) {
352
        this._onHeightDidChange = arg
353
    }
354

355
    get mode(): SheetMode| undefined {
356
        return this._mode
357
    }
358
    set mode(arg: SheetMode| undefined) {
359
        this._mode = arg
360
    }
361

362
    get onDetentsDidChange(): Callback<number>| undefined {
363
        return this._onDetentsDidChange
364
    }
365
    set onDetentsDidChange(arg: Callback<number>| undefined) {
366
        this._onDetentsDidChange = arg
367
    }
368

369
    get onWidthDidChange(): Callback<number>| undefined {
370
        return this._onWidthDidChange
371
    }
372
    set onWidthDidChange(arg: Callback<number>| undefined) {
373
        this._onWidthDidChange = arg
374
    }
375

376
    get onTypeDidChange(): Callback<int>| undefined {
377
        return this._onTypeDidChange
378
    }
379
    set onTypeDidChange(arg: Callback<int>| undefined) {
380
        this._onTypeDidChange = arg
381
    }
382

383
    get uiContext(): UIContext| undefined {
384
        return this._uiContext
385
    }
386
    set uiContext(arg: UIContext| undefined) {
387
        this._uiContext = arg
388
    }
389

390
    get backgroundColor(): ResourceColor | undefined {
391
        return this._backgroundColor
392
    }
393
    set backgroundColor(arg: ResourceColor | undefined) {
394
        this._backgroundColor = arg
395
    }
396

397
    get onAppear(): (() => void) | undefined {
398
        return this._onAppear
399
    }
400
    set onAppear(arg: (() => void) | undefined) {
401
        this._onAppear = arg
402
    }
403

404
    get onDisappear(): (() => void) | undefined {
405
        return this._onDisappear
406
    }
407
    set onDisappear(arg: (() => void) | undefined) {
408
        this._onDisappear = arg
409
    }
410

411
    get onWillAppear(): (() => void) | undefined {
412
        return this._onWillAppear
413
    }
414
    set onWillAppear(arg: (() => void) | undefined) {
415
        this._onWillAppear = arg
416
    }
417

418
    get onWillDisappear(): (() => void) | undefined {
419
        return this._onWillDisappear
420
    }
421
    set onWillDisappear(arg: (() => void) | undefined) {
422
        this._onWillDisappear = arg
423
    }
424
}
425

426
class BlurOptionsImpl implements BlurOptions {
427
    _grayscale: [number, number]
428

429
    constructor(grayscale: [number, number]) {
430
        this._grayscale = grayscale
431
    }
432

433
    get grayscale(): [number, number] {
434
        return this._grayscale
435
    }
436
    set grayscale(arg: [number, number]) {
437
        this._grayscale = arg
438
    }
439
}
440

441
function checkPerf2(count: number) {
442
    let peer = new ArkButtonPeer(ArkUINodeType.Button)
443
    let start = Date.now()
444
    for (let i = 0; i < count; i++) {
445
        let data: byte[]
446
        if ( i % 2 == 0) {
447
            data = new byte[2]
448
            data[0] = 1
449
            data[1] = 2
450
        } else {
451
            data = new byte[0]
452
        }
453
        peer.backdropBlurAttribute(i, i % 2 == 0 ? undefined : new BlurOptionsImpl([1, 2] as [number, number]))
454
    }
455
    let passed = Date.now() - start
456
    console.log(`backdropBlur: ${Math.round(passed)}ms for ${count} iteration, ${Math.round(passed / count * 1_000_000)}ms per 1M iterations`)
457
}
458

459
function checkPerf3(count: number) {
460
    let peer = new ArkButtonPeer(ArkUINodeType.Button)
461
    let start = Date.now()
462
    for (let i = 0; i < count; i++) {
463
        peer.widthAttribute(testString1000)
464
    }
465
    let passed = Date.now() - start
466
    console.log(`widthAttributeString: ${Math.round(passed)}ms for ${count} iteration, ${Math.round(passed / count * 1_000_000)}ms per 1M iterations`)
467
}
468

469
function checkButton() {
470
    let data = new byte[5]
471
    data[0] = 42
472
    checkResult("TestPerfNumberWithArray",
473
        () => NativeModule._TestPerfNumberWithArray(data, data.length),
474
        "TestPerfNumberWithArray(42, 5)")
475
    let peer = new ArkButtonPeer(ArkUINodeType.Button)
476
    checkResult("width", () => peer.widthAttribute("42%"),
477
        "width({1, 42.000000, 3, 0})")
478
    checkResult("type", () => peer.typeAttribute(1 as ButtonType), "type(1)")
479
    checkResult("labelStyle", () => peer.labelStyleAttribute(new LabelStyleImpl(3)),
480
         "labelStyle({{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_OBJECT, {102, .i32=3}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}})")
481
    checkResult("labelStyle2", () => peer.labelStyleAttribute(new LabelStyleImpl()),
482
        "labelStyle({{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}})")
483
/*
484
    checkResult("height", () => peer.heightAttribute({ id: 43, bundleName: "MyApp", moduleName: "MyApp" }),
485
        "height(Length {value=0.000000, unit=vp, resource=43})")
486
*/
487
    checkResult("bindSheet", () =>
488
        peer.bindSheetAttribute(false, () => {}, new SheetOptionsImpl(new SheetTitleOptionsImpl("My App"))),
489
        "bindSheet(false, {0, .value0={42}}, {ARK_TAG_OBJECT, {{ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_OBJECT, {0, .value0={{0, .value0={\"My App\", 6}}, {ARK_TAG_UNDEFINED, {}}}}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}, {ARK_TAG_UNDEFINED, {}}}})"
490
    )
491
}
492

493
export function main(): void {
494
    checkPerf(5 * 1000 * 1000)
495
    checkPerf2(5 * 1000 * 1000)
496
    checkPerf3(5 * 1000 * 1000)
497

498
    checkButton()
499
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.