前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vscode源码分析【三】程序的启动逻辑,性能问题的追踪

vscode源码分析【三】程序的启动逻辑,性能问题的追踪

作者头像
liulun
发布2019-07-02 17:32:31
1.1K0
发布2019-07-02 17:32:31
举报
文章被收录于专栏:liulunliulun

启动追踪

代码文件:src\main.js 如果指定了特定的启动参数:trace vscode会在启动之初,执行下面的代码:

代码语言:javascript
复制
const contentTracing = require('electron').contentTracing;
		const traceOptions = {
			categoryFilter: args['trace-category-filter'] || '*',
			traceOptions: args['trace-options'] || 'record-until-full,enable-sampling'
		};
		contentTracing.startRecording(traceOptions, () => onReady());

这段代码的主要目的是:从Chromium的内容模块收集跟踪数据,以查找性能瓶颈和程序执行缓慢的操作。 注意,这个操作只能在app.ready事件触发之后才能执行; startRecoding会异步请求所有子进程开始执行追踪操作; 一旦所有子进程都确认了主进程的请求,主进程就会执行startRecoding的回调方法;

结束追踪

在窗口成功启动之后,vscode结束了性能问题的追踪(如果30秒窗口还没启动,那么也会结束性能问题的追踪) 代码文件:vs\code\electron-main\app.ts(在上一篇博文中,启动第一个窗口,也是在这里执行的)

代码语言:javascript
复制
const windows = appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor, electronIpcServer, sharedProcessClient));

stopTracingEventually方法的代码为:

代码语言:javascript
复制
	private stopTracingEventually(windows: ICodeWindow[]): void {
		this.logService.info(`Tracing: waiting for windows to get ready...`);

		let recordingStopped = false;
		const stopRecording = (timeout: boolean) => {
			if (recordingStopped) {
				return;
			}

			recordingStopped = true; // only once

			contentTracing.stopRecording(join(homedir(), `${product.applicationName}-${Math.random().toString(16).slice(-4)}.trace.txt`), path => {
				if (!timeout) {
					if (this.windowsMainService) {
						this.windowsMainService.showMessageBox({
							type: 'info',
							message: localize('trace.message', "Successfully created trace."),
							detail: localize('trace.detail', "Please create an issue and manually attach the following file:\n{0}", path),
							buttons: [localize('trace.ok', "Ok")]
						}, this.windowsMainService.getLastActiveWindow());
					}
				} else {
					this.logService.info(`Tracing: data recorded (after 30s timeout) to ${path}`);
				}
			});
		};

		// Wait up to 30s before creating the trace anyways
		const timeoutHandle = setTimeout(() => stopRecording(true), 30000);

		// Wait for all windows to get ready and stop tracing then
		Promise.all(windows.map(window => window.ready())).then(() => {
			clearTimeout(timeoutHandle);
			stopRecording(false);
		});
	}

子进程会缓存跟踪数据,一般不会把跟踪数据发送给主进程(避免发送数据再造成性能消耗), 所以,结束跟踪也是主进程异步地要求所有子进程持久化跟踪数据的。 跟踪结束后,会执行stopRecording的回调函数。 在这里会显示一个提示框,提示用户性能追踪的结果;(如果超了30秒,那么就只记日志了)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 启动追踪
  • 结束追踪
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档