forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ivy): performance trace mechanism for ngtsc (angular#29380)
This commit adds a `tracePerformance` option for tsconfig.json. When specified, it causes a JSON file with timing information from the ngtsc compiler to be emitted at the specified path. This tracing system is used to instrument the analysis/emit phases of compilation, and will be useful in debugging future integration work with @angular/cli. See ngtsc/perf/README.md for more details. PR Close angular#29380
- Loading branch information
Showing
14 changed files
with
289 additions
and
5 deletions.
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
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,15 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
ts_library( | ||
name = "perf", | ||
srcs = ["index.ts"] + glob([ | ||
"src/*.ts", | ||
]), | ||
deps = [ | ||
"//packages:types", | ||
"@npm//@types/node", | ||
"@npm//typescript", | ||
], | ||
) |
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,21 @@ | ||
# What is the `perf` package? | ||
|
||
This package contains utilities for collecting performance information from around the compiler and for producing ngtsc performance traces. | ||
|
||
This feature is currently undocumented and not exposed to users as the trace file format is still unstable. | ||
|
||
# What is a performance trace? | ||
|
||
A performance trace is a JSON file which logs events within ngtsc with microsecond precision. It tracks the phase of compilation, the file and possibly symbol being compiled, and other metadata explaining what the compiler was doing during the event. | ||
|
||
Traces track two specific kinds of events: marks and spans. A mark is a single event with a timestamp, while a span is two events (start and end) that cover a section of work in the compiler. Analyzing a file is a span event, while a decision (such as deciding not to emit a particular file) is a mark event. | ||
|
||
# Enabling performance traces | ||
|
||
Performance traces are enabled via the undocumented `tracePerformance` option in `angularCompilerOptions` inside the tsconfig.json file. This option takes a string path relative to the current directory, which will be used to write the trace JSON file. | ||
|
||
## In-Memory TS Host Tracing | ||
|
||
By default, the trace file will be written with the `fs` package directly. However, ngtsc supports in-memory compilation using a `ts.CompilerHost` for all operations. In the event that tracing is required when using an in-memory filesystem, a `ts:` prefix can be added to the value of `tracePerformance`, which will cause the trace JSON file to be written with the TS host's `writeFile` method instead. | ||
|
||
This is not done by default as `@angular/cli` does not allow writing arbitrary JSON files via its host. |
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,11 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export {PerfRecorder} from './src/api'; | ||
export {NOOP_PERF_RECORDER} from './src/noop'; | ||
export {PerfTracker} from './src/tracking'; |
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,18 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
|
||
export interface PerfRecorder { | ||
readonly enabled: boolean; | ||
|
||
mark(name: string, node?: ts.SourceFile|ts.Declaration, category?: string, detail?: string): void; | ||
start(name: string, node?: ts.SourceFile|ts.Declaration, category?: string, detail?: string): | ||
number; | ||
stop(span: number): void; | ||
} |
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,21 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
// This file uses 'process' | ||
/// <reference types="node" /> | ||
|
||
export type HrTime = [number, number]; | ||
|
||
export function mark(): HrTime { | ||
return process.hrtime(); | ||
} | ||
|
||
export function timeSinceInMicros(mark: HrTime): number { | ||
const delta = process.hrtime(mark); | ||
return (delta[0] * 1000000) + Math.floor(delta[1] / 1000); | ||
} |
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,20 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import {PerfRecorder} from './api'; | ||
|
||
export const NOOP_PERF_RECORDER: PerfRecorder = { | ||
enabled: false, | ||
mark: (name: string, node: ts.SourceFile | ts.Declaration, category?: string, detail?: string): | ||
void => {}, | ||
start: (name: string, node: ts.SourceFile | ts.Declaration, category?: string, detail?: string): | ||
number => { return 0;}, | ||
stop: (span: number | false): void => {}, | ||
}; |
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,110 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/// <reference types="node" /> | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import {PerfRecorder} from './api'; | ||
import {HrTime, mark, timeSinceInMicros} from './clock'; | ||
|
||
export class PerfTracker implements PerfRecorder { | ||
private nextSpanId = 1; | ||
private log: PerfLogEvent[] = []; | ||
|
||
readonly enabled = true; | ||
|
||
private constructor(private zeroTime: HrTime) {} | ||
|
||
static zeroedToNow(): PerfTracker { return new PerfTracker(mark()); } | ||
|
||
mark(name: string, node?: ts.SourceFile|ts.Declaration, category?: string, detail?: string): | ||
void { | ||
const msg = this.makeLogMessage(PerfLogEventType.MARK, name, node, category, detail, undefined); | ||
this.log.push(msg); | ||
} | ||
|
||
start(name: string, node?: ts.SourceFile|ts.Declaration, category?: string, detail?: string): | ||
number { | ||
const span = this.nextSpanId++; | ||
const msg = this.makeLogMessage(PerfLogEventType.SPAN_OPEN, name, node, category, detail, span); | ||
this.log.push(msg); | ||
return span; | ||
} | ||
|
||
stop(span: number): void { | ||
this.log.push({ | ||
type: PerfLogEventType.SPAN_CLOSE, | ||
span, | ||
stamp: timeSinceInMicros(this.zeroTime), | ||
}); | ||
} | ||
|
||
private makeLogMessage( | ||
type: PerfLogEventType, name: string, node: ts.SourceFile|ts.Declaration|undefined, | ||
category: string|undefined, detail: string|undefined, span: number|undefined): PerfLogEvent { | ||
const msg: PerfLogEvent = { | ||
type, | ||
name, | ||
stamp: timeSinceInMicros(this.zeroTime), | ||
}; | ||
if (category !== undefined) { | ||
msg.category = category; | ||
} | ||
if (detail !== undefined) { | ||
msg.detail = detail; | ||
} | ||
if (span !== undefined) { | ||
msg.span = span; | ||
} | ||
if (node !== undefined) { | ||
msg.file = node.getSourceFile().fileName; | ||
if (!ts.isSourceFile(node)) { | ||
const name = ts.getNameOfDeclaration(node); | ||
if (name !== undefined && ts.isIdentifier(name)) { | ||
msg.declaration = name.text; | ||
} | ||
} | ||
} | ||
return msg; | ||
} | ||
|
||
asJson(): unknown { return this.log; } | ||
|
||
serializeToFile(target: string, host: ts.CompilerHost): void { | ||
const json = JSON.stringify(this.log, null, 2); | ||
|
||
if (target.startsWith('ts:')) { | ||
target = target.substr('ts:'.length); | ||
const outFile = path.posix.resolve(host.getCurrentDirectory(), target); | ||
host.writeFile(outFile, json, false); | ||
} else { | ||
const outFile = path.posix.resolve(host.getCurrentDirectory(), target); | ||
fs.writeFileSync(outFile, json); | ||
} | ||
} | ||
} | ||
|
||
export interface PerfLogEvent { | ||
name?: string; | ||
span?: number; | ||
file?: string; | ||
declaration?: string; | ||
type: PerfLogEventType; | ||
category?: string; | ||
detail?: string; | ||
stamp: number; | ||
} | ||
|
||
export enum PerfLogEventType { | ||
SPAN_OPEN, | ||
SPAN_CLOSE, | ||
MARK, | ||
} |
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
Oops, something went wrong.