• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16let initBenchmarks = require('./caller.js').initBenchmarks;
17let JSCallee = require('./callee.js');
18
19function initArkVM() {
20    let penv = process.env;
21    let arkVM = require(penv.MODULE_PATH + '/ets_interop_js_napi');
22    // NB: Consider setting compiler-enable-jit=false for local run
23    const arkVMRes = arkVM.createRuntime({
24        'log-level': 'info',
25        'log-components': 'ets_interop_js',
26        'boot-panda-files': penv.ARK_ETS_STDLIB_PATH + ':' + penv.ARK_ETS_INTEROP_JS_GTEST_ABC_PATH,
27        'panda-files': penv.ARK_ETS_INTEROP_JS_GTEST_ABC_PATH,
28        'gc-trigger-type': 'heap-trigger',
29        'compiler-enable-jit': 'false',
30        'run-gc-in-place': 'true',
31    });
32
33    if (!arkVMRes) {
34        console.error('Failed to create ETS runtime');
35        abort();
36    }
37    return arkVM;
38}
39
40
41function runTest() {
42    console.log('Running test ' + bench + ' (' + callerSite + '->' + calleeSite + ')');
43
44    let benchmarks;
45    if (callerSite === 'JS') {
46        benchmarks = initBenchmarks(arkVM);
47    } else if (callerSite === 'STS') {
48        let initializer = arkVM.getFunction('LETSGLOBAL;', 'initBenchmarks');
49        benchmarks = initializer(callerSite !== calleeSite);
50    }
51
52    let caller = benchmarks[bench];
53    if (!caller) {
54        throw 'Benchmark not found: \'' + bench + '\' (' + callerSite + '->' + calleeSite + ')';
55    }
56    const MS2NS = 1000000;
57
58    // Warmup:
59    let start = Date.now();
60    caller(warmup);
61    let timeNs = (Date.now() - start) * MS2NS;
62    console.log('warmup ' + bench + ': ' + timeNs / warmup + ' ns/iter, iters: ' + warmup);
63
64    start = Date.now();
65    caller(iters);
66    timeNs = (Date.now() - start) * MS2NS;
67    console.log('iters  ' + bench + ': ' + timeNs / iters + ' ns/iter, iters: ' + iters);
68
69    return null;
70}
71
72
73globalThis.require = require;
74globalThis.jsCallee = JSCallee;
75
76let args = process.argv;
77if (args.length !== 7) {
78    console.log('Expected <test name> <caller-site(JS/STS)> <callee-site(JS/STS)> <warmup iters> <bench iters>');
79    process.exit(1);
80}
81let bench = args[2];
82let callerSite = args[3];
83let calleeSite = args[4];
84
85if (callerSite !== 'STS' && callerSite !== 'JS') {
86    throw 'Caller site should be \'STS\' or \'JS\', got \'' + callerSite + '\'.';
87}
88if (calleeSite !== 'STS' && calleeSite !== 'JS') {
89    throw 'Callee site should be \'STS\' or \'JS\', got \'' + calleeSite + '\'.';
90}
91let arkVM = (callerSite === 'STS' || calleeSite === 'STS') ? initArkVM() : undefined;
92let warmup = parseInt(args[5], 10);
93let iters = parseInt(args[6], 10);
94
95runTest();
96