Authored by 王钱钧

添加图片加载性能统计。

... ... @@ -8,6 +8,7 @@
#import "SDWebImageDownloaderOperation+Performance.h"
#import "Aspects.h"
#import "YH_Analytics.h"
@implementation SDWebImageDownloaderOperation (Performance)
... ... @@ -19,6 +20,98 @@
op.begainLoadImageTimestamp = [[NSDate date]timeIntervalSince1970];
} error:NULL];
// finish
[SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationDidFinish) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
SDWebImageDownloaderOperation *op = [aspectInfo instance];
NSTimeInterval currentTS = [[NSDate date]timeIntervalSince1970];
NSInteger status = -1;
long deltaTS = (currentTS - op.begainLoadImageTimestamp) * 1000;
NSString *url = [SDWebImageDownloaderOperation urlWithOperation:op];
NSString *opAddr = [NSString stringWithFormat:@"%p", op];
if (op.response) {
if ([op.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)op.response;
status = [response statusCode];
}
if (status == 200) {
// success
status = 1;
}
[[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeImageLoad
parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
JsonKeyPerformanceURL: url?:@"",
JsonKeyPerformanceTS: @((long)(op.begainLoadImageTimestamp*1000)),
JsonKeyPerformanceCTS:@(deltaTS),
JsonKeyPerformanceSTATUS : @(status)
}];
}
} error:NULL];
// error
[SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationError:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
SDWebImageDownloaderOperation *op = [aspectInfo instance];
NSTimeInterval currentTS = [[NSDate date]timeIntervalSince1970];
long deltaTS = (currentTS - op.begainLoadImageTimestamp) * 1000;
NSString *url = [SDWebImageDownloaderOperation urlWithOperation:op];
NSString *opAddr = [NSString stringWithFormat:@"%p", op];
// error code default is `-1`
NSInteger errorCode = -1;
if (op.error) {
errorCode = [op.error code];
}
[[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeImageLoad
parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
JsonKeyPerformanceURL: url?:@"",
JsonKeyPerformanceTS: @((long)(op.begainLoadImageTimestamp*1000)),
JsonKeyPerformanceCTS:@(deltaTS),
JsonKeyPerformanceSTATUS : @(errorCode)
}];
} error:NULL];
// cancel
// [SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationCancel) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
// SDWebImageDownloaderOperation *op = [aspectInfo instance];
//
// NSTimeInterval currentTS = [[NSDate date]timeIntervalSince1970];
//
// long deltaTS = (currentTS - op.begainLoadImageTimestamp) * 1000;
// NSString *url = [SDWebImageDownloaderOperation urlWithOperation:op];
// NSString *opAddr = [NSString stringWithFormat:@"%p", op];
// NSLog(@"%p \n url = %@ ====cancel===> %ld",op,url,deltaTS);
//
//// [[YH_Analytics sharedInstance]logPerformanceWithType:kPerformanceTypeAPICall
//// parameters:@{JsonKeyPerformanceEvtEID:opAddr?:@"",
//// JsonKeyPerformanceURL: url?:@"",
//// JsonKeyPerformanceTS: @((long)(op.begainLoadImageTimestamp*1000)),
//// JsonKeyPerformanceCTS:@(deltaTS),
//// JsonKeyPerformanceSTATUS : @(0)
//// }];
// } error:NULL];
}
+ (NSString *)urlWithOperation:(SDWebImageDownloaderOperation *)op
{
NSString *url = @"";
if (op && op.request) {
url = [op.request.URL absoluteString];
if ([op.request.HTTPMethod isEqualToString:@"POST"] && ![url containsString:@"http://analytics.yhurl.com/yas_mobile"]) {
NSString *parmeter = [[NSString alloc]initWithData:op.request.HTTPBody encoding:NSUTF8StringEncoding];
url = [url stringByAppendingString:[NSString stringWithFormat:@"/?%@",parmeter]];
}
}
return url;
}
@end
... ...