Skip to content

Commit

Permalink
Add session start time to native payload
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador committed Dec 2, 2019
1 parent 51febae commit 0473fd7
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions ios/FlipperReactPerformancePlugin.m
Expand Up @@ -34,13 +34,15 @@ @interface FlipperReactPerformancePlugin ()
@implementation FlipperReactPerformancePlugin
{
CFTimeInterval _nativeStartupDuration;
NSTimeInterval _sessionStartTime;
}

- (instancetype)init {
if (self = [super init]) {
CFTimeInterval absoluteTimeToRelativeTime = CACurrentMediaTime() - [NSDate date].timeIntervalSince1970;
sPreMainStartTimeRelative = getProcessStartTime() + absoluteTimeToRelativeTime;
_bridge = nil;
_sessionStartTime = [NSDate date].timeIntervalSince1970;
_nativeStartupDuration = CACurrentMediaTime() - sPreMainStartTimeRelative;
}
return self;
Expand All @@ -61,23 +63,28 @@ - (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge;
NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
[notificationCenter addObserver:self
selector:@selector(emitSafely)
selector:@selector(scriptWillLoad)
name:RCTJavaScriptWillStartLoadingNotification
object:bridge];
[notificationCenter addObserver:self
selector:@selector(emitSafely)
selector:@selector(sendMeasurements)
name:RCTJavaScriptDidLoadNotification
object:bridge];
[notificationCenter addObserver:self
selector:@selector(emitSafely)
selector:@selector(sendMeasurements)
name:RCTBridgeDidDownloadScriptNotification
object:bridge];
[notificationCenter addObserver:self
selector:@selector(emitSafely)
selector:@selector(sendMeasurements)
name:RCTContentDidAppearNotification
object:nil];
}

- (void)scriptWillLoad {
_sessionStartTime = [NSDate date].timeIntervalSince1970;
[self sendMeasurements];
}

- (NSObject *)getDurationForTag:(RCTPLTag)tag {
if (self.bridge) {
int64_t value = [self.bridge.performanceLogger durationForTag:tag];
Expand All @@ -98,23 +105,27 @@ - (NSObject *)getValueForTag:(RCTPLTag)tag {
return [NSNull null];
}

- (void)emitSafely {
- (void)sendSafely:(NSString *)method withParams:(NSDictionary *)params {
if (!self.connection) {
return;
}

[self.connection send:@"measurements" withParams:@{
@"NativeStartup": @(_nativeStartupDuration * 1000),
@"BundleSize": [self getValueForTag:RCTPLBundleSize],
@"ScriptDownload": [self getDurationForTag:RCTPLScriptDownload],
@"ScriptExecution": [self getDurationForTag:RCTPLScriptExecution],
@"TTI": [self getDurationForTag:RCTPLTTI],
[self.connection send:method withParams:params];
}

- (void)sendMeasurements {
[self sendSafely:@"measurements" withParams:@{
@"sessionStartedAt": @(ceil(_sessionStartTime * 1000)),
@"nativeStartup": @(_nativeStartupDuration * 1000),
@"bundleSize": [self getValueForTag:RCTPLBundleSize],
@"scriptDownload": [self getDurationForTag:RCTPLScriptDownload],
@"scriptExecution": [self getDurationForTag:RCTPLScriptExecution],
@"tti": [self getDurationForTag:RCTPLTTI],
}];
}

- (void)didConnect:(id<FlipperConnection>)connection {
self.connection = connection;
[self emitSafely];
[self sendMeasurements];
}

- (void)didDisconnect {
Expand All @@ -126,7 +137,7 @@ - (NSString *)identifier {
}

- (BOOL)runInBackground {
return NO;
return YES;
}

- (void)dealloc {
Expand Down

0 comments on commit 0473fd7

Please sign in to comment.