• 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
16/*
17    This file was generated by VMB using these parameters:
18
19        Benchmarks Class:     $state_name
20        Bench Setup:          $state_setup
21        Bench Method:         $method_name -> $method_rettype
22        Parameter Set:        #$fix_id $fixture
23        Bench Full Name:      $bench_name
24        WarmUp Iterations:    $wi
25        Measure Iterations:   $mi
26        Iteration Time (sec): $it
27        Warmup Time (sec):    $wt
28        Fast Iterations:      $fi
29        GC pause (ms):        $gc (N/A)
30*/
31
32'use strict';
33
34$imports
35
36// params
37let WI = $wi;
38let MI = $mi;
39let IT = $it;
40let WT = $wt;
41let FI = $fi;
42
43const MAX_LOOP_COUNT = 10_000_000_000;
44const MS2NS = 1_000_000;
45const S2MS = 1_000;
46
47class Consumer {
48    static x1 = 0x41c64e6d;
49    static x2 = 0xd431;
50    static x3 = 1;
51
52    static boola = false;
53    static boolb = true;
54    static chara = 'X';
55    static charb = 'Y';
56    static inta = 24;
57    static intb = 53;
58    static floata = 24.0;
59    static floatb = 53.0;
60
61    static localObj = new Object();
62    static localObjs = [new Object()];
63    static pseudorand = Date.now();
64
65    static consumeBool(boolc) {
66        if (boolc === this.boola && boolc === this.boolb) {
67            throw new Error();
68        }
69    };
70
71    static consumeChar(charc) {
72        if (charc === this.chara && charc === this.charb) {
73            throw new Error();
74        }
75    };
76
77    static consumeInt(intc) {
78        if (intc === this.inta && intc === this.intb) {
79            throw new Error();
80        }
81    };
82
83    static consumeFloat(floatc) {
84        if (floatc === this.floata && floatc === this.floatb) {
85            throw new Error();
86        }
87    };
88
89    static consumeObj(obj) {
90        this.pseudorand = (this.pseudorand * this.x1 + this.x2);
91        if ((this.pseudorand & this.x3) === 0) {
92            this.x3 = (this.x3 << 1) + 0xad;
93            this.localObj = obj;
94        }
95    };
96
97    static consumeObjs(objs) {
98        this.pseudorand = (this.pseudorand * this.x1 + this.x2);
99        if ((this.pseudorand & this.x3) === 0) {
100            this.x3 = (this.x3 << 1) + 0xad;
101            this.localObjs = objs;
102        }
103    };
104};
105
106$common
107
108$src
109
110function log(msg) {
111    print(msg);
112}
113
114var bench = new $state_name();
115$state_params
116$state_setup
117
118var loopCount1;
119var totalOps;
120var totalMs;
121var iter;
122
123function tune() {
124    let iterMs = 1 * S2MS;
125    let loopMs = 0;
126    let loopCount = 1;
127    while (loopMs < iterMs && loopCount < MAX_LOOP_COUNT) {
128        loopCount = loopCount * 2;
129        let start = Date.now();
130        for (let i = 0; i < loopCount; i++) {
131            $method_call
132        }
133        loopMs = Date.now() - start;
134    }
135    loopCount1 = loopCount * iterMs / loopMs >> 0;
136    if (loopCount1 == 0) loopCount1++;
137    log('Tuning: ' +  loopCount + ' ops, ' + loopMs*MS2NS/loopCount + ' ns/op => '
138        + loopCount1 + ' reps');
139}
140
141function runIters(phase, count, time) {
142    let iterMs = time * S2MS;
143    totalOps = 0;
144    // make sure zero is encoded as double to avoid deoptimization on overflow later
145    totalMs = 1/Infinity;
146    for (let k = 0; k < count; k++, iter++) {
147        let ops = 0;
148        let elapsedMs = 0;
149        let start = Date.now();
150        while (elapsedMs < iterMs) {
151            for (let i = 0; i < loopCount1; i++) {
152                $method_call
153            }
154            elapsedMs = Date.now() - start;
155            ops += loopCount1;
156        }
157        totalOps += ops;
158        totalMs += elapsedMs;
159        log(phase + ' ' + iter + ':' + ops + ' ops, ' + elapsedMs*MS2NS/ops + ' ns/op');
160    }
161}
162
163log('Startup execution started: ' + Date.now() * MS2NS);
164if (FI > 0) {
165    let start = Date.now();
166    for (let i = 0; i < FI; i++) {
167        $method_call
168    }
169    let elapsed = Date.now() - start;
170    if (elapsed <= 0) {
171        elapsed = 1;  // 0 is invalid result
172    }
173    log('Benchmark result: $bench_name ' + elapsed*MS2NS / FI);
174} else {
175    tune();
176    if (WI > 0) {
177        iter = 1;
178        // Re-entering runIters in warmup loop to allow profiler complete the method.
179        // Possible deoptimizations and recompilations is done in warmup instead of measure phase.
180        for (let wi = 0; wi < WI; ++wi) {
181            runIters('Warmup', 1, WT);
182        }
183    }
184    iter = 1;
185    var measure_iters = MI >> 0; // make sure it has int type
186    runIters('Iter', measure_iters, IT);
187    log('Benchmark result: $bench_name ' + totalMs*MS2NS/totalOps);
188}
189
190Consumer.consumeObj(bench);
191