forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNFBPerfModule.m
More file actions
216 lines (177 loc) · 6.82 KB
/
Copy pathRNFBPerfModule.m
File metadata and controls
216 lines (177 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import <React/RCTUtils.h>
#import <Firebase/Firebase.h>
#import "RNFBPerfModule.h"
static __strong NSMutableDictionary *traces;
static __strong NSMutableDictionary *httpMetrics;
@implementation RNFBPerfModule
#pragma mark -
#pragma mark Module Setup
RCT_EXPORT_MODULE();
- (instancetype)init {
self = [super init];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
traces = [[NSMutableDictionary alloc] init];
httpMetrics = [[NSMutableDictionary alloc] init];
});
return self;
}
- (void)dealloc {
@synchronized([self class]) {
for (NSString *key in [traces allKeys]) {
[traces removeObjectForKey:key];
}
for (NSString *key in [httpMetrics allKeys]) {
[httpMetrics removeObjectForKey:key];
}
}
}
- (NSDictionary *)constantsToExport {
NSMutableDictionary *constants = [NSMutableDictionary new];
constants[@"isPerformanceCollectionEnabled"] =
@([RCTConvert BOOL:@([FIRPerformance sharedInstance].dataCollectionEnabled)]);
constants[@"isInstrumentationEnabled"] =
@([RCTConvert BOOL:@([FIRPerformance sharedInstance].instrumentationEnabled)]);
return constants;
}
+ (BOOL)requiresMainQueueSetup {
return NO;
}
#pragma mark -
#pragma mark Firebase Perf Methods
RCT_EXPORT_METHOD(setPerformanceCollectionEnabled
: (BOOL)enabled resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
[FIRPerformance sharedInstance].dataCollectionEnabled = (BOOL)enabled;
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(startTrace
: (nonnull NSNumber *)id identifier
: (NSString *)identifier resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
FIRTrace *trace = [[FIRPerformance sharedInstance] traceWithName:identifier];
[trace start];
@synchronized([self class]) {
traces[id] = trace;
}
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(stopTrace
: (nonnull NSNumber *)id traceData
: (NSDictionary *)traceData resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
FIRTrace *trace;
@synchronized([self class]) {
trace = traces[id];
}
NSDictionary *metrics = traceData[@"metrics"];
NSDictionary *attributes = traceData[@"attributes"];
[metrics enumerateKeysAndObjectsUsingBlock:^(NSString *metricName, NSNumber *value, BOOL *stop) {
[trace setIntValue:[value longLongValue] forMetric:metricName];
}];
[attributes
enumerateKeysAndObjectsUsingBlock:^(NSString *attributeName, NSString *value, BOOL *stop) {
[trace setValue:value forAttribute:attributeName];
}];
[trace stop];
@synchronized([self class]) {
[traces removeObjectForKey:id];
}
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(startHttpMetric
: (nonnull NSNumber *)id url
: (NSString *)url httpMethod
: (NSString *)httpMethod resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
FIRHTTPMethod method = FIRHTTPMethodGET;
NSURL *toNSURL = [NSURL URLWithString:url];
if ([httpMethod compare:@"put" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodPUT;
if ([httpMethod compare:@"post" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodPUT;
if ([httpMethod compare:@"head" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodHEAD;
if ([httpMethod compare:@"trace" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodTRACE;
if ([httpMethod compare:@"patch" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodPATCH;
if ([httpMethod compare:@"delete" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodDELETE;
if ([httpMethod compare:@"options" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodOPTIONS;
if ([httpMethod compare:@"connect" options:NSCaseInsensitiveSearch] == NSOrderedSame)
method = FIRHTTPMethodCONNECT;
FIRHTTPMetric *httpMetric = [[FIRHTTPMetric alloc] initWithURL:toNSURL HTTPMethod:method];
[httpMetric start];
@synchronized([self class]) {
httpMetrics[id] = httpMetric;
}
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(stopHttpMetric
: (nonnull NSNumber *)id metricData
: (NSDictionary *)metricData resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
FIRHTTPMetric *httpMetric;
@synchronized([self class]) {
httpMetric = httpMetrics[id];
}
NSDictionary *attributes = metricData[@"attributes"];
[attributes
enumerateKeysAndObjectsUsingBlock:^(NSString *attributeName, NSString *value, BOOL *stop) {
[httpMetric setValue:value forAttribute:attributeName];
}];
if (metricData[@"httpResponseCode"]) {
NSNumber *responseCode = (NSNumber *)metricData[@"httpResponseCode"];
[httpMetric setResponseCode:[responseCode integerValue]];
}
if (metricData[@"requestPayloadSize"]) {
NSNumber *requestPayloadSize = (NSNumber *)metricData[@"requestPayloadSize"];
[httpMetric setRequestPayloadSize:[requestPayloadSize longValue]];
}
if (metricData[@"responsePayloadSize"]) {
NSNumber *responsePayloadSize = (NSNumber *)metricData[@"responsePayloadSize"];
[httpMetric setResponsePayloadSize:[responsePayloadSize longValue]];
}
if (metricData[@"responseContentType"]) {
NSString *responseContentType = (NSString *)metricData[@"responseContentType"];
[httpMetric setResponseContentType:responseContentType];
}
[httpMetric stop];
@synchronized([self class]) {
[httpMetrics removeObjectForKey:id];
}
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(instrumentationEnabled
: (BOOL)enabled resolver
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
[FIRPerformance sharedInstance].instrumentationEnabled = (BOOL)enabled;
resolve([NSNull null]);
}
@end