• 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('RenameProperty 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 RenameProperty benchmark test', function () {
63    let fileContent = `
64      class TestClass{
65        prop1 = 1; prop2 = 2; prop3 = 3; prop4 = 4; prop5 = 5;
66        prop6 = 6; prop7 = 7; prop8 = 8; prop9 = 9; prop10 = 10;
67        prop11 = 11; prop12 = 12; prop13 = 13; prop14 = 14; prop15 = 15;
68        prop16 = 16; prop17 = 17; prop18 = 18; prop19 = 19; prop20 = 20;
69      }
70    `;
71    let option: IOptions = {
72      mCompact: false,
73      mRemoveComments: false,
74      mOutputDir: '',
75      mDisableConsole: false,
76      mSimplify: false,
77      mNameObfuscation: {
78        mEnable: true,
79        mNameGeneratorType: 1,
80        mDictionaryList: [],
81        mRenameProperties: true,
82        mKeepStringProperty: false,
83        mTopLevel: false,
84        mReservedProperties: [],
85      },
86      mEnableSourceMap: false,
87      mEnableNameCache: false,
88    };
89    let transformer: TransformerFactory<Node> = transformerPlugin.createTransformerFactory(option);
90    const sourceFile: SourceFile = createSourceFile('demo.ts', fileContent, ScriptTarget.ES2015, true);
91
92    benchmarkSuite.add('property obfuscation test', function () {
93      let transformed = transform(sourceFile, [transformer]);
94    });
95
96    benchmarkSuite.on('cycle', (event: any) => {
97      console.log(' ', String(event.target));
98    });
99
100    benchmarkSuite.run({ async: true });
101  });
102});