• 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
16import Benchmark from 'benchmark';
17import { IOptions } from '../../src/configs/IOptions';
18import { createSourceFile, Node, ScriptTarget, SourceFile, transform, TransformerFactory } from 'typescript';
19import { transformerPlugin } from '../../src/transformers/rename/RenameIdentifierTransformer';
20
21const benchmarkSuite = new Benchmark.Suite;
22const namePaddingSize: number = 56;
23const dataPaddingSize: number = 16;
24
25function alignColumn(name, ops, variance, runsSampled) {
26  const namePadding = name.padEnd(namePaddingSize);
27  const opsPadding = ops.padEnd(dataPaddingSize);
28  const variancePadding = variance.padEnd(dataPaddingSize);
29  const runsSampledPadding = runsSampled.toString().padEnd(dataPaddingSize);
30  return `| ${namePadding} | ${opsPadding} | ${variancePadding} | ${runsSampledPadding} |`;
31}
32
33describe('RenameIdentifier Benchmark Test', function() {
34  after(() => {
35    benchmarkSuite.on('complete', function () {
36      console.log('\n  Benchmark Summary Results:\n');
37      console.log(' ', alignColumn('Test Name', 'Ops/Sec', 'Variance', 'Number of Runs'));
38      console.log(
39        ' ',
40        alignColumn(
41          '-'.repeat(namePaddingSize),
42          '-'.repeat(dataPaddingSize),
43          '-'.repeat(dataPaddingSize),
44          '-'.repeat(dataPaddingSize),
45        ),
46      );
47      this.forEach((result) => {
48        console.log(
49          ' ',
50          alignColumn(
51            result.name,
52            `${Math.round(result.hz)}`,
53            `${result.stats.rme.toFixed(2)}%`,
54            result.stats.sample.length,
55          ),
56        );
57      });
58      console.log('\n');
59    });
60  });
61
62  it('run RenameIdentifier benchmark test', function () {
63    let fileContent = `
64      let var1 = 1;\nlet var2 = 1;\nlet var3 = 1;\nlet var4 = 1;\nlet var5 = 1;\nlet var6 = 1;\nlet var7 = 1;\n
65      let var8 = 1;\nlet var9 = 1;\nlet var10 = 1;\nlet var11 = 1;\nlet var12 = 1;\nlet var13 = 1;\nlet var14 = 1;\n
66      let var15 = 1;\nlet var16 = 1;\nlet var17 = 1;\nlet var18 = 1;\nlet var19 = 1;\nlet var20 = 1;\nlet var21 = 1;\n
67    `;
68    let option: IOptions = {
69      mCompact: false,
70      mRemoveComments: false,
71      mOutputDir: '',
72      mDisableConsole: false,
73      mSimplify: false,
74      mNameObfuscation: {
75        mEnable: true,
76        mNameGeneratorType: 1,
77        mDictionaryList: [],
78        mRenameProperties: false,
79        mKeepStringProperty: false,
80        mTopLevel: true,
81        mReservedProperties: [],
82      },
83      mEnableSourceMap: false,
84      mEnableNameCache: false,
85    };
86    let transformer: TransformerFactory<Node> = transformerPlugin.createTransformerFactory(option);
87    const sourceFile: SourceFile = createSourceFile('demo.ts', fileContent, ScriptTarget.ES2015, true);
88
89    benchmarkSuite.add('identifier obfuscation test', function () {
90      let transformed = transform(sourceFile, [transformer]);
91    });
92
93    benchmarkSuite.on('cycle', (event: any) => {
94      console.log(' ', String(event.target));
95    });
96
97    benchmarkSuite.run({ async: true });
98  });
99});