SDWebImageDownloaderOperation+Performance.m
3.69 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
//
// SDWebImageDownloaderOperation+Performance.m
// SDWebImage
//
// Created by 王钱钧 on 15/12/24.
// Copyright © 2015年 Dailymotion. All rights reserved.
//
#import "SDWebImageDownloaderOperation+Performance.h"
#import "Aspects.h"
@implementation SDWebImageDownloaderOperation (Performance)
+ (void)addOperationMonitor
{
// start
[SDWebImageDownloaderOperation aspect_hookSelector:@selector(start) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo>aspectInfo){
SDWebImageDownloaderOperation *op = [aspectInfo instance];
op.begainLoadImageTimestamp = [[NSDate date]timeIntervalSince1970];
} error:NULL];
// finish
[SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationDidFinish) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
SDWebImageDownloaderOperation *op = [aspectInfo instance];
NSInteger status = -1;
if (op.response) {
if ([op.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)op.response;
status = [response statusCode];
}
if (status == 200) {
// success
status = 1;
}
}
} error:NULL];
// error
[SDWebImageDownloaderOperation aspect_hookSelector:@selector(operationError:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo>aspectInfo){
SDWebImageDownloaderOperation *op = [aspectInfo instance];
// error code default is `-1`
NSInteger errorCode = -1;
if (op.error) {
errorCode = [op.error code];
}
} 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];
BOOL containsAnanlyticURL = ([url rangeOfString:@"yas_mobile"].location == NSNotFound) ? NO : YES;
if ([op.request.HTTPMethod isEqualToString:@"POST"] && !containsAnanlyticURL) {
NSString *parmeter = [[NSString alloc]initWithData:op.request.HTTPBody encoding:NSUTF8StringEncoding];
url = [url stringByAppendingString:[NSString stringWithFormat:@"/?%@",parmeter]];
}
}
return url;
}
@end