From 54a5444dc60d9b7308e326746b70127582fc5ec6 Mon Sep 17 00:00:00 2001 From: wuhailong Date: Mon, 16 Oct 2023 14:21:53 +0800 Subject: [PATCH] SDK performance instrumentation Signed-off-by: wuhailong Change-Id: I1bd5d6f593f4e8e60b7868948ac6a7232b9f68aa --- compiler/src/ets_checker.ts | 15 +++++-- .../ark_compiler/generate_module_abc.ts | 22 ++++++---- .../ark_compiler/module/module_build_mode.ts | 12 +++--- .../ark_compiler/module/module_hotfix_mode.ts | 8 ++-- .../module/module_hotreload_mode.ts | 33 +++++++++------ .../ark_compiler/module/module_mode.ts | 22 ++++++++-- .../module/module_preview_mode.ts | 12 +++--- .../ark_compiler/module/module_source_file.ts | 41 +++++++++++++++---- .../src/fast_build/ark_compiler/transform.ts | 24 ++++++++--- compiler/src/fast_build/ark_compiler/utils.ts | 8 +++- .../ets_ui/rollup-plugin-ets-checker.ts | 14 +++++-- .../ets_ui/rollup-plugin-ets-typescript.ts | 8 +++- compiler/src/process_module_files.ts | 10 ++++- compiler/src/process_ui_syntax.ts | 6 +-- compiler/src/utils.ts | 30 +++++++++++++- 15 files changed, 195 insertions(+), 70 deletions(-) diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index 8e043330f..1c220d093 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -69,7 +69,12 @@ import { props, logger } from './compile_info'; -import { hasDecorator, isString } from './utils'; +import { + hasDecorator, + isString, + createAndStartEvent, + stopEvent +} from './utils'; import { generateSourceFilesInHar } from './utils'; import { isExtendFunction, isOriginalExtend } from './process_ui_syntax'; import { visualTransform } from './process_visual'; @@ -283,7 +288,7 @@ export let fastBuildLogger = null; export const checkerResult: CheckerResult = {count: 0}; export const warnCheckerResult: WarnCheckerResult = {count: 0}; export let languageService: ts.LanguageService = null; -export function serviceChecker(rootFileNames: string[], newLogger: any = null, resolveModulePaths: string[] = null): void { +export function serviceChecker(rootFileNames: string[], newLogger: any = null, resolveModulePaths: string[] = null, parentEvent: any): void { fastBuildLogger = newLogger; let cacheFile: string = null; if (projectConfig.xtsMode || process.env.watchMode === 'true') { @@ -307,7 +312,7 @@ export function serviceChecker(rootFileNames: string[], newLogger: any = null, r languageService = createLanguageService(filterFiles, resolveModulePaths); } globalProgram.program = languageService.getProgram(); - runArkTSLinter(); + runArkTSLinter(parentEvent); collectSourceFilesMap(globalProgram.program); if (process.env.watchMode !== 'true') { processBuildHap(cacheFile, rootFileNames); @@ -957,7 +962,8 @@ export function incrementWatchFile(watchModifiedFiles: string[], }); } -function runArkTSLinter(): void { +function runArkTSLinter(parentEvent?: any): void { + const eventRunArkTsLinter = createAndStartEvent(parentEvent, 'run Arkts linter'); const arkTSLinterDiagnostics = doArkTSLinter(globalProgram.program, getArkTSLinterMode(), printArkTSLinterDiagnostic, !projectConfig.xtsMode); if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) { @@ -965,6 +971,7 @@ function runArkTSLinter(): void { updateErrorFileCache(diagnostic); }); } + stopEvent(eventRunArkTsLinter); } function printArkTSLinterDiagnostic(diagnostic: ts.Diagnostic): void { diff --git a/compiler/src/fast_build/ark_compiler/generate_module_abc.ts b/compiler/src/fast_build/ark_compiler/generate_module_abc.ts index 52177a0f7..f6cb44abd 100644 --- a/compiler/src/fast_build/ark_compiler/generate_module_abc.ts +++ b/compiler/src/fast_build/ark_compiler/generate_module_abc.ts @@ -19,35 +19,43 @@ import { ModuleHotfixMode } from './module/module_hotfix_mode'; import { ModuleHotreloadMode } from './module/module_hotreload_mode'; import { ModulePreviewMode } from './module/module_preview_mode'; import { ModuleSourceFile } from './module/module_source_file'; +import { + getHookEventFactory, + createAndStartEvent, + stopEvent +} from '../../utils'; export async function generateModuleAbc(error) { + const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'buildEnd'); if (error) { // When error thrown in previous plugins, rollup will catch and call buildEnd plugin. // Stop generate abc if error exists return; } if (this.share.projectConfig.compileMode === ESMODULE) { - await ModuleSourceFile.processModuleSourceFiles(this); + await ModuleSourceFile.processModuleSourceFiles(this, hookEventFactory); if (this.share.projectConfig.compileHar) { // compileHar: compile closed source har of project, which convert .ets to .d.ts and js, doesn't emit abc. return; } - generateAbc(this); + generateAbc(this, hookEventFactory); } } -function generateAbc(rollupObject: any) { +function generateAbc(rollupObject: any, parentEvent: any) { + const eventGenerateAbc = createAndStartEvent(parentEvent, 'generate abc'); if (rollupObject.share.projectConfig.watchMode !== 'true') { const moduleBuildMode: ModuleBuildMode = new ModuleBuildMode(rollupObject); - moduleBuildMode.generateAbc(rollupObject); + moduleBuildMode.generateAbc(rollupObject, eventGenerateAbc); } else if (rollupObject.share.arkProjectConfig.hotReload) { const moduleHotreloadMode: ModuleHotreloadMode = new ModuleHotreloadMode(rollupObject); - moduleHotreloadMode.generateAbc(rollupObject); + moduleHotreloadMode.generateAbc(rollupObject, eventGenerateAbc); } else if (rollupObject.share.arkProjectConfig.hotFix) { const moduleHotfixMode: ModuleHotfixMode = new ModuleHotfixMode(rollupObject); - moduleHotfixMode.generateAbc(rollupObject); + moduleHotfixMode.generateAbc(rollupObject, eventGenerateAbc); } else { const modulePreviewMode: ModulePreviewMode = new ModulePreviewMode(rollupObject); - modulePreviewMode.generateAbc(rollupObject); + modulePreviewMode.generateAbc(rollupObject, eventGenerateAbc); } + stopEvent(eventGenerateAbc); } diff --git a/compiler/src/fast_build/ark_compiler/module/module_build_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_build_mode.ts index e28e4c423..3a8d9e027 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_build_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_build_mode.ts @@ -22,17 +22,17 @@ export class ModuleBuildMode extends ModuleMode { super(rollupObject); } - generateAbc(rollupObject: any) { - this.prepareForCompilation(rollupObject); - this.buildModuleSourceMapInfo(); - this.executeArkCompiler(); + generateAbc(rollupObject: any, parentEvent: any) { + this.prepareForCompilation(rollupObject, parentEvent); + this.buildModuleSourceMapInfo(parentEvent); + this.executeArkCompiler(parentEvent); } - executeArkCompiler() { + executeArkCompiler(parentEvent: any) { if (isEs2Abc(this.projectConfig)) { this.generateEs2AbcCmd(); this.addCacheFileArgs(); - this.generateMergedAbcOfEs2Abc(); + this.generateMergedAbcOfEs2Abc(parentEvent); } else if (isTs2Abc(this.projectConfig)) { this.filterModulesByHashJson(); const splittedModules: any[] = this.getSplittedModulesByNumber(); diff --git a/compiler/src/fast_build/ark_compiler/module/module_hotfix_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_hotfix_mode.ts index 7504a77e1..6a0663685 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_hotfix_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_hotfix_mode.ts @@ -31,15 +31,15 @@ export class ModuleHotfixMode extends ModuleMode { this.enableMap = false; } - generateAbc(rollupObject: any) { + generateAbc(rollupObject: any, parentEvent: any) { this.patch = this.projectConfig.patch || false; this.inOldSymbolTablePath = this.projectConfig.inOldSymbolTablePath || this.projectConfig.projectRootPath; this.enableMap = this.projectConfig.enableMap || false; - this.prepareForCompilation(rollupObject); - this.buildModuleSourceMapInfo(); + this.prepareForCompilation(rollupObject, parentEvent); + this.buildModuleSourceMapInfo(parentEvent); this.generateEs2AbcCmdForHotfix(); - this.generateMergedAbcOfEs2Abc(); + this.generateMergedAbcOfEs2Abc(parentEvent); } private generateEs2AbcCmdForHotfix() { diff --git a/compiler/src/fast_build/ark_compiler/module/module_hotreload_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_hotreload_mode.ts index c5233ead8..80ee26015 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_hotreload_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_hotreload_mode.ts @@ -28,6 +28,8 @@ import { isJsonSourceFile } from '../utils'; import { newSourceMaps } from '../transform'; import { mkdirsSync, + createAndStartEvent, + stopEvent, toUnixPath, validateFilePathLength } from '../../../utils'; @@ -46,12 +48,12 @@ export class ModuleHotreloadMode extends ModuleMode { } } - generateAbc(rollupObject: any) { + generateAbc(rollupObject: any, parentEvent: any) { if (isFirstBuild) { - this.compileAllFiles(rollupObject); + this.compileAllFiles(rollupObject, parentEvent); isFirstBuild = false; } else { - this.compileChangeListFiles(rollupObject); + this.compileChangeListFiles(rollupObject, parentEvent); } } @@ -67,13 +69,13 @@ export class ModuleHotreloadMode extends ModuleMode { this.cmdArgs.push('--hot-reload'); } - private compileAllFiles(rollupObject: any) { - this.prepareForCompilation(rollupObject); - this.buildModuleSourceMapInfo(); - this.generateAbcByEs2abc(); + private compileAllFiles(rollupObject: any, parentEvent: any) { + this.prepareForCompilation(rollupObject, parentEvent); + this.buildModuleSourceMapInfo(parentEvent); + this.generateAbcByEs2abc(parentEvent); } - private compileChangeListFiles(rollupObject: any) { + private compileChangeListFiles(rollupObject: any, parentEvent: any) { if (!fs.existsSync(this.projectConfig.changedFileList)) { this.logger.debug(blue, `ArkTS: Cannot find file: ${ this.projectConfig.changedFileList}, skip hot reload build`, reset); @@ -100,20 +102,24 @@ export class ModuleHotreloadMode extends ModuleMode { return; } + const eventCollectModuleFileList = createAndStartEvent(parentEvent, 'collect module file list'); this.collectModuleFileList(rollupObject, changedFileListInAbsolutePath[Symbol.iterator]()); + stopEvent(eventCollectModuleFileList); if (!fs.existsSync(this.projectConfig.patchAbcPath)) { mkdirsSync(this.projectConfig.patchAbcPath); } - this.updateSourceMapFromFileList(changedFileList); + + this.updateSourceMapFromFileList(changedFileList, parentEvent); const outputABCPath: string = path.join(this.projectConfig.patchAbcPath, MODULES_ABC); validateFilePathLength(outputABCPath, this.logger); this.moduleAbcPath = outputABCPath; - this.generateAbcByEs2abc(); + this.generateAbcByEs2abc(parentEvent); } - private updateSourceMapFromFileList(fileList: Array) { + private updateSourceMapFromFileList(fileList: Array, parentEvent: any) { + const eventUpdateSourceMapFromFileList = createAndStartEvent(parentEvent, 'update source map from file list'); const relativeProjectPath: string = this.projectConfig.projectPath.slice( this.projectConfig.projectRootPath.length + path.sep.length); for (const file of fileList) { @@ -126,11 +132,12 @@ export class ModuleHotreloadMode extends ModuleMode { validateFilePathLength(sourceMapFilePath, this.logger); fs.writeFileSync(sourceMapFilePath, JSON.stringify(hotReloadSourceMap, null, 2), 'utf-8'); + stopEvent(eventUpdateSourceMapFromFileList); } - private generateAbcByEs2abc() { + private generateAbcByEs2abc(parentEvent: any) { this.generateEs2AbcCmd(); this.addHotReloadArgs(); - this.generateMergedAbcOfEs2Abc(); + this.generateMergedAbcOfEs2Abc(parentEvent); } } diff --git a/compiler/src/fast_build/ark_compiler/module/module_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_mode.ts index 7b3da55cc..b0bf95947 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_mode.ts @@ -69,7 +69,9 @@ import { mkdirsSync, toUnixPath, toHashData, - validateFilePathLength + validateFilePathLength, + createAndStartEvent, + stopEvent } from '../../../utils'; import { getPackageInfo, @@ -151,9 +153,11 @@ export class ModuleMode extends CommonMode { this.symlinkMap = rollupObject.share.symlinkMap; } - prepareForCompilation(rollupObject: any): void { + prepareForCompilation(rollupObject: any, parentEvent: any): void { + const eventPrepareForCompilation = createAndStartEvent(parentEvent, 'preparation for compilation'); this.collectModuleFileList(rollupObject, rollupObject.getModuleIds()); this.removeCacheInfo(rollupObject); + stopEvent(eventPrepareForCompilation); } collectModuleFileList(module: any, fileList: IterableIterator) { @@ -307,18 +311,22 @@ export class ModuleMode extends CommonMode { }); } - buildModuleSourceMapInfo() { + buildModuleSourceMapInfo(parentEvent: any) { if (this.projectConfig.widgetCompile) { return; } + const eventUpdateCachedSourceMaps = createAndStartEvent(parentEvent, 'update cached source maps'); this.updateCachedSourceMaps(); + stopEvent(eventUpdateCachedSourceMaps); this.triggerAsync(() => { + const eventWriteFile = createAndStartEvent(parentEvent, 'write source map (async)', true); fs.writeFile(this.sourceMapPath, JSON.stringify(this.cacheSourceMapObject, null, 2), 'utf-8', (err) => { if (err) { this.throwArkTsCompilerError('ArkTS:ERROR failed to write sourceMaps'); } fs.copyFileSync(this.sourceMapPath, this.cacheSourceMapPath); + stopEvent(eventWriteFile, true); this.triggerEndSignal(); }); }); @@ -383,24 +391,30 @@ export class ModuleMode extends CommonMode { this.generateAbcCacheFilesInfo(); } - generateMergedAbcOfEs2Abc() { + generateMergedAbcOfEs2Abc(parentEvent: any) { // collect data error from subprocess let errMsg: string = ''; + const eventGenDescriptionsForMergedEs2abc = createAndStartEvent(parentEvent, 'generate descriptions for merged es2abc'); this.genDescriptionsForMergedEs2abc(); + stopEvent(eventGenDescriptionsForMergedEs2abc); const genAbcCmd: string = this.cmdArgs.join(' '); try { + let eventGenAbc: any; const child = this.triggerAsync(() => { + eventGenAbc = createAndStartEvent(parentEvent, 'generate merged abc by es2abc (async)', true); return childProcess.exec(genAbcCmd, { windowsHide: true }); }); child.on('exit', (code: any) => { if (code === FAIL) { this.throwArkTsCompilerError('ArkTS:ERROR failed to execute es2abc'); } + stopEvent(eventGenAbc, true); this.triggerEndSignal(); this.processAotIfNeeded(); }); child.on('error', (err: any) => { + stopEvent(eventGenAbc,true); this.throwArkTsCompilerError(err.toString()); }); diff --git a/compiler/src/fast_build/ark_compiler/module/module_preview_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_preview_mode.ts index b62c90dd7..889e8d9e4 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_preview_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_preview_mode.ts @@ -22,17 +22,17 @@ export class ModulePreviewMode extends ModuleMode { super(rollupObject); } - generateAbc(rollupObject: any) { - this.prepareForCompilation(rollupObject); - this.buildModuleSourceMapInfo(); - this.executeArkCompiler(); + generateAbc(rollupObject: any, parentEvent: any) { + this.prepareForCompilation(rollupObject, parentEvent); + this.buildModuleSourceMapInfo(parentEvent); + this.executeArkCompiler(parentEvent); } - executeArkCompiler() { + executeArkCompiler(parentEvent: any) { if (isEs2Abc(this.projectConfig)) { this.generateEs2AbcCmd(); this.addCacheFileArgs(); - this.generateMergedAbcOfEs2Abc(); + this.generateMergedAbcOfEs2Abc(parentEvent); } else if (isTs2Abc(this.projectConfig)) { this.filterModulesByHashJson(); const splittedModules: any[] = this.getSplittedModulesByNumber(); diff --git a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts index b210b723e..c35d8bc25 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts @@ -34,7 +34,11 @@ import { updateSourceMap, writeFileContentToTempDir } from '../utils'; -import { toUnixPath } from '../../../utils'; +import { + toUnixPath, + createAndStartEvent, + stopEvent +} from '../../../utils'; import { newSourceMaps } from '../transform'; import { getArkguardNameCache, writeObfuscationNameCache } from '../common/ob_config_resolver'; import { ORIGIN_EXTENTION } from '../process_mock'; @@ -171,7 +175,7 @@ export class ModuleSourceFile { return ModuleSourceFile.sourceFiles; } - static async processModuleSourceFiles(rollupObject: any) { + static async processModuleSourceFiles(rollupObject: any, parentEvent: any) { this.initPluginEnv(rollupObject); // collect mockConfigInfo @@ -183,20 +187,29 @@ export class ModuleSourceFile { for (const source of ModuleSourceFile.sourceFiles) { if (!rollupObject.share.projectConfig.compileHar) { // compileHar: compile closed source har of project, which convert .ets to .d.ts and js, doesn't transform module request. - await source.processModuleRequest(rollupObject); + const eventBuildModuleSourceFile = createAndStartEvent(parentEvent, 'build module source files'); + await source.processModuleRequest(rollupObject, eventBuildModuleSourceFile); + stopEvent(eventBuildModuleSourceFile); } - await source.writeSourceFile(); + const eventWriteSourceFile = createAndStartEvent(parentEvent, 'write source file'); + await source.writeSourceFile(eventWriteSourceFile); + stopEvent(eventWriteSourceFile); } + + const eventObfuscatedCode = createAndStartEvent(parentEvent, 'write obfuscation name cache'); if ((ModuleSourceFile.projectConfig.arkObfuscator || ModuleSourceFile.projectConfig.terserConfig) && ModuleSourceFile.projectConfig.obfuscationOptions) { writeObfuscationNameCache(ModuleSourceFile.projectConfig, ModuleSourceFile.projectConfig.obfuscationOptions.obfuscationCacheDir, ModuleSourceFile.projectConfig.obfuscationMergedObConfig.options?.printNameCache); } + stopEvent(eventObfuscatedCode); + const eventGenerateMockConfigFile = createAndStartEvent(parentEvent, 'generate mock config file'); if (ModuleSourceFile.needProcessMock) { ModuleSourceFile.generateMockConfigFile(rollupObject); } + stopEvent(eventGenerateMockConfigFile); ModuleSourceFile.sourceFiles = []; } @@ -205,11 +218,11 @@ export class ModuleSourceFile { return this.moduleId; } - private async writeSourceFile() { + private async writeSourceFile(parentEvent: any) { if (this.isSourceNode && !isJsSourceFile(this.moduleId)) { - await writeFileSyncByNode(this.source, ModuleSourceFile.projectConfig, ModuleSourceFile.logger); + await writeFileSyncByNode(this.source, ModuleSourceFile.projectConfig, parentEvent, ModuleSourceFile.logger); } else { - await writeFileContentToTempDir(this.moduleId, this.source, ModuleSourceFile.projectConfig, ModuleSourceFile.logger); + await writeFileContentToTempDir(this.moduleId, this.source, ModuleSourceFile.projectConfig, ModuleSourceFile.logger, parentEvent); } } @@ -372,19 +385,29 @@ export class ModuleSourceFile { // Replace each module request in source file to a unique representation which is called 'ohmUrl'. // This 'ohmUrl' will be the same as the record name for each file, to make sure runtime can find the corresponding // record based on each module request. - async processModuleRequest(rollupObject: any) { + async processModuleRequest(rollupObject: any, parentEvent: any) { if (isJsonSourceFile(this.moduleId)) { return; } if (isJsSourceFile(this.moduleId)) { + const eventProcessJsModuleRequest = createAndStartEvent(parentEvent, 'process Js module request'); this.processJsModuleRequest(rollupObject); + stopEvent(eventProcessJsModuleRequest); return; } + // Only when files were transformed to ts, the corresponding ModuleSourceFile were initialized with sourceFile node, // if files were transformed to js, ModuleSourceFile were initialized with srouce string. - this.isSourceNode ? this.processTransformedTsModuleRequest(rollupObject) : + if (this.isSourceNode) { + const eventProcessTransformedTsModuleRequest = createAndStartEvent(parentEvent, 'process transformed Ts module request'); + this.processTransformedTsModuleRequest(rollupObject); + stopEvent(eventProcessTransformedTsModuleRequest); + } else { + const eventProcessTransformedJsModuleRequest = createAndStartEvent(parentEvent, 'process transformed Js module request'); await this.processTransformedJsModuleRequest(rollupObject); + stopEvent(eventProcessTransformedJsModuleRequest); + } } private static initPluginEnv(rollupObject: any) { diff --git a/compiler/src/fast_build/ark_compiler/transform.ts b/compiler/src/fast_build/ark_compiler/transform.ts index 4c18557e5..41f53ae92 100644 --- a/compiler/src/fast_build/ark_compiler/transform.ts +++ b/compiler/src/fast_build/ark_compiler/transform.ts @@ -28,7 +28,12 @@ import { isTsOrEtsSourceFile, shouldETSOrTSFileTransformToJS } from './utils'; -import { toUnixPath } from '../../utils'; +import { + toUnixPath, + getHookEventFactory, + createAndStartEvent, + stopEvent +} from '../../utils'; export let newSourceMaps: Object = {}; @@ -38,39 +43,45 @@ export let newSourceMaps: Object = {}; * @param {string} id: absolute path of an input file */ export function transformForModule(code: string, id: string) { + const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'transform'); + const eventTransformForModule = createAndStartEvent(hookEventFactory, 'transform for module'); if (this.share.projectConfig.compileMode === ESMODULE) { const projectConfig: any = Object.assign(this.share.arkProjectConfig, this.share.projectConfig); if (isTsOrEtsSourceFile(id) && shouldETSOrTSFileTransformToJS(id, projectConfig)) { - preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig); + preserveSourceMap(id, this.getCombinedSourcemap(), projectConfig, eventTransformForModule); ModuleSourceFile.newSourceFile(id, code); } if (isJsSourceFile(id) || isJsonSourceFile(id)) { let code: string = this.getModuleInfo(id).originalCode; if (isJsSourceFile(id)) { - const transformedResult: any = transformJsByBabelPlugin(code); + const transformedResult: any = transformJsByBabelPlugin(code, eventTransformForModule); code = transformedResult.code; - preserveSourceMap(id, transformedResult.map, projectConfig); + preserveSourceMap(id, transformedResult.map, projectConfig, eventTransformForModule); } ModuleSourceFile.newSourceFile(id, code); } } + stopEvent(eventTransformForModule); } -function preserveSourceMap(sourceFilePath: string, sourcemap: any, projectConfig: any): void { +function preserveSourceMap(sourceFilePath: string, sourcemap: any, projectConfig: any, parentEvent: any): void { if (isCommonJsPluginVirtualFile(sourceFilePath)) { // skip automatic generated files like 'jsfile.js?commonjs-exports' return; } + const eventAddSourceMapInfo = createAndStartEvent(parentEvent, 'preserve source map for ts/ets files'); const relativeSourceFilePath = toUnixPath(sourceFilePath.replace(projectConfig.projectRootPath + path.sep, '')); sourcemap['sources'] = [ relativeSourceFilePath ]; sourcemap['file'] = path.basename(relativeSourceFilePath); sourcemap.sourcesContent && delete sourcemap.sourcesContent; newSourceMaps[relativeSourceFilePath] = sourcemap; + stopEvent(eventAddSourceMapInfo); } -function transformJsByBabelPlugin(code: string): any { +function transformJsByBabelPlugin(code: string, parentEvent: any): any { + const eventTransformByBabel = createAndStartEvent(parentEvent, 'transform Js by babel plugin'); const transformed: any = require('@babel/core').transformSync(code, { plugins: [ @@ -80,5 +91,6 @@ function transformJsByBabelPlugin(code: string): any { sourceMaps: true } ); + stopEvent(eventTransformByBabel); return transformed; } diff --git a/compiler/src/fast_build/ark_compiler/utils.ts b/compiler/src/fast_build/ark_compiler/utils.ts index 296f5601b..f078f48fb 100644 --- a/compiler/src/fast_build/ark_compiler/utils.ts +++ b/compiler/src/fast_build/ark_compiler/utils.ts @@ -35,7 +35,9 @@ import { genTemporaryPath, mkdirsSync, validateFilePathLength, - toUnixPath + toUnixPath, + createAndStartEvent, + stopEvent } from '../../utils'; import { writeObfuscatedSourceCode @@ -96,7 +98,7 @@ export function shouldETSOrTSFileTransformToJS(filePath: string, projectConfig: return fs.existsSync(cacheFilePath); } -export async function writeFileContentToTempDir(id: string, content: string, projectConfig: any, logger: any) { +export async function writeFileContentToTempDir(id: string, content: string, projectConfig: any, logger: any, parentEvent: any) { if (isCommonJsPluginVirtualFile(id)) { return; } @@ -116,6 +118,7 @@ export async function writeFileContentToTempDir(id: string, content: string, pro filePath = genTemporaryPath(id, projectConfig.projectPath, projectConfig.cachePath, projectConfig); } + const eventWriteFileContent = createAndStartEvent(parentEvent, 'write file content'); switch (path.extname(id)) { case EXTNAME_ETS: case EXTNAME_TS: @@ -131,6 +134,7 @@ export async function writeFileContentToTempDir(id: string, content: string, pro default: break; } + stopEvent(eventWriteFileContent); } async function writeFileContent(sourceFilePath: string, filePath: string, content: string, projectConfig: any, logger: any) { diff --git a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts index c6d38f8b7..1947ff123 100644 --- a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts +++ b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-checker.ts @@ -28,7 +28,12 @@ import { fastBuildLogger } from '../../ets_checker'; import { TS_WATCH_END_MSG } from '../../pre_define'; -import { setChecker } from '../../utils'; +import { + setChecker, + getHookEventFactory, + createAndStartEvent, + stopEvent +} from '../../utils'; export let tsWatchEmitter: EventEmitter | undefined = undefined; export let tsWatchEndPromise: Promise; @@ -38,6 +43,7 @@ export function etsChecker() { return { name: 'etsChecker', buildStart() { + const hookEventFactory = getHookEventFactory(this.share, 'etsChecker', 'buildStart'); if (process.env.watchMode === 'true' && process.env.triggerTsWatch === 'true') { tsWatchEmitter = new EventEmitter(); tsWatchEndPromise = new Promise(resolve => { @@ -64,8 +70,9 @@ export function etsChecker() { Array.isArray(this.share.projectConfig.resolveModulePaths)) { resolveModulePaths.push(...this.share.projectConfig.resolveModulePaths); } + const eventServiceChecker = createAndStartEvent(hookEventFactory, 'check Ets code syntax'); if (process.env.watchMode === 'true') { - !executedOnce && serviceChecker(rootFileNames, logger, resolveModulePaths); + !executedOnce && serviceChecker(rootFileNames, logger, resolveModulePaths, eventServiceChecker); executedOnce = true; globalProgram.program = languageService.getProgram(); const allDiagnostics: ts.Diagnostic[] = globalProgram.program @@ -78,8 +85,9 @@ export function etsChecker() { fastBuildLogger.debug(TS_WATCH_END_MSG); tsWatchEmitter.emit(TS_WATCH_END_MSG); } else { - serviceChecker(rootFileNames, logger, resolveModulePaths); + serviceChecker(rootFileNames, logger, resolveModulePaths, eventServiceChecker); } + stopEvent(eventServiceChecker); setChecker(); } }; diff --git a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts index f59ee2aaf..fdc99c427 100644 --- a/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts +++ b/compiler/src/fast_build/ets_ui/rollup-plugin-ets-typescript.ts @@ -32,6 +32,9 @@ import { fileInfo, resourcesRawfile, differenceResourcesRawfile, + getHookEventFactory, + createAndStartEvent, + stopEvent, CacheFile } from '../../utils'; import { @@ -227,6 +230,7 @@ async function transform(code: string, id: string) { return null; } + const hookEventFactory = getHookEventFactory(this.share, 'etsTransform', 'transform'); storedFileInfo.collectTransformedFiles(path.resolve(id)); const logger = this.share.getLogger('etsTransform'); @@ -279,6 +283,7 @@ async function transform(code: string, id: string) { validateEts(code, id, this.getModuleInfo(id).isEntry, logger, targetSourceFile); + const eventSetEmit = createAndStartEvent(hookEventFactory, 'emit UI transformed file'); const emitResult: EmitResult = { outputText: '', sourceMapText: '' }; const writeFile: ts.WriteFileCallback = (fileName: string, data: string) => { if (/.map$/.test(fileName)) { @@ -292,7 +297,7 @@ async function transform(code: string, id: string) { tsProgram.getCompilerOptions().noEmit = false; // use `try finally` to restore `noEmit` when error thrown by `processUISyntax` in preview mode try { - tsProgram.emit(targetSourceFile, writeFile, undefined, undefined, { before: [ processUISyntax(null) ] }); + tsProgram.emit(targetSourceFile, writeFile, undefined, undefined, { before: [ processUISyntax(null, false, eventSetEmit) ] }); } finally { // restore `noEmit` to prevent tsc's watchService emitting automatically. tsProgram.getCompilerOptions().noEmit = true; @@ -303,6 +308,7 @@ async function transform(code: string, id: string) { emitLogInfo(logger, getTransformLog(transformLog), true, id); resetLog(); } + stopEvent(eventSetEmit); return { code: emitResult.outputText, diff --git a/compiler/src/process_module_files.ts b/compiler/src/process_module_files.ts index 4048d07ef..f2c318ff7 100644 --- a/compiler/src/process_module_files.ts +++ b/compiler/src/process_module_files.ts @@ -26,6 +26,8 @@ import { genTemporaryPath, mkdirsSync, toUnixPath, + createAndStartEvent, + stopEvent } from './utils'; import { genSourceMapFileName, @@ -38,8 +40,11 @@ import { isAotMode, isDebug } from './fast_build/ark_compiler/utils'; export const SRC_MAIN: string = 'src/main'; -export async function writeFileSyncByNode(node: ts.SourceFile, projectConfig: any, logger?: any): Promise { +export async function writeFileSyncByNode(node: ts.SourceFile, projectConfig: any, parentEvent: any, logger?: any): Promise { + const eventWriteFileSyncByNode = createAndStartEvent(parentEvent, 'write file sync by node'); + const eventGenContentAndSourceMapInfo = createAndStartEvent(eventWriteFileSyncByNode, 'generate content and source map information'); const mixedInfo: {content: string, sourceMapJson: any} = genContentAndSourceMapInfo(node, projectConfig); + stopEvent(eventGenContentAndSourceMapInfo); let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, process.env.cachePath, projectConfig); if (temporaryFile.length === 0) { @@ -61,10 +66,13 @@ export async function writeFileSyncByNode(node: ts.SourceFile, projectConfig: an sourceMaps = webpackNewSourceMaps; } if (projectConfig.compileHar || (!isDebug(projectConfig) && isAotMode(projectConfig))) { + const eventWriteObfuscatedSourceCode = createAndStartEvent(eventWriteFileSyncByNode, 'write obfuscated source code'); await writeObfuscatedSourceCode(mixedInfo.content, temporaryFile, logger, projectConfig, relativeSourceFilePath, sourceMaps); + stopEvent(eventWriteObfuscatedSourceCode); return; } fs.writeFileSync(temporaryFile, mixedInfo.content); + stopEvent(eventWriteFileSyncByNode); } function genContentAndSourceMapInfo(node: ts.SourceFile, projectConfig: any): any { diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 9ac8f6fb0..efc689b33 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -135,7 +135,7 @@ export const builderTypeParameter: {params: string[]} = {params: []}; export let hasTsNoCheckOrTsIgnoreFiles: string[] = []; export let compilingEtsOrTsFiles: string[] = []; -export function processUISyntax(program: ts.Program, ut = false): Function { +export function processUISyntax(program: ts.Program, ut = false, parentEvent?: any): Function { let entryNodeKey: ts.Expression; return (context: ts.TransformationContext) => { contextGlobal = context; @@ -163,7 +163,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { hasTsNoCheckOrTsIgnore ? hasTsNoCheckOrTsIgnoreFiles.push(path.normalize(processedNode.fileName)) : ModuleSourceFile.newSourceFile(path.normalize(processedNode.fileName), processedNode); } else { - writeFileSyncByNode(processedNode, projectConfig); + writeFileSyncByNode(processedNode, projectConfig, parentEvent); } } } @@ -191,7 +191,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { hasTsNoCheckOrTsIgnore ? hasTsNoCheckOrTsIgnoreFiles.push(path.normalize(processedNode.fileName)) : ModuleSourceFile.newSourceFile(path.normalize(processedNode.fileName), processedNode); } else { - writeFileSyncByNode(processedNode, projectConfig); + writeFileSyncByNode(processedNode, projectConfig, parentEvent); } } return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index cf6feeb75..7417ee484 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -912,4 +912,32 @@ export function differenceResourcesRawfile(oldRawfile: Set, newRawfile: export function isString(text: unknown): text is string { return typeof text === 'string'; -} \ No newline at end of file +} + +export function getHookEventFactory(share: any, pluginName: string, hookName: string): any { + if (typeof share.getHookEventFactory === 'function') { + return share.getHookEventFactory(pluginName, hookName); + } else { + return undefined; + } +} + +export function createAndStartEvent(eventOrEventFactory: any, eventName: string, syncFlag = false): any { + if (eventOrEventFactory === undefined) { + return undefined; + } + let event: any; + if (typeof eventOrEventFactory.createSubEvent === 'function') { + event = eventOrEventFactory.createSubEvent(eventName); + } else { + event = eventOrEventFactory.createEvent(eventName); + } + syncFlag ? event.startAsyncEvent() : event.start(); + return event; +} + +export function stopEvent(event:any, syncFlag = false): void { + if (event !== undefined) { + syncFlag ? event.stopAsyncEvent() : event.stop(); + } +} -- Gitee