Conditionally enable performance metrics

This commit is contained in:
Ron Buckton 2016-05-17 18:43:13 -07:00
parent b8a9efb66c
commit abc9fda0e8
5 changed files with 55 additions and 34 deletions

View file

@ -94,10 +94,10 @@ namespace ts {
const binder = createBinder();
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
Performance.mark("bindStart");
performance.mark("bindStart");
binder(file, options);
Performance.mark("bindEnd");
Performance.measure("bindTime", "bindStart", "bindEnd");
performance.mark("bindEnd");
performance.measure("bindTime", "bindStart", "bindEnd");
}
function createBinder(): (file: SourceFile, options: CompilerOptions) => void {

View file

@ -16064,12 +16064,12 @@ namespace ts {
}
function checkSourceFile(node: SourceFile) {
Performance.mark("checkStart");
performance.mark("checkStart");
checkSourceFileWorker(node);
Performance.mark("checkEnd");
Performance.measure("checkTime", "checkStart", "checkEnd");
performance.mark("checkEnd");
performance.measure("checkTime", "checkStart", "checkEnd");
}
// Fully type check a source file and collect the relevant diagnostics.

View file

@ -1134,8 +1134,9 @@ namespace ts {
: ((fileName) => fileName.toLowerCase());
}
/** Performance measurements for the compiler. */
/*@internal*/
export namespace Performance {
export namespace performance {
interface MarkData {
markName: string;
timestamp: number;
@ -1159,6 +1160,7 @@ namespace ts {
const measures: MeasureData[] = [];
let start = now();
let enabled = false;
/** Gets the current timer for performance measurements. */
export function now() {
@ -1172,7 +1174,9 @@ namespace ts {
* @param markName The name of the performance mark.
*/
export function mark(markName: string) {
marks.push({ markName, timestamp: now() });
if (enabled) {
marks.push({ markName, timestamp: now() });
}
}
/**
@ -1188,13 +1192,15 @@ namespace ts {
* If not specified, the current time is used.
*/
export function measure(measureName: string, startMarkName?: string, endMarkName?: string) {
measures.push({
measureName,
startMarkName,
endMarkName,
timestamp: now(),
marksOffset: marks.length
});
if (enabled) {
measures.push({
measureName,
startMarkName,
endMarkName,
timestamp: now(),
marksOffset: marks.length
});
}
}
/**
@ -1268,5 +1274,15 @@ namespace ts {
measures.length = 0;
start = now();
}
/** Enables performance measurements for the compiler. */
export function enable() {
enabled = true;
}
/** Disables performance measurements for the compiler. */
export function disable() {
enabled = false;
}
}
}

View file

@ -776,10 +776,10 @@ namespace ts {
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
let text: string;
try {
Performance.mark("ioReadStart");
performance.mark("ioReadStart");
text = sys.readFile(fileName, options.charset);
Performance.mark("ioReadEnd");
Performance.measure("ioReadTime", "ioReadStart", "ioReadEnd");
performance.mark("ioReadEnd");
performance.measure("ioReadTime", "ioReadStart", "ioReadEnd");
}
catch (e) {
if (onError) {
@ -846,7 +846,7 @@ namespace ts {
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
try {
Performance.mark("ioWriteStart");
performance.mark("ioWriteStart");
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) {
@ -856,8 +856,8 @@ namespace ts {
sys.writeFile(fileName, data, writeByteOrderMark);
}
Performance.mark("ioWriteEnd");
Performance.measure("ioWriteTime", "ioWriteStart", "ioWriteEnd");
performance.mark("ioWriteEnd");
performance.measure("ioWriteTime", "ioWriteStart", "ioWriteEnd");
}
catch (e) {
if (onError) {
@ -959,7 +959,7 @@ namespace ts {
let resolvedTypeReferenceDirectives: Map<ResolvedTypeReferenceDirective> = {};
let fileProcessingDiagnostics = createDiagnosticCollection();
Performance.mark("programStart");
performance.mark("programStart");
host = host || createCompilerHost(options);
@ -1052,8 +1052,8 @@ namespace ts {
verifyCompilerOptions();
Performance.mark("programEnd");
Performance.measure("programTime", "programStart", "programEnd");
performance.mark("programEnd");
performance.measure("programTime", "programStart", "programEnd");
return program;
@ -1286,7 +1286,7 @@ namespace ts {
// checked is to not pass the file to getEmitResolver.
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile);
Performance.mark("emitStart");
performance.mark("emitStart");
// TODO(rbuckton): remove USE_TRANSFORMS condition when we switch to transforms permanently.
let useLegacyEmitter = options.useLegacyEmitter;
@ -1300,8 +1300,8 @@ namespace ts {
getEmitHost(writeFileCallback),
sourceFile);
Performance.mark("emitEnd");
Performance.measure("emitTime", "emitStart", "emitEnd");
performance.mark("emitEnd");
performance.measure("emitTime", "emitStart", "emitEnd");
return emitResult;
}

View file

@ -544,7 +544,10 @@ namespace ts {
}
function compile(fileNames: string[], compilerOptions: CompilerOptions, compilerHost: CompilerHost) {
Performance.reset();
if (compilerOptions.diagnostics) {
performance.enable();
performance.reset();
}
const program = createProgram(fileNames, compilerOptions, compilerHost);
const exitStatus = compileProgram();
@ -556,12 +559,14 @@ namespace ts {
}
if (compilerOptions.diagnostics) {
const ioReadTime = reduceLeft(Performance.getMeasures("ioReadTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const ioWriteTime = reduceLeft(Performance.getMeasures("ioWriteTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const programTime = reduceLeft(Performance.getMeasures("programTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const bindTime = reduceLeft(Performance.getMeasures("bindTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const checkTime = reduceLeft(Performance.getMeasures("checkTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const emitTime = reduceLeft(Performance.getMeasures("emitTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const ioReadTime = reduceLeft(performance.getMeasures("ioReadTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const ioWriteTime = reduceLeft(performance.getMeasures("ioWriteTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const programTime = reduceLeft(performance.getMeasures("programTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const bindTime = reduceLeft(performance.getMeasures("bindTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const checkTime = reduceLeft(performance.getMeasures("checkTime"), (aggregate, measure) => aggregate + measure.duration, 0);
const emitTime = reduceLeft(performance.getMeasures("emitTime"), (aggregate, measure) => aggregate + measure.duration, 0);
performance.disable();
performance.reset();
const memoryUsed = sys.getMemoryUsage ? sys.getMemoryUsage() : -1;
reportCountStatistic("Files", program.getSourceFiles().length);