-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(performance): AngularFire Performance Monitoring #2064
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
755ab85
First.
jamesdaniels 2687080
Now firebase/performance...
jamesdaniels ec1dfbf
Tests and cleanup
jamesdaniels db73285
Merged master
jamesdaniels 5a6c4d6
Adding AUTOMATICALLY_TRACE_CORE_NG_METRICS InjectionToken and getting…
jamesdaniels 2c60616
Doc structure
jamesdaniels 2bff468
Merge branch 'performance_monitoring' of github.com:angular/angularfi…
jamesdaniels 1d277f8
Merged master.
jamesdaniels f415faa
Operator rename, more configuration, and docs
jamesdaniels c59c4a2
Build with node:lts and longer timeout
jamesdaniels 39f73f7
Don't blow up when v8 or 9-rc drop
jamesdaniels 164d55a
Use the angular-devkit/architect RC until 8.0 drops
jamesdaniels de29d4f
Changelog
jamesdaniels 1344106
Changing the main package.json to angular/fire
jamesdaniels 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
# Getting started with Performance Monitoring | ||
|
||
## Automatic page load tracing | ||
|
||
Understand your Angular application's real-world performance with [Firebase Performance Monitoring](https://firebase.google.com/docs/perf-mon). Performance Monitoring automatically provides a trace for **page load** when you add `AngularFirePerformanceModule` into your App Module's imports. | ||
|
||
```ts | ||
import { AngularFireModule } from '@angular/fire'; | ||
import { AngularFirePerformanceModule } from '@angular/fire/performance'; | ||
import { environment } from '../environments/environment'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
BrowserModule, | ||
AngularFireModule.initializeApp(environment.firebase), | ||
AngularFirePerformanceModule, | ||
... | ||
], | ||
declarations: [ AppComponent ], | ||
bootstrap: [ AppComponent ] | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
The page load trace breaks down into the following default metrics: | ||
|
||
* [first paint traces](https://firebase.google.com/docs/perf-mon/automatic-web#first-paint) — measure the time between when the user navigates to a page and when any visual change happens | ||
* [first contentful paint traces](https://firebase.google.com/docs/perf-mon/automatic-web#contentful-paint) — measure the time between when a user navigates to a page and when meaningful content displays, like an image or text | ||
* [domInteractive traces](https://firebase.google.com/docs/perf-mon/automatic-web#domInteractive) — measure the time between when the user navigates to a page and when the page is considered interactive for the user | ||
* [domContentLoadedEventEnd traces](https://firebase.google.com/docs/perf-mon/automatic-web#domContentLoaded) — measure the time between when the user navigates to a page and when the initial HTML document is completely loaded and parsed | ||
* [loadEventEnd traces](https://firebase.google.com/docs/perf-mon/automatic-web#loadEventEnd) — measure the time between when the user navigates to the page and when the current document's load event completes | ||
* [first input delay traces](https://firebase.google.com/docs/perf-mon/automatic-web#input-delay) — measure the time between when the user interacts with a page and when the browser is able to respond to that input | ||
* **Angular specific traces** - measure the time needed for `ApplicationRef.isStable` to be true, an important metric to track if you're concerned about solving Zone.js issues for proper functionality of NGSW and Server Side Rendering | ||
|
||
## Manual traces | ||
|
||
You can inject `AngularFirePerformance` to perform manual traces on Observables. | ||
|
||
```ts | ||
constructor(private afp: AngularFirePerformance, private afs: AngularFirestore) {} | ||
|
||
ngOnInit() { | ||
this.articles = afs.collection('articles') | ||
.collection('articles', ref => ref.orderBy('publishedAt', 'desc')) | ||
.snapshotChanges() | ||
.pipe( | ||
// measure the amount of time between the Observable being subscribed to and first emission (or completion) | ||
this.afp.trace('getArticles'), | ||
map(articles => ...) | ||
); | ||
} | ||
``` | ||
|
||
### `trace(name:string)` | ||
|
||
The most basic operator, `trace` will measure the amount of time it takes for your observable to either complete or emit its first value. Beyond the basic trace there are several other operators: | ||
|
||
### `traceUntil(name:string, test: (T) => Boolean)` | ||
|
||
Trace the observable until the first emission that passes the provided test. | ||
|
||
### `traceWhile(name:string, test: (T) => Boolean)` | ||
|
||
Trace the observable until the first emission that fails the provided test. | ||
|
||
### `traceUntilLast(name:string)` | ||
|
||
Trace the observable until completion. | ||
|
||
### `traceUntilFirst(name: string)` | ||
|
||
Traces the observable until the first emission. | ||
|
||
## Advanced usage | ||
|
||
### Configuration via Dependency Injection | ||
|
||
By default, `AngularFirePerformanceModule` traces your Angular application's time to `ApplicationRef.isStable`. `isStable` is an important metric to track if you're concerned about proper functionality of NGSW and Server Side Rendering. If you want to opt-out of the tracing of this metric use the `AUTOMATICALLY_TRACE_CORE_NG_METRICS` injection token: | ||
|
||
```ts | ||
import { NgModule } from '@angular/core'; | ||
import { AngularFirePerformanceModule, AUTOMATICALLY_TRACE_CORE_NG_METRICS } from '@angular/fire/functions'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
... | ||
AngularFirePerformanceModule, | ||
... | ||
], | ||
... | ||
providers: [ | ||
{ provide: AUTOMATICALLY_TRACE_CORE_NG_METRICS, useValue: false } | ||
] | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
Similarly, setting `INSTRUMENTATION_ENABLED` or `DATA_COLLECTION_ENABLED` to false disable all automatic and custom traces respectively. | ||
|
||
### Get at an observable form of trace | ||
|
||
`trace$(name:string)` provides an observable version of `firebase/perf`'s `.trace` method; the basis for `AngularFirePerfomance`'s pipes. | ||
|
||
`.subscribe()` is equivalent to calling `.start()` | ||
`.unsubscribe()` is equivalent to calling `.stop()` | ||
|
||
### Using `TraceOptions` to collect additional metrics | ||
|
||
`TraceOptions` can be provided to the aformentioned operators to collect custom metrics and attributes on your traces: | ||
|
||
```ts | ||
type TraceOptions = { | ||
metrics?: { [key:string]: number }, | ||
attributes?: { [key:string]: string }, | ||
attribute$?: { [key:string]: Observable<string> }, | ||
incrementMetric$: { [key:string]: Observable<number|void|null|undefined> }, | ||
metric$?: { [key:string]: Observable<number> } | ||
}; | ||
``` | ||
|
||
#### Usage: | ||
|
||
```ts | ||
const articleLength$ = this.articles.pipe( | ||
map(actions => actions.length) | ||
); | ||
|
||
const articleSize$ = this.articles.pipe( | ||
map(actions => actions.reduce((sum, a) => sum += JSON.stringify(a.payload.doc.data()).length)) | ||
) | ||
|
||
this.articles = afs.collection('articles') | ||
.collection('articles', ref => ref.orderBy('publishedAt', 'desc')) | ||
.snapshotChanges() | ||
.pipe( | ||
this.afp.trace('getArticles', { | ||
attributes: { gitSha: '1d277f823ad98dd739fb86e9a6c440aa8237ff3a' }, | ||
metrics: { something: 42 }, | ||
metrics$: { count: articleLength$, size: articleSize$ }, | ||
attributes$: { user: this.afAuth.user }, | ||
incrementMetric$: { buttonClicks: fromEvent(button, 'click') } | ||
}), | ||
share() | ||
); | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
export * from './performance.spec'; |
This file contains hidden or 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 @@ | ||
export * from './public_api'; |
This file contains hidden or 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,30 @@ | ||
{ | ||
"name": "@angular/fire/performance", | ||
"version": "ANGULARFIRE2_VERSION", | ||
"description": "The performance monitoring module", | ||
"main": "../bundles/performance.umd.js", | ||
"module": "index.js", | ||
"es2015": "./es2015/index.js", | ||
"keywords": [ | ||
"angular", | ||
"firebase", | ||
"rxjs" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/angular/angularfire2.git" | ||
}, | ||
"author": "angular,firebase", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@angular/fire": "ANGULARFIRE2_VERSION", | ||
"@angular/common": "ANGULAR_VERSION", | ||
"@angular/core": "ANGULAR_VERSION", | ||
"@angular/platform-browser": "ANGULAR_VERSION", | ||
"@angular/platform-browser-dynamic": "ANGULAR_VERSION", | ||
"firebase": "FIREBASE_VERSION", | ||
"rxjs": "RXJS_VERSION", | ||
"zone.js": "ZONEJS_VERSION" | ||
}, | ||
"typings": "index.d.ts" | ||
} |
This file contains hidden or 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,9 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { AngularFirePerformance } from './performance'; | ||
|
||
import 'firebase/performance'; | ||
|
||
@NgModule({ | ||
providers: [ AngularFirePerformance ] | ||
}) | ||
export class AngularFirePerformanceModule { } |
This file contains hidden or 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,36 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { FirebaseApp, AngularFireModule } from '@angular/fire'; | ||
import { AngularFirePerformance, AngularFirePerformanceModule } from '@angular/fire/performance'; | ||
import { COMMON_CONFIG } from './test-config'; | ||
|
||
describe('AngularFirePerformance', () => { | ||
let app: FirebaseApp; | ||
let afp: AngularFirePerformance; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
AngularFireModule.initializeApp(COMMON_CONFIG), | ||
AngularFirePerformanceModule | ||
] | ||
}); | ||
inject([FirebaseApp, AngularFirePerformance], (app_: FirebaseApp, _perf: AngularFirePerformance) => { | ||
app = app_; | ||
afp = _perf; | ||
})(); | ||
}); | ||
|
||
afterEach(done => { | ||
app.delete(); | ||
done(); | ||
}); | ||
|
||
it('should be exist', () => { | ||
expect(afp instanceof AngularFirePerformance).toBe(true); | ||
}); | ||
|
||
it('should have the Performance instance', () => { | ||
expect(afp.performance).toBeDefined(); | ||
}); | ||
|
||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.