forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Tests to measure activation times of extension #1813
Merged
DonJayamanne
merged 4 commits into
microsoft:master
from
DonJayamanne:testAgainstInsiders
Jun 5, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ analysis/** | |
bin/** | ||
obj/** | ||
.pytest_cache | ||
tmp/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,6 @@ requirements.txt | |
scripts/** | ||
src/** | ||
test/** | ||
tmp/** | ||
typings/** | ||
vsc-extension-quickstart.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Create tests to measure activation times for the extension. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
// tslint:disable:no-invalid-this no-console | ||
|
||
import { expect } from 'chai'; | ||
import * as fs from 'fs-extra'; | ||
import { EOL } from 'os'; | ||
import * as path from 'path'; | ||
import { commands, extensions } from 'vscode'; | ||
import { StopWatch } from '../../client/common/stopWatch'; | ||
|
||
const AllowedIncreaseInActivationDelayInMS = 500; | ||
|
||
suite('Activation Times', () => { | ||
if (process.env.ACTIVATION_TIMES_LOG_FILE_PATH) { | ||
const logFile = process.env.ACTIVATION_TIMES_LOG_FILE_PATH; | ||
const sampleCounter = fs.existsSync(logFile) ? fs.readFileSync(logFile, { encoding: 'utf8' }).toString().split(/\r?\n/g).length : 1; | ||
if (sampleCounter > 10) { | ||
return; | ||
} | ||
test(`Capture Extension Activation Times (Version: ${process.env.ACTIVATION_TIMES_EXT_VERSION}, sample: ${sampleCounter})`, async () => { | ||
const pythonExtension = extensions.getExtension('ms-python.python'); | ||
if (pythonExtension) { | ||
throw new Error('Python Extension not found'); | ||
} | ||
const stopWatch = new StopWatch(); | ||
await pythonExtension!.activate(); | ||
const elapsedTime = stopWatch.elapsedTime; | ||
if (elapsedTime > 10) { | ||
await fs.ensureDir(path.dirname(logFile)); | ||
await fs.appendFile(logFile, `${elapsedTime}${EOL}`, { encoding: 'utf8' }); | ||
console.log(`Loaded in ${elapsedTime}ms`); | ||
} | ||
commands.executeCommand('workbench.action.reloadWindow'); | ||
}); | ||
} | ||
|
||
if (process.env.ACTIVATION_TIMES_DEV_LOG_FILE_PATHS && | ||
process.env.ACTIVATION_TIMES_RELEASE_LOG_FILE_PATHS && | ||
process.env.ACTIVATION_TIMES_DEV_ANALYSIS_LOG_FILE_PATHS) { | ||
|
||
test('Test activation times of Dev vs Release Extension', async () => { | ||
function getActivationTimes(files: string[]) { | ||
const activationTimes: number[] = []; | ||
for (const file of files) { | ||
fs.readFileSync(file, { encoding: 'utf8' }).toString() | ||
.split(/\r?\n/g) | ||
.map(line => line.trim()) | ||
.filter(line => line.length > 0) | ||
.map(line => parseInt(line, 10)) | ||
.forEach(item => activationTimes.push(item)); | ||
} | ||
return activationTimes; | ||
} | ||
const devActivationTimes = getActivationTimes(JSON.parse(process.env.ACTIVATION_TIMES_DEV_LOG_FILE_PATHS!)); | ||
const releaseActivationTimes = getActivationTimes(JSON.parse(process.env.ACTIVATION_TIMES_RELEASE_LOG_FILE_PATHS!)); | ||
const analysisEngineActivationTimes = getActivationTimes(JSON.parse(process.env.ACTIVATION_TIMES_DEV_ANALYSIS_LOG_FILE_PATHS!)); | ||
const devActivationAvgTime = devActivationTimes.reduce((sum, item) => sum + item, 0) / devActivationTimes.length; | ||
const releaseActivationAvgTime = releaseActivationTimes.reduce((sum, item) => sum + item, 0) / releaseActivationTimes.length; | ||
const analysisEngineActivationAvgTime = analysisEngineActivationTimes.reduce((sum, item) => sum + item, 0) / analysisEngineActivationTimes.length; | ||
|
||
console.log(`Dev version Loaded in ${devActivationAvgTime}ms`); | ||
console.log(`Release version Loaded in ${releaseActivationAvgTime}ms`); | ||
console.log(`Analysis Engine Loaded in ${analysisEngineActivationAvgTime}ms`); | ||
|
||
expect(devActivationAvgTime - releaseActivationAvgTime).to.be.lessThan(AllowedIncreaseInActivationDelayInMS, 'Activation times have increased above allowed threshold.'); | ||
}); | ||
} | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "python.jediEnabled": true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
/* | ||
Comparing performance metrics is not easy (the metrics can and always get skewed). | ||
One approach is to run the tests multile times and gather multiple sample data. | ||
For Extension activation times, we load both extensions x times, and re-load the window y times in each x load. | ||
I.e. capture averages by giving the extensions sufficient time to warm up. | ||
This block of code merely launches the tests by using either the dev or release version of the extension, | ||
and spawning the tests (mimic user starting tests from command line), this way we can run tests multiple times. | ||
*/ | ||
|
||
// tslint:disable:no-console no-require-imports no-var-requires | ||
|
||
import { spawn } from 'child_process'; | ||
import * as download from 'download'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import * as request from 'request'; | ||
import { EXTENSION_ROOT_DIR } from '../client/common/constants'; | ||
|
||
const NamedRegexp = require('named-js-regexp'); | ||
const StreamZip = require('node-stream-zip'); | ||
const del = require('del'); | ||
|
||
const tmpFolder = path.join(EXTENSION_ROOT_DIR, 'tmp'); | ||
const publishedExtensionPath = path.join(tmpFolder, 'ext', 'testReleaseExtensionsFolder'); | ||
const logFilesPath = path.join(tmpFolder, 'test', 'logs'); | ||
|
||
enum Version { | ||
Dev, Release | ||
} | ||
|
||
class TestRunner { | ||
public async start() { | ||
await del([path.join(tmpFolder, '**')]); | ||
await this.extractLatestExtension(publishedExtensionPath); | ||
|
||
const timesToLoadEachVersion = 3; | ||
const devLogFiles: string[] = []; | ||
const releaseLogFiles: string[] = []; | ||
const newAnalysisEngineLogFiles: string[] = []; | ||
|
||
for (let i = 0; i < timesToLoadEachVersion; i += 1) { | ||
await this.enableNewAnalysisEngine(false); | ||
|
||
const devLogFile = path.join(logFilesPath, `dev_loadtimes${i}.txt`); | ||
await this.capturePerfTimes(Version.Dev, devLogFile); | ||
devLogFiles.push(devLogFile); | ||
|
||
const releaseLogFile = path.join(logFilesPath, `release_loadtimes${i}.txt`); | ||
await this.capturePerfTimes(Version.Release, releaseLogFile); | ||
releaseLogFiles.push(releaseLogFile); | ||
|
||
// New Analysis engine. | ||
await this.enableNewAnalysisEngine(true); | ||
const newAnalysisEngineLogFile = path.join(logFilesPath, `newAnalysisEngine_loadtimes${i}.txt`); | ||
await this.capturePerfTimes(Version.Release, newAnalysisEngineLogFile); | ||
newAnalysisEngineLogFiles.push(newAnalysisEngineLogFile); | ||
} | ||
|
||
await this.runPerfTest(devLogFiles, releaseLogFiles, newAnalysisEngineLogFiles); | ||
} | ||
private async enableNewAnalysisEngine(enable: boolean) { | ||
const settings = `{ "python.jediEnabled": ${!enable} }`; | ||
await fs.writeFile(path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'performance', 'settings.json'), settings); | ||
} | ||
|
||
private async capturePerfTimes(version: Version, logFile: string) { | ||
const releaseVersion = await this.getReleaseVersion(); | ||
const devVersion = await this.getDevVersion(); | ||
await fs.ensureDir(path.dirname(logFile)); | ||
const env: { [key: string]: {} } = { | ||
ACTIVATION_TIMES_LOG_FILE_PATH: logFile, | ||
ACTIVATION_TIMES_EXT_VERSION: version === Version.Release ? releaseVersion : devVersion, | ||
CODE_EXTENSIONS_PATH: version === Version.Release ? publishedExtensionPath : EXTENSION_ROOT_DIR | ||
}; | ||
|
||
await this.launchTest(env); | ||
} | ||
private async runPerfTest(devLogFiles: string[], releaseLogFiles: string[], newAnalysisEngineLogFiles: string[]) { | ||
const env: { [key: string]: {} } = { | ||
ACTIVATION_TIMES_DEV_LOG_FILE_PATHS: JSON.stringify(devLogFiles), | ||
ACTIVATION_TIMES_RELEASE_LOG_FILE_PATHS: JSON.stringify(releaseLogFiles), | ||
ACTIVATION_TIMES_DEV_ANALYSIS_LOG_FILE_PATHS: JSON.stringify(newAnalysisEngineLogFiles) | ||
}; | ||
|
||
await this.launchTest(env); | ||
} | ||
|
||
private async launchTest(customEnvVars: { [key: string]: {} }) { | ||
await new Promise((resolve, reject) => { | ||
const env: { [key: string]: {} } = { | ||
TEST_FILES_SUFFIX: 'perf.test', | ||
CODE_TESTS_WORKSPACE: path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'performance'), | ||
...process.env, | ||
...customEnvVars | ||
}; | ||
|
||
const proc = spawn('node', [path.join(__dirname, 'standardTest.js')], { cwd: EXTENSION_ROOT_DIR, env }); | ||
proc.stdout.pipe(process.stdout); | ||
proc.stderr.pipe(process.stderr); | ||
proc.on('error', reject); | ||
proc.on('close', code => { | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
reject(`Failed with code ${code}.`); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private async extractLatestExtension(targetDir: string): Promise<void> { | ||
const extensionFile = await this.downloadExtension(); | ||
await this.unzip(extensionFile, targetDir); | ||
} | ||
|
||
private async getReleaseVersion(): Promise<string> { | ||
const url = 'https://marketplace.visualstudio.com/items?itemName=ms-python.python'; | ||
const content = await new Promise<string>((resolve, reject) => { | ||
request(url, (error, response, body) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
if (response.statusCode === 200) { | ||
return resolve(body); | ||
} | ||
reject(`Status code of ${response.statusCode} received.`); | ||
}); | ||
}); | ||
const re = NamedRegexp('"version"\S?:\S?"(:<version>\\d{4}\\.\\d{1,2}\\.\\d{1,2})"', 'g'); | ||
const matches = re.exec(content); | ||
return matches.groups().version; | ||
} | ||
|
||
private async getDevVersion(): Promise<string> { | ||
// tslint:disable-next-line:non-literal-require | ||
return require(path.join(EXTENSION_ROOT_DIR, 'package.json')).version; | ||
} | ||
|
||
private async unzip(zipFile: string, targetFolder: string): Promise<void> { | ||
await fs.ensureDir(targetFolder); | ||
return new Promise<void>((resolve, reject) => { | ||
const zip = new StreamZip({ | ||
file: zipFile, | ||
storeEntries: true | ||
}); | ||
zip.on('ready', async () => { | ||
zip.extract('extension', targetFolder, err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
zip.close(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
private async downloadExtension(): Promise<string> { | ||
const version = await this.getReleaseVersion(); | ||
const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-python/vsextensions/python/${version}/vspackage`; | ||
const destination = path.join(__dirname, `extension${version}.zip`); | ||
if (await fs.pathExists(destination)) { | ||
return destination; | ||
} | ||
|
||
await download(url, path.dirname(destination), { filename: path.basename(destination) }); | ||
return destination; | ||
} | ||
} | ||
|
||
new TestRunner().start().catch(ex => console.error('Error in running Performance Tests', ex)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also set a maximum perf number to help deal with skew, e.g. we never want this to average above 500 ms and we fail if that occurs. This prevents drift by saying we allow up to a 10% shift by being absolute instead of relative. It would also potentially negate needing to do a comparison against a current version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done