Authored by 孙凯

add http 数据结构 review by hongmo

... ... @@ -92,15 +92,13 @@
+ (nullable NSData *)yher_sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse * _Nullable * _Nullable)response error:(NSError **)error
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithCollectionURL:[request.URL absoluteString]];
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:request];
}
NSData *data = [self yher_sendSynchronousRequest:request returningResponse:response error:error];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
if (error) {
[[YH_EventCollector sharedInstance] timeEventEndWithCollectionURL:[request.URL absoluteString] status:YHEventLoadStatusFailed];
}else{
[[YH_EventCollector sharedInstance] timeEventEndWithCollectionURL:[request.URL absoluteString] status:YHEventLoadStatusSuc];
}
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:request response:*response];
}
return data;
}
... ... @@ -111,7 +109,7 @@
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithCollectionURL:[request.URL absoluteString]];
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:request];
}
[self yher_sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
... ... @@ -119,11 +117,7 @@
handler(response,data,connectionError);
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
if (connectionError) {
[[YH_EventCollector sharedInstance] timeEventEndWithCollectionURL:[request.URL absoluteString] status:YHEventLoadStatusFailed];
}else{
[[YH_EventCollector sharedInstance] timeEventEndWithCollectionURL:[request.URL absoluteString] status:YHEventLoadStatusSuc];
}
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:request response:response];
}
}];
}
... ...
... ... @@ -55,22 +55,25 @@
- (NSURLSessionDataTask *)yher_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
{
if (completionHandler == nil) {
return [self yher_dataTaskWithRequest:request completionHandler:completionHandler];
}
return [self yher_dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(data,response,error);
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
if (error) {
[[YH_EventCollector sharedInstance] timeEventEndWithSessionURL:[request.URL absoluteString] status:YHEventLoadStatusFailed];
}else{
[[YH_EventCollector sharedInstance] timeEventEndWithSessionURL:[request.URL absoluteString] status:YHEventLoadStatusSuc];
}
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:request response:response];
}
}];
}
- (NSURLSessionDownloadTask *)yher_downloadTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
{
if (completionHandler == nil) {
return [self yher_downloadTaskWithRequest:request completionHandler:completionHandler];
}
return [self yher_downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
... ... @@ -78,11 +81,7 @@
completionHandler(location,response,error);
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
if (error) {
[[YH_EventCollector sharedInstance] timeEventEndWithSessionURL:[NSString stringWithFormat:@"%@",location] status:YHEventLoadStatusFailed];
}else{
[[YH_EventCollector sharedInstance] timeEventEndWithSessionURL:[NSString stringWithFormat:@"%@",location] status:YHEventLoadStatusSuc];
}
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:request response:response];
}
}];
}
... ... @@ -139,8 +138,7 @@ static inline BOOL yher_addMethod(Class class, SEL selector, Method method) {
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
NSURLSessionTask *task = (NSURLSessionTask*)self;
NSString *url = [task.currentRequest.URL absoluteString];
[[YH_EventCollector sharedInstance] timeEventStartWithSessionURL:url];
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:task.currentRequest];
}
[self yher_resume];
}
... ...
... ... @@ -30,11 +30,8 @@
- (void)timeEventStartWithUIGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;
- (void)timeEventEndWithUIGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;
- (void)timeEventStartWithCollectionURL:(NSString *)collectionURL;
- (void)timeEventEndWithCollectionURL:(NSString *)collectionURL status:(YHEventLoadStatus)status;
- (void)timeEventStartWithSessionURL:(NSString *)sessionURL;
- (void)timeEventEndWithSessionURL:(NSString *)sessionURL status:(YHEventLoadStatus)status;
- (void)timeEventStartWithRequest:(NSURLRequest*)request;
- (void)timeEventEndWithRequest:(NSURLRequest*)request response:(NSURLResponse*)response;
- (void)timeEventEndWithUITableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath;
- (void)timeEventStartWithUITableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath;
... ...
... ... @@ -544,8 +544,9 @@
}
#pragma mark - http performance time
- (void)timeEventStartWithCollectionURL:(NSString *)collectionURL
- (void)timeEventStartWithRequest:(NSURLRequest*)request
{
NSString *collectionURL = [[request URL]absoluteString];
NSNumber *startTime = @([[NSDate date] timeIntervalSince1970]);
NSString *viewId = [NSString stringWithFormat:@"%@",collectionURL];
NSLog(@"startTime = %f",[[NSDate date] timeIntervalSince1970]);
... ... @@ -563,8 +564,9 @@
});
}
- (void)timeEventEndWithCollectionURL:(NSString *)collectionURL status:(YHEventLoadStatus)status
- (void)timeEventEndWithRequest:(NSURLRequest*)request response:(NSURLResponse*)response
{
NSString *collectionURL = [[request URL]absoluteString];
NSTimeInterval elapsedTime = [self eventElapsedTime:collectionURL];
if (elapsedTime==0) {
... ... @@ -576,48 +578,7 @@
});
}
NSDictionary *param = [YH_EventDataFactory factoryTimeEventDataWithHttp:collectionURL ElapsedTime:elapsedTime];
NSLog(@"%@",param);
if (param) {
[[YH_EventCacheManager sharedInstance] pushPerformanceData:param pointName:YHPN_HTTP];
}
}
- (void)timeEventStartWithSessionURL:(NSString *)sessionURL
{
NSNumber *startTime = @([[NSDate date] timeIntervalSince1970]);
NSString *viewId = [NSString stringWithFormat:@"%@",sessionURL];
NSLog(@"startTime = %f",[[NSDate date] timeIntervalSince1970]);
if (startTime==0||IsStrEmpty(viewId)) {
return;
}
if (!sessionURL) {
return;
}
dispatch_async(self.serialQueue, ^{
self.timedEvents[viewId] = startTime;
});
}
- (void)timeEventEndWithSessionURL:(NSString *)sessionURL status:(YHEventLoadStatus)status
{
NSTimeInterval elapsedTime = [self eventElapsedTime:sessionURL];
if (elapsedTime==0) {
return;
}
if (!IsStrEmpty(sessionURL)) {
dispatch_async(self.serialQueue, ^{
[self.timedEvents removeObjectForKey:sessionURL];
});
}
NSDictionary *param = [YH_EventDataFactory factoryTimeEventDataWithHttp:sessionURL ElapsedTime:elapsedTime];
NSDictionary *param = [YH_EventDataFactory factoryTimeEventDataWithRequest:request response:response ElapsedTime:elapsedTime];
NSLog(@"%@",param);
if (param) {
... ...
... ... @@ -165,6 +165,12 @@ typedef NS_ENUM(NSInteger, YHPointType) {
#define kYHEventReportTimeTitle @"title"
#define kYHEventReportTimeURL @"url"
#define kYHEventReportTimeHttpCode @"code"
#define kYHEventReportTimeHttpLength @"length"
#define kYHEventReportTimeHttpMethod @"method"
#define kYHEventReportTimeHttpEi @"ei"
#define kYHEventReportTimeStatus @"status"
... ...
... ... @@ -35,6 +35,6 @@
+(NSString *)getDataType:(YHEventReportTrackDataType)type;
+(NSDictionary *)factoryTimeEventDataWithHttp:(NSString*)url ElapsedTime:(NSTimeInterval)elapsedTime;
+(NSDictionary *)factoryTimeEventDataWithRequest:(NSURLRequest*)request response:(NSURLResponse*)response ElapsedTime:(NSTimeInterval)elapsedTime;
@end
... ...
... ... @@ -106,9 +106,11 @@
}
+(NSDictionary *)factoryTimeEventDataWithHttp:(NSString*)url ElapsedTime:(NSTimeInterval)elapsedTime {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url er_encodedString],kYHEventReportTimeURL,[NSString stringWithFormat:@"%.0f",elapsedTime*1000],kYHEventReportTimeElapsed,@"",kYHEventReportTimeTitle,nil];
+(NSDictionary *)factoryTimeEventDataWithRequest:(NSURLRequest*)request response:(NSURLResponse*)response ElapsedTime:(NSTimeInterval)elapsedTime
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSString *status = [NSString stringWithFormat:@"%ld",(long)[httpResponse statusCode]];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[[request.URL absoluteString] er_encodedString],kYHEventReportTimeURL,[NSString stringWithFormat:@"%.0f",elapsedTime*1000],kYHEventReportTimeElapsed,status,kYHEventReportTimeHttpCode,[NSString stringWithFormat:@"%lld",response.expectedContentLength],kYHEventReportTimeHttpLength,request.HTTPMethod,kYHEventReportTimeHttpMethod,@"",kYHEventReportTimeHttpEi,nil];
return dict;
}
... ... @@ -145,6 +147,9 @@
case YHPN_VIEWCONTROLLER:
str = @"PAGE";
break;
case YHPN_HTTP:
str = @"HTTP";
break;
case YHPN_WEBVIEW:
str = @"WEBVIEW";
break;
... ...