react-native/React/Base/RCTPerformanceLogger.m
Tadeu Zagallo 080d3b9f62 [ReactNative] Add PerformanceLogger to measure TTI
Summary:
@public

Add PerformanceLogger to keep track of JS download, initial script execution and
full TTI.

Test Plan:
The Native side currently calls `addTimespans` when it's finish initializing
with the six values (start and end for the three events), so I just checked it
with a `PerformanceLogger.logTimespans()` at the end of the function.

```
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "ScriptDownload: 48ms"
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "ScriptExecution: 106ms"
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "TTI: 293ms"
```
2015-06-19 15:01:35 -08:00

78 lines
1.6 KiB
Objective-C

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <QuartzCore/QuartzCore.h>
#import "RCTPerformanceLogger.h"
#import "RCTRootView.h"
static int64_t RCTPLData[RCTPLSize][2] = {};
void RCTPerformanceLoggerStart(RCTPLTag tag)
{
RCTPLData[tag][0] = CACurrentMediaTime() * 1000;
}
void RCTPerformanceLoggerEnd(RCTPLTag tag)
{
RCTPLData[tag][1] = CACurrentMediaTime() * 1000;
}
NSArray *RCTPerformanceLoggerOutput(void)
{
return @[
@(RCTPLData[0][0]),
@(RCTPLData[0][1]),
@(RCTPLData[1][0]),
@(RCTPLData[1][1]),
@(RCTPLData[2][0]),
@(RCTPLData[2][1]),
];
}
@interface RCTPerformanceLogger : NSObject <RCTBridgeModule>
@end
@implementation RCTPerformanceLogger
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
- (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendTimespans)
name:RCTContentDidAppearNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)sendTimespans
{
[_bridge enqueueJSCall:@"PerformanceLogger.addTimespans" args:@[
RCTPerformanceLoggerOutput(),
@[
@"ScriptDownload",
@"ScriptExecution",
@"TTI",
],
]];
}
@end