1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | #import <QuartzCore/QuartzCore.h>
|
9 |
|
10 | #import "RCTPerformanceLogger.h"
|
11 | #import "RCTRootView.h"
|
12 | #import "RCTLog.h"
|
13 | #import "RCTProfile.h"
|
14 |
|
15 | @interface RCTPerformanceLogger ()
|
16 | {
|
17 | int64_t _data[RCTPLSize][2];
|
18 | NSUInteger _cookies[RCTPLSize];
|
19 | }
|
20 |
|
21 | @property (nonatomic, copy) NSArray<NSString *> *labelsForTags;
|
22 |
|
23 | @end
|
24 |
|
25 | @implementation RCTPerformanceLogger
|
26 |
|
27 | - (instancetype)init
|
28 | {
|
29 | if (self = [super init]) {
|
30 | // Keep this in sync with RCTPLTag
|
31 | _labelsForTags = @[
|
32 | @"ScriptDownload",
|
33 | @"ScriptExecution",
|
34 | @"RAMBundleLoad",
|
35 | @"RAMStartupCodeSize",
|
36 | @"RAMStartupNativeRequires",
|
37 | @"RAMStartupNativeRequiresCount",
|
38 | @"RAMNativeRequires",
|
39 | @"RAMNativeRequiresCount",
|
40 | @"NativeModuleInit",
|
41 | @"NativeModuleMainThread",
|
42 | @"NativeModulePrepareConfig",
|
43 | @"NativeModuleMainThreadUsesCount",
|
44 | @"NativeModuleSetup",
|
45 | @"TurboModuleSetup",
|
46 | @"JSCWrapperOpenLibrary",
|
47 | @"BridgeStartup",
|
48 | @"RootViewTTI",
|
49 | @"BundleSize",
|
50 | ];
|
51 | }
|
52 | return self;
|
53 | }
|
54 |
|
55 | - (void)markStartForTag:(RCTPLTag)tag
|
56 | {
|
57 | #if RCT_PROFILE
|
58 | if (RCTProfileIsProfiling()) {
|
59 | NSString *label = _labelsForTags[tag];
|
60 | _cookies[tag] = RCTProfileBeginAsyncEvent(RCTProfileTagAlways, label, nil);
|
61 | }
|
62 | #endif
|
63 | _data[tag][0] = CACurrentMediaTime() * 1000;
|
64 | _data[tag][1] = 0;
|
65 | }
|
66 |
|
67 |
|
68 | - (void)markStopForTag:(RCTPLTag)tag
|
69 | {
|
70 | #if RCT_PROFILE
|
71 | if (RCTProfileIsProfiling()) {
|
72 | NSString *label =_labelsForTags[tag];
|
73 | RCTProfileEndAsyncEvent(RCTProfileTagAlways, @"native", _cookies[tag], label, @"RCTPerformanceLogger");
|
74 | }
|
75 | #endif
|
76 | if (_data[tag][0] != 0 && _data[tag][1] == 0) {
|
77 | _data[tag][1] = CACurrentMediaTime() * 1000;
|
78 | } else {
|
79 | RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
|
80 | }
|
81 | }
|
82 |
|
83 | - (void)setValue:(int64_t)value forTag:(RCTPLTag)tag
|
84 | {
|
85 | _data[tag][0] = 0;
|
86 | _data[tag][1] = value;
|
87 | }
|
88 |
|
89 | - (void)addValue:(int64_t)value forTag:(RCTPLTag)tag
|
90 | {
|
91 | _data[tag][0] = 0;
|
92 | _data[tag][1] += value;
|
93 | }
|
94 |
|
95 | - (void)appendStartForTag:(RCTPLTag)tag
|
96 | {
|
97 | _data[tag][0] = CACurrentMediaTime() * 1000;
|
98 | }
|
99 |
|
100 | - (void)appendStopForTag:(RCTPLTag)tag
|
101 | {
|
102 | if (_data[tag][0] != 0) {
|
103 | _data[tag][1] += CACurrentMediaTime() * 1000 - _data[tag][0];
|
104 | _data[tag][0] = 0;
|
105 | } else {
|
106 | RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
|
107 | }
|
108 | }
|
109 |
|
110 | - (NSArray<NSNumber *> *)valuesForTags
|
111 | {
|
112 | NSMutableArray *result = [NSMutableArray array];
|
113 | for (NSUInteger index = 0; index < RCTPLSize; index++) {
|
114 | [result addObject:@(_data[index][0])];
|
115 | [result addObject:@(_data[index][1])];
|
116 | }
|
117 | return result;
|
118 | }
|
119 |
|
120 | - (int64_t)durationForTag:(RCTPLTag)tag
|
121 | {
|
122 | return _data[tag][1] - _data[tag][0];
|
123 | }
|
124 |
|
125 | - (int64_t)valueForTag:(RCTPLTag)tag
|
126 | {
|
127 | return _data[tag][1];
|
128 | }
|
129 |
|
130 | @end
|