13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciimport * as fs from 'fs'; 173af6ab5fSopenharmony_ciimport mocha from 'mocha'; 183af6ab5fSopenharmony_ciimport { TimeTracker, TimeSumPrinter } from '../../../src/utils/PrinterUtils'; 193af6ab5fSopenharmony_ciimport { assert, expect } from 'chai'; 203af6ab5fSopenharmony_ciimport { isFileExist } from '../../../src/initialization/utils'; 213af6ab5fSopenharmony_ciconst sinon = require('sinon'); 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cidescribe('test Cases for <PrinterUtils>.', function () { 243af6ab5fSopenharmony_ci describe('Tester Cases for <TimeTracker>.', function () { 253af6ab5fSopenharmony_ci let printer: TimeTracker; 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_ci beforeEach(() => { 283af6ab5fSopenharmony_ci printer = new TimeTracker(); 293af6ab5fSopenharmony_ci }) 303af6ab5fSopenharmony_ci 313af6ab5fSopenharmony_ci describe('Tester Cases for <setOutputPath>.', function () { 323af6ab5fSopenharmony_ci /** test for setOutputPath */ 333af6ab5fSopenharmony_ci it('Tester: <setOutputPath> case for TimeTracker#setOutputPath', function () { 343af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo1.txt'; 353af6ab5fSopenharmony_ci printer.setOutputPath(path); 363af6ab5fSopenharmony_ci let content: string = printer.getOutputPath(); 373af6ab5fSopenharmony_ci assert.strictEqual(content, path); 383af6ab5fSopenharmony_ci }); 393af6ab5fSopenharmony_ci }); 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_ci describe('Tester Cases for <print>.', function () { 423af6ab5fSopenharmony_ci /** test for print */ 433af6ab5fSopenharmony_ci it('Tester: <available outputPath> case for TimeTracker#print', function () { 443af6ab5fSopenharmony_ci let path = 'test/ut/utils/testTimeTrackerPrint.txt'; 453af6ab5fSopenharmony_ci printer.setOutputPath(path); 463af6ab5fSopenharmony_ci printer.print('available outputPath case'); 473af6ab5fSopenharmony_ci let content: string = fs.readFileSync(path, 'utf-8'); 483af6ab5fSopenharmony_ci assert.strictEqual(content, 'available outputPath case\n'); 493af6ab5fSopenharmony_ci fs.unlinkSync(path); 503af6ab5fSopenharmony_ci }); 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci it('Tester: <unavailable outputPath> case for TimeTracker#print', function () { 533af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo2.txt'; 543af6ab5fSopenharmony_ci let message = 'unavailable outputPath case'; 553af6ab5fSopenharmony_ci var spy = sinon.spy(console, 'log'); 563af6ab5fSopenharmony_ci printer.print(message); 573af6ab5fSopenharmony_ci assert(spy.calledWith(message), message); 583af6ab5fSopenharmony_ci assert.strictEqual(isFileExist(path), false); 593af6ab5fSopenharmony_ci spy.restore(); 603af6ab5fSopenharmony_ci }); 613af6ab5fSopenharmony_ci }); 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_ci describe('Tester Cases for <startEvent>.', function () { 643af6ab5fSopenharmony_ci /** test for startEvent */ 653af6ab5fSopenharmony_ci it('Tester: <start test event> case for TimeTracker#startEvent', function () { 663af6ab5fSopenharmony_ci let eventName = 'test event'; 673af6ab5fSopenharmony_ci let path = 'test/ut/utils/testTimeTrackerPrint.txt'; 683af6ab5fSopenharmony_ci let message = 'start test event'; 693af6ab5fSopenharmony_ci printer.setOutputPath(path); 703af6ab5fSopenharmony_ci printer.startEvent(eventName, undefined, message); 713af6ab5fSopenharmony_ci const eventData = printer.getEventStack().get(eventName); 723af6ab5fSopenharmony_ci expect(eventData?.duration).to.equal(0); 733af6ab5fSopenharmony_ci expect(eventData?.endMemory).to.equal(0); 743af6ab5fSopenharmony_ci expect(eventData?.memoryUsage).to.equal(0); 753af6ab5fSopenharmony_ci assert.strictEqual(isFileExist(path), false); 763af6ab5fSopenharmony_ci }); 773af6ab5fSopenharmony_ci 783af6ab5fSopenharmony_ci it('Tester: <start create ast event> case for TimeTracker#startEvent', function () { 793af6ab5fSopenharmony_ci let eventName = 'Create AST'; 803af6ab5fSopenharmony_ci let path = 'test/ut/utils/testTimeTrackerPrint.txt'; 813af6ab5fSopenharmony_ci let message = 'start Create AST event'; 823af6ab5fSopenharmony_ci printer.setOutputPath(path); 833af6ab5fSopenharmony_ci printer.startEvent(eventName, undefined, message); 843af6ab5fSopenharmony_ci let content: string = fs.readFileSync(path, 'utf-8'); 853af6ab5fSopenharmony_ci const eventData = printer.getEventStack().get(eventName); 863af6ab5fSopenharmony_ci expect(eventData?.duration).to.equal(0); 873af6ab5fSopenharmony_ci expect(eventData?.endMemory).to.equal(0); 883af6ab5fSopenharmony_ci expect(eventData?.memoryUsage).to.equal(0); 893af6ab5fSopenharmony_ci assert.strictEqual(content, 'start Create AST event\n'); 903af6ab5fSopenharmony_ci fs.unlinkSync(path); 913af6ab5fSopenharmony_ci }); 923af6ab5fSopenharmony_ci }); 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ci describe('Tester Cases for <endEvent>.', function () { 953af6ab5fSopenharmony_ci /** test for endEvent */ 963af6ab5fSopenharmony_ci it('should throw an error if the event has not started', function () { 973af6ab5fSopenharmony_ci let eventName = ''; 983af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo1.txt'; 993af6ab5fSopenharmony_ci printer.setOutputPath(path); 1003af6ab5fSopenharmony_ci printer.startEvent('test event'); 1013af6ab5fSopenharmony_ci expect(() => printer.endEvent(eventName)).to.throw(`Event "${eventName}" not started`); 1023af6ab5fSopenharmony_ci }); 1033af6ab5fSopenharmony_ci 1043af6ab5fSopenharmony_ci it('should calculate duration and memory usage correctly', () => { 1053af6ab5fSopenharmony_ci const eventName = 'test Event'; 1063af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, 1073af6ab5fSopenharmony_ci { start: 0, duration: 0, startMemory: 0, endMemory: 0, memoryUsage: 0}); 1083af6ab5fSopenharmony_ci printer.endEvent(eventName); 1093af6ab5fSopenharmony_ci const eventData = printer.getEventStack().get(eventName); 1103af6ab5fSopenharmony_ci expect(eventData?.duration).to.not.equal(0); 1113af6ab5fSopenharmony_ci expect(eventData?.endMemory).to.not.equal(0); 1123af6ab5fSopenharmony_ci expect(eventData?.memoryUsage).to.not.equal(0); 1133af6ab5fSopenharmony_ci }); 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_ci it('should update filesTimeSum and maxTimeUsage when isFilesPrinter is true', () => { 1163af6ab5fSopenharmony_ci const eventName = 'file Event'; 1173af6ab5fSopenharmony_ci const startMemory = process.memoryUsage().heapUsed; 1183af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, 1193af6ab5fSopenharmony_ci { start: 0, duration: 0, startMemory: startMemory, endMemory: 0, memoryUsage: 0}); 1203af6ab5fSopenharmony_ci printer.endEvent(eventName, undefined, true); 1213af6ab5fSopenharmony_ci expect(printer.getFilesTimeSum()).to.not.equal(0); 1223af6ab5fSopenharmony_ci expect(printer.getMaxTimeUsage()).to.not.equal(0); 1233af6ab5fSopenharmony_ci expect(printer.getMaxTimeFile()).to.equal(eventName); 1243af6ab5fSopenharmony_ci }); 1253af6ab5fSopenharmony_ci 1263af6ab5fSopenharmony_ci it('should update maxMemoryUsage and maxMemoryFile when isFilesPrinter is true', () => { 1273af6ab5fSopenharmony_ci const eventName = 'file Event'; 1283af6ab5fSopenharmony_ci const startTime = Date.now(); 1293af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, 1303af6ab5fSopenharmony_ci { start: startTime, duration: 0, startMemory: 0, endMemory: 0, memoryUsage: 0}); 1313af6ab5fSopenharmony_ci printer.endEvent(eventName, undefined, true); 1323af6ab5fSopenharmony_ci expect(printer.getMaxMemoryUsage()).to.not.equal(0); 1333af6ab5fSopenharmony_ci expect(printer.getMaxMemoryFile()).to.equal(eventName); 1343af6ab5fSopenharmony_ci }); 1353af6ab5fSopenharmony_ci 1363af6ab5fSopenharmony_ci it('should output data and print max time and memory usage for ALL_FILES_OBFUSCATION', () => { 1373af6ab5fSopenharmony_ci const eventName = 'All files obfuscation'; 1383af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo1.txt'; 1393af6ab5fSopenharmony_ci printer.setOutputPath(path); 1403af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, { start: 0, duration: 0, startMemory: 0, endMemory: 0, memoryUsage: 0}); 1413af6ab5fSopenharmony_ci printer.endEvent(eventName); 1423af6ab5fSopenharmony_ci let content: string = fs.readFileSync(path, 'utf-8'); 1433af6ab5fSopenharmony_ci const lines = content.split('\n'); 1443af6ab5fSopenharmony_ci const firstLine = lines[0].split(':'); 1453af6ab5fSopenharmony_ci const secondLine = lines[2].split(':'); 1463af6ab5fSopenharmony_ci const thirdLine = lines[3].split(':'); 1473af6ab5fSopenharmony_ci assert.strictEqual(firstLine[0], 'All files obfuscation'); 1483af6ab5fSopenharmony_ci assert.strictEqual(secondLine[0], 'Max time cost'); 1493af6ab5fSopenharmony_ci assert.strictEqual(thirdLine[0], 'Max memory usage'); 1503af6ab5fSopenharmony_ci fs.unlinkSync(path); 1513af6ab5fSopenharmony_ci }); 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_ci it('should output data for CREATE_PRINTER', () => { 1543af6ab5fSopenharmony_ci const eventName = 'Create Printer'; 1553af6ab5fSopenharmony_ci const startMemory = process.memoryUsage().heapUsed; 1563af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo1.txt'; 1573af6ab5fSopenharmony_ci printer.setOutputPath(path); 1583af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, 1593af6ab5fSopenharmony_ci { start: Date.now(), duration: 0, startMemory: startMemory, endMemory: 0, memoryUsage: 0}); 1603af6ab5fSopenharmony_ci printer.endEvent(eventName); 1613af6ab5fSopenharmony_ci let content: string = fs.readFileSync(path, 'utf-8'); 1623af6ab5fSopenharmony_ci const firstLine = content.split(':'); 1633af6ab5fSopenharmony_ci assert.strictEqual(firstLine[0], ' Create Printer'); 1643af6ab5fSopenharmony_ci fs.unlinkSync(path); 1653af6ab5fSopenharmony_ci }); 1663af6ab5fSopenharmony_ci }); 1673af6ab5fSopenharmony_ci 1683af6ab5fSopenharmony_ci describe('Tester Cases for <getCurrentEventData>.', function () { 1693af6ab5fSopenharmony_ci it('should return a string with formatted event data', () => { 1703af6ab5fSopenharmony_ci const eventName = 'test event'; 1713af6ab5fSopenharmony_ci printer.getEventStack().set(eventName, 1723af6ab5fSopenharmony_ci { start: 0, duration: 10, startMemory: 1024, endMemory: 2048, memoryUsage: 1024}); 1733af6ab5fSopenharmony_ci const actualOutput: string = printer.getCurrentEventData(); 1743af6ab5fSopenharmony_ci const expectOutput: string = 'test event: timeCost:10.000s startMemory:0.001MB endMemory:0.002MB memoryUsage:0.001MB\n'; 1753af6ab5fSopenharmony_ci expect(actualOutput).to.equal(expectOutput); 1763af6ab5fSopenharmony_ci }); 1773af6ab5fSopenharmony_ci }); 1783af6ab5fSopenharmony_ci 1793af6ab5fSopenharmony_ci describe('Tester Cases for <getEventStack>', () => { 1803af6ab5fSopenharmony_ci 1813af6ab5fSopenharmony_ci it('should return the event stack', () => { 1823af6ab5fSopenharmony_ci printer.startEvent('test event'); 1833af6ab5fSopenharmony_ci const eventStack = printer.getEventStack(); 1843af6ab5fSopenharmony_ci expect(eventStack).to.have.keys(['test event']); 1853af6ab5fSopenharmony_ci expect(eventStack.get('test event')?.start).to.closeTo(Date.now(), 10); 1863af6ab5fSopenharmony_ci expect(eventStack.get('test event')?.duration).to.equal(0); 1873af6ab5fSopenharmony_ci expect(eventStack.get('test event')?.endMemory).to.equal(0); 1883af6ab5fSopenharmony_ci expect(eventStack.get('test event')?.memoryUsage).to.equal(0); 1893af6ab5fSopenharmony_ci }); 1903af6ab5fSopenharmony_ci }); 1913af6ab5fSopenharmony_ci }); 1923af6ab5fSopenharmony_ci 1933af6ab5fSopenharmony_ci 1943af6ab5fSopenharmony_ci describe('Tester Cases for <TimeSumPrinter>.', function () { 1953af6ab5fSopenharmony_ci let printer: TimeSumPrinter; 1963af6ab5fSopenharmony_ci 1973af6ab5fSopenharmony_ci beforeEach(() => { 1983af6ab5fSopenharmony_ci printer = new TimeSumPrinter(); 1993af6ab5fSopenharmony_ci }) 2003af6ab5fSopenharmony_ci 2013af6ab5fSopenharmony_ci describe('Tester Cases for <addEventDuration>.', function () { 2023af6ab5fSopenharmony_ci /** test for addEventDuration */ 2033af6ab5fSopenharmony_ci it('should add duration to the event sum', function () { 2043af6ab5fSopenharmony_ci printer.addEventDuration('test event1', 10); 2053af6ab5fSopenharmony_ci printer.addEventDuration('test event2', 20); 2063af6ab5fSopenharmony_ci printer.addEventDuration('test event1', 30); 2073af6ab5fSopenharmony_ci const event1Duration = printer.getEventSum().get('test event1'); 2083af6ab5fSopenharmony_ci const event2Duration = printer.getEventSum().get('test event2'); 2093af6ab5fSopenharmony_ci expect(event1Duration).to.equal(40); 2103af6ab5fSopenharmony_ci expect(event2Duration).to.equal(20); 2113af6ab5fSopenharmony_ci }); 2123af6ab5fSopenharmony_ci }); 2133af6ab5fSopenharmony_ci 2143af6ab5fSopenharmony_ci describe('Tester Cases for <summarizeEventDuration>.', function () { 2153af6ab5fSopenharmony_ci /** test for summarizeEventDuration */ 2163af6ab5fSopenharmony_ci it('should print the summarized event data', function () { 2173af6ab5fSopenharmony_ci let path = 'test/ut/utils/demo1.txt'; 2183af6ab5fSopenharmony_ci printer.setOutputPath(path); 2193af6ab5fSopenharmony_ci printer.addEventDuration('test event1', 10); 2203af6ab5fSopenharmony_ci printer.addEventDuration('test event2', 20); 2213af6ab5fSopenharmony_ci printer.addEventDuration('test event1', 30); 2223af6ab5fSopenharmony_ci printer.summarizeEventDuration(); 2233af6ab5fSopenharmony_ci let content: string = fs.readFileSync(path, 'utf-8'); 2243af6ab5fSopenharmony_ci const expectOutput = "test event1: 40.000s\ntest event2: 20.000s\n\n"; 2253af6ab5fSopenharmony_ci expect(content).to.equal(expectOutput); 2263af6ab5fSopenharmony_ci fs.unlinkSync(path); 2273af6ab5fSopenharmony_ci }); 2283af6ab5fSopenharmony_ci }); 2293af6ab5fSopenharmony_ci 2303af6ab5fSopenharmony_ci describe('Tester Cases for <getCurrentEventData>.', function () { 2313af6ab5fSopenharmony_ci it('should return a string with formatted event data', () => { 2323af6ab5fSopenharmony_ci const eventName = 'test event'; 2333af6ab5fSopenharmony_ci printer.getEventSum().set(eventName, 10); 2343af6ab5fSopenharmony_ci const actualOutput: string = printer.getCurrentEventData(); 2353af6ab5fSopenharmony_ci const expectOutput: string = 'test event: 10.000s\n'; 2363af6ab5fSopenharmony_ci expect(actualOutput).to.equal(expectOutput); 2373af6ab5fSopenharmony_ci }); 2383af6ab5fSopenharmony_ci }); 2393af6ab5fSopenharmony_ci 2403af6ab5fSopenharmony_ci describe('Tester Cases for <getEventSum>', () => { 2413af6ab5fSopenharmony_ci it('should return the event sum', () => { 2423af6ab5fSopenharmony_ci printer.addEventDuration('test event', 10); 2433af6ab5fSopenharmony_ci const eventSum = printer.getEventSum(); 2443af6ab5fSopenharmony_ci expect(eventSum).to.have.keys(['test event']); 2453af6ab5fSopenharmony_ci expect(eventSum.get('test event')).to.equal(10); 2463af6ab5fSopenharmony_ci }); 2473af6ab5fSopenharmony_ci }); 2483af6ab5fSopenharmony_ci }); 2493af6ab5fSopenharmony_ci}); 2503af6ab5fSopenharmony_ci 2513af6ab5fSopenharmony_ci 2523af6ab5fSopenharmony_ci 2533af6ab5fSopenharmony_ci 254