[perf][android] Add documentation

This commit is contained in:
Elliot Hesp 2017-05-24 12:51:44 +01:00
parent 66d7e3c944
commit d0d09163df
2 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package io.invertase.firebase.perf;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;
import java.util.HashMap;
public class RNFirebasePerformance extends ReactContextBaseJavaModule {
private static final String TAG = "RNFirebasePerformance";
private HashMap<String, Trace> traces = new HashMap<>();
public RNFirebasePerformance(ReactApplicationContext reactContext) {
super(reactContext);
Log.d(TAG, "New instance");
}
/**
* @return
*/
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void start(String identifier) {
Trace trace = getOrCreateTrace(identifier);
trace.start();
}
@ReactMethod
public void stop(String identifier) {
Trace trace = getOrCreateTrace(identifier);
trace.stop();
}
@ReactMethod
public void incrementCounter(String identifier, String event) {
Trace trace = getOrCreateTrace(identifier);
trace.incrementCounter(event);
}
private Trace getOrCreateTrace(String identifier) {
if (traces.containsKey(identifier)) {
return traces.get(identifier);
}
Trace trace = FirebasePerformance.getInstance().newTrace(identifier);
traces.put(identifier, trace);
return trace;
}
}

54
docs/modules/perf.md Normal file
View File

@ -0,0 +1,54 @@
# Performance Monitoring
!> Performance monitoring requires react-native-firebase version 1.2.0.
?> If you plan on using this module in your own application, please ensure the optional setup instructions for
[Android](#) and [iOS](#) have been followed.
Out of the box, [Firebase Performance Monitoring](https://firebase.google.com/docs/perf-mon/automatic) monitors a number of
[automatic traces](https://firebase.google.com/docs/perf-mon/automatic) such as app start/background/foreground response times.
You can easily trace your own events with RNFirebase:
## API
#### newTrace(id: `string`): `Trace`
Returns a new instance of Trace (see API below). The id is the unique name of something you'd like to run performance
monitoring against.
```js
const trace = firebase.perf().newTrace("test_trace");
```
### Trace
#### start()
Initializes the trace to start tracing performance to relay back to Firebase.
```js
trace.start();
```
#### incrementCounter(event: string)
Notifies Firebase an event has occured. These events will be visible on Firebase once your trace has stopped.
```js
someCacheService.get('user:123')
.then((user) => {
if (user) {
trace.incrementCounter('user_cache_hit');
} else {
trace.incrementCounter('user_cache_missed');
}
});
```
#### stop()
Stops performance tracing. The completed trace stats are now sent to Firebase.
```js
trace.stop();
```