From 66d7e3c944528963072e9d167f3da90f9b063a2f Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 24 May 2017 12:37:52 +0100 Subject: [PATCH] [perf][android] Add perf module --- .../invertase/firebase/RNFirebasePackage.java | 2 ++ lib/firebase.js | 4 ++++ lib/modules/perf/Trace.js | 24 +++++++++++++++++++ lib/modules/perf/index.js | 18 ++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 lib/modules/perf/Trace.js create mode 100644 lib/modules/perf/index.js diff --git a/android/src/main/java/io/invertase/firebase/RNFirebasePackage.java b/android/src/main/java/io/invertase/firebase/RNFirebasePackage.java index b24a3251..fb5ac6e1 100644 --- a/android/src/main/java/io/invertase/firebase/RNFirebasePackage.java +++ b/android/src/main/java/io/invertase/firebase/RNFirebasePackage.java @@ -20,6 +20,7 @@ import io.invertase.firebase.database.RNFirebaseDatabase; import io.invertase.firebase.analytics.RNFirebaseAnalytics; import io.invertase.firebase.crash.RNFirebaseCrash; import io.invertase.firebase.messaging.RNFirebaseMessaging; +import io.invertase.firebase.perf.RNFirebasePerformance; @SuppressWarnings("unused") public class RNFirebasePackage implements ReactPackage { @@ -43,6 +44,7 @@ public class RNFirebasePackage implements ReactPackage { modules.add(new RNFirebaseMessaging(reactContext)); modules.add(new RNFirebaseCrash(reactContext)); modules.add(new RNFirebaseRemoteConfig(reactContext)); + modules.add(new RNFirebasePerformance(reactContext)); return modules; } diff --git a/lib/firebase.js b/lib/firebase.js index a0096b21..84263a64 100644 --- a/lib/firebase.js +++ b/lib/firebase.js @@ -15,6 +15,7 @@ import Messaging, { statics as MessagingStatics } from './modules/messaging'; import Analytics from './modules/analytics'; import Crash from './modules/crash'; import RemoteConfig from './modules/config'; +import Performance from './modules/perf'; const instances: Object = { default: null }; const FirebaseModule = NativeModules.RNFirebase; @@ -34,6 +35,7 @@ export default class Firebase { _messaging: ?Object; _config: ?Object; _crash: ?Object; + _perf: ?Object; auth: Function; crash: Function; @@ -42,6 +44,7 @@ export default class Firebase { analytics: Function; messaging: Function; config: Function; + perf: Function; eventHandlers: Object; debug: boolean; @@ -86,6 +89,7 @@ export default class Firebase { this.analytics = this._staticsOrInstance('analytics', {}, Analytics); this.crash = this._staticsOrInstance('crash', {}, Crash); this.config = this._staticsOrInstance('config', {}, RemoteConfig); + this.perf = this._staticsOrInstance('perf', {}, Performance); // init auth to start listeners this.auth(); diff --git a/lib/modules/perf/Trace.js b/lib/modules/perf/Trace.js new file mode 100644 index 00000000..096444af --- /dev/null +++ b/lib/modules/perf/Trace.js @@ -0,0 +1,24 @@ +const FirebasePerformance = NativeModules.RNFirebasePerformance; + +export default class Trace { + + constructor(perf: Object, identifier: string) { + this.perf = perf; + this.identifier = identifier; + } + + start() { + this.perf.log.debug(`Starting trace for ${this.identifier}`); + FirebasePerformance.start(this.identifier); + } + + stop() { + this.perf.log.debug(`Stopping trace for ${this.identifier}`); + FirebasePerformance.stop(this.identifier); + } + + incrementCounter(event: string) { + this.perf.log.debug(`Incrementing counter event ${event} trace for ${this.identifier}`); + FirebasePerformance.incrementCounter(this.identifier, event); + } +} diff --git a/lib/modules/perf/index.js b/lib/modules/perf/index.js new file mode 100644 index 00000000..6062e3f9 --- /dev/null +++ b/lib/modules/perf/index.js @@ -0,0 +1,18 @@ +// @flow +import { Base } from './../base'; +import Trace from './Trace'; + +export default class PerformanceMonitoring extends Base { + + /** + * Returns a new trace instance + * @param trace + */ + newTrace(trace: string): void { + return new Trace(this, trace); + } + + get namespace(): string { + return 'firebase:perf'; + } +}