performance.contribution.ts 4.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import { IEnvironmentService } from 'vs/platform/environment/common/environment';
9 10
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
11
import { IMessageService } from 'vs/platform/message/common/message';
12
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
13
import { IWindowsService } from 'vs/platform/windows/common/windows';
14
import { IWorkbenchContributionsRegistry, IWorkbenchContribution, Extensions } from 'vs/workbench/common/contributions';
15
import { Registry } from 'vs/platform/registry/common/platform';
16 17 18 19
import { ReportPerformanceIssueAction } from 'vs/workbench/electron-browser/actions';
import { TPromise } from 'vs/base/common/winjs.base';
import { join } from 'path';
import { localize } from 'vs/nls';
J
Johannes Rieken 已提交
20
import { readdir } from 'vs/base/node/pfs';
21
import { stopProfiling } from 'vs/base/node/profiler';
22

23 24 25 26 27 28 29
class StartupProfiler implements IWorkbenchContribution {

	constructor(
		@IWindowsService private readonly _windowsService: IWindowsService,
		@IMessageService private readonly _messageService: IMessageService,
		@IEnvironmentService private readonly _environmentService: IEnvironmentService,
		@IInstantiationService private readonly _instantiationService: IInstantiationService,
30
		@ILifecycleService lifecycleService: ILifecycleService,
31 32
		@IExtensionService extensionService: IExtensionService,
	) {
33
		// wait for everything to be ready
34
		extensionService.onReady().then(() => {
35 36
			this._stopProfiling();
		});
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	}

	getId(): string {
		return 'performance.StartupProfiler';
	}

	private _stopProfiling(): void {

		const { profileStartup } = this._environmentService;
		if (!profileStartup) {
			return;
		}

		stopProfiling(profileStartup.dir, profileStartup.prefix).then(() => {
			readdir(profileStartup.dir).then(files => {
				return files.filter(value => value.indexOf(profileStartup.prefix) === 0);
			}).then(files => {
				const profileFiles = files.reduce((prev, cur) => `${prev}${join(profileStartup.dir, cur)}\n`, '\n');

56
				const primaryButton = this._messageService.confirmSync({
57 58 59 60 61 62 63 64 65
					type: 'info',
					message: localize('prof.message', "Successfully created profiles."),
					detail: localize('prof.detail', "Please create an issue and manually attach the following files:\n{0}", profileFiles),
					primaryButton: localize('prof.restartAndFileIssue', "Create Issue and Restart"),
					secondaryButton: localize('prof.restart', "Restart")
				});

				if (primaryButton) {
					const action = this._instantiationService.createInstance(ReportPerformanceIssueAction, ReportPerformanceIssueAction.ID, ReportPerformanceIssueAction.LABEL);
66 67
					TPromise.join<any>([
						this._windowsService.showItemInFolder(join(profileStartup.dir, files[0])),
J
Johannes Rieken 已提交
68
						action.run(`:warning: Make sure to **attach** these files from your *home*-directory: :warning:\n${files.map(file => `-\`${file}\``).join('\n')}`)
69 70
					]).then(() => {
						// keep window stable until restart is selected
71
						this._messageService.confirmSync({
72 73 74 75 76 77 78 79
							type: 'info',
							message: localize('prof.thanks', "Thanks for helping us."),
							detail: localize('prof.detail.restart', "A final restart is required to continue to use '{0}'. Again, thank you for your contribution.", this._environmentService.appNameLong),
							primaryButton: localize('prof.restart', "Restart"),
							secondaryButton: null
						});
						// now we are ready to restart
						this._windowsService.relaunch({ removeArgs: ['--prof-startup'] });
80
					});
81 82 83 84

				} else {
					// simply restart
					this._windowsService.relaunch({ removeArgs: ['--prof-startup'] });
85 86 87 88 89 90
				}
			});
		});
	}
}

91
const registry = Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench);
92
registry.registerWorkbenchContribution(StartupProfiler, LifecyclePhase.Running);