Skip to content

Commit 43ebdf8

Browse files
authored
Fix emitting native markers when using TurboModules on iOS (oblador#73)
1 parent 0c053af commit 43ebdf8

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

packages/react-native-performance/ios/RNPerformanceManager.mm

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import <RNPerformanceSpec/RNPerformanceSpec.h>
1111
#endif
1212

13-
static CFTimeInterval getProcessStartTime()
13+
static CFTimeInterval RNPerformanceGetProcessStartTime()
1414
{
1515
size_t len = 4;
1616
int mib[len];
@@ -25,7 +25,7 @@ static CFTimeInterval getProcessStartTime()
2525
return startTime.tv_sec + startTime.tv_usec / 1e6;
2626
}
2727

28-
static int64_t getTimestamp()
28+
static int64_t RNPerformanceGetTimestamp()
2929
{
3030
return std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
3131
}
@@ -36,36 +36,43 @@ static int64_t getTimestamp()
3636
@implementation RNPerformanceManager
3737
{
3838
bool hasListeners;
39+
bool didEmit;
3940
}
4041

4142
RCT_EXPORT_MODULE();
4243

4344
+ (void) initialize
4445
{
4546
[super initialize];
46-
sNativeLaunchStart = (getProcessStartTime() - [NSDate date].timeIntervalSince1970) * 1000 + getTimestamp();
47-
sNativeLaunchEnd = getTimestamp();
47+
sNativeLaunchStart = (RNPerformanceGetProcessStartTime() - [NSDate date].timeIntervalSince1970) * 1000 + RNPerformanceGetTimestamp();
48+
sNativeLaunchEnd = RNPerformanceGetTimestamp();
4849
}
4950

5051
- (void)setBridge:(RCTBridge *)bridge
5152
{
5253
[super setBridge:bridge];
53-
NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
54-
[notificationCenter addObserver:self
55-
selector:@selector(contentDidAppear)
56-
name:RCTContentDidAppearNotification
57-
object:nil];
54+
[[NSNotificationCenter defaultCenter] addObserver:self
55+
selector:@selector(contentDidAppear)
56+
name:RCTContentDidAppearNotification
57+
object:nil];
5858
}
5959

6060
- (void)contentDidAppear
6161
{
62-
int64_t startTime = getTimestamp();
62+
if(didEmit != YES) {
63+
[self emitMarks];
64+
}
65+
}
66+
67+
- (void)emitMarks
68+
{
69+
didEmit = YES;
6370
[self emitMarkNamed:@"nativeLaunchStart" withStartTime:sNativeLaunchStart];
6471
[self emitMarkNamed:@"nativeLaunchEnd" withStartTime:sNativeLaunchEnd];
6572
[self emitTag:RCTPLScriptDownload withNamePrefix:@"download"];
6673
[self emitTag:RCTPLScriptExecution withNamePrefix:@"runJsBundle"];
6774
[self emitTag:RCTPLBridgeStartup withNamePrefix:@"bridgeSetup"];
68-
[self emitMarkNamed:@"contentAppeared" withStartTime:startTime];
75+
[self emitMarkNamed:@"contentAppeared" withMediaTime:[self.bridge.performanceLogger valueForTag:RCTPLTTI]];
6976
[self emitMetricNamed:@"bundleSize" withValue:@([self.bridge.performanceLogger valueForTag:RCTPLBundleSize])];
7077
}
7178

@@ -84,6 +91,9 @@ - (void)invalidate
8491
- (void)startObserving
8592
{
8693
hasListeners = YES;
94+
if (didEmit != YES && [self.bridge.performanceLogger valueForTag:RCTPLTTI] != 0) {
95+
[self emitMarks];
96+
}
8797
}
8898

8999
-(void)stopObserving
@@ -99,9 +109,13 @@ - (void)emitTag:(RCTPLTag)tag withNamePrefix:(NSString *)namePrefix
99109
NSLog(@"Ignoring marks prefixed %@ (%lu) as data is unavailable (duration: %lld, end: %lld)", namePrefix, (unsigned long)tag, duration, end);
100110
return;
101111
}
102-
end += getTimestamp() - (CACurrentMediaTime() * 1000);
103-
[self emitMarkNamed:[namePrefix stringByAppendingString:@"Start"] withStartTime:end-duration];
104-
[self emitMarkNamed:[namePrefix stringByAppendingString:@"End"] withStartTime:end];
112+
[self emitMarkNamed:[namePrefix stringByAppendingString:@"Start"] withMediaTime:end-duration];
113+
[self emitMarkNamed:[namePrefix stringByAppendingString:@"End"] withMediaTime:end];
114+
}
115+
116+
- (void)emitMarkNamed:(NSString *)name withMediaTime:(int64_t)mediaTime
117+
{
118+
[self emitMarkNamed:name withStartTime:mediaTime + RNPerformanceGetTimestamp() - (CACurrentMediaTime() * 1000)];
105119
}
106120

107121
- (void)emitMarkNamed:(NSString *)name withStartTime:(int64_t)startTime
@@ -119,7 +133,7 @@ - (void)emitMetricNamed:(NSString *)name withValue:(NSNumber *)value
119133
if (hasListeners) {
120134
[self sendEventWithName:@"metric" body:@{
121135
@"name": name,
122-
@"startTime": @(getTimestamp()),
136+
@"startTime": @(RNPerformanceGetTimestamp()),
123137
@"value": value
124138
}];
125139
}

0 commit comments

Comments
 (0)