From d56636e15b2d106f275ae9ea0887a0da04b7024d Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 30 Jan 2020 18:33:16 +0800 Subject: [PATCH] Update the Example and Test case about URLSessionMetrics, expose the API in UIVIew+WebCache to make it easy to write code (or user have to write NSStringFromClass) --- .../SDWebImage Demo/MasterViewController.m | 20 +++++++++--- SDWebImage/Core/UIView+WebCache.h | 7 +++++ Tests/Tests/SDWebImageDownloaderTests.m | 31 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index f131f55f..f3aecbea 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -113,10 +113,22 @@ } cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; - [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] - placeholderImage:placeholderImage - options:indexPath.row == 0 ? SDWebImageRefreshCached : 0 - context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}]; + __weak SDAnimatedImageView *imageView = cell.customImageView; + [imageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] + placeholderImage:placeholderImage + options:indexPath.row == 0 ? SDWebImageRefreshCached : 0 + context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))} + progress:nil + completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { + SDWebImageCombinedOperation *operation = [imageView sd_imageLoadOperationForKey:imageView.sd_latestOperationKey]; + SDWebImageDownloadToken *token = operation.loaderOperation; + if (@available(iOS 10.0, *)) { + NSURLSessionTaskMetrics *metrics = token.metrics; + if (metrics) { + printf("Metrics: %s download in (%f) seconds\n", [imageURL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding], metrics.taskInterval.duration); + } + } + }]; return cell; } diff --git a/SDWebImage/Core/UIView+WebCache.h b/SDWebImage/Core/UIView+WebCache.h index d0a7966f..c7e12a47 100644 --- a/SDWebImage/Core/UIView+WebCache.h +++ b/SDWebImage/Core/UIView+WebCache.h @@ -31,6 +31,13 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima */ @property (nonatomic, strong, readonly, nullable) NSURL *sd_imageURL; +/** + * Get the current image operation key. Operation key is used to identify the different queries for one view instance (like UIButton). + * See more about this in `SDWebImageContextSetImageOperationKey`. + * @note You can use method `UIView+WebCacheOperation` to invesigate different queries' operation. + */ +@property (nonatomic, strong, readonly, nullable) NSString *sd_latestOperationKey; + /** * The current image loading progress associated to the view. The unit count is the received size and excepted size of download. * The `totalUnitCount` and `completedUnitCount` will be reset to 0 after a new image loading start (change from current queue). And they will be set to `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue). diff --git a/Tests/Tests/SDWebImageDownloaderTests.m b/Tests/Tests/SDWebImageDownloaderTests.m index e5f0a2b5..467623ac 100644 --- a/Tests/Tests/SDWebImageDownloaderTests.m +++ b/Tests/Tests/SDWebImageDownloaderTests.m @@ -642,6 +642,37 @@ }]; } +- (void)test26DownloadURLSessionMetrics { + XCTestExpectation *expectation1 = [self expectationWithDescription:@"Download URLSessionMetrics works"]; + + SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init]; + + __block SDWebImageDownloadToken *token; + token = [downloader downloadImageWithURL:[NSURL URLWithString:kTestJPEGURL] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { + expect(error).beNil(); + if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, *)) { + NSURLSessionTaskMetrics *metrics = token.metrics; + expect(metrics).notTo.beNil(); + expect(metrics.redirectCount).equal(0); + expect(metrics.transactionMetrics.count).equal(1); + NSURLSessionTaskTransactionMetrics *metric = metrics.transactionMetrics.firstObject; + // Metrcis Test + expect(metric.fetchStartDate).notTo.beNil(); + expect(metric.connectStartDate).notTo.beNil(); + expect(metric.connectEndDate).notTo.beNil(); + expect(metric.networkProtocolName).equal(@"http/1.1"); + expect(metric.resourceFetchType).equal(NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad); + expect(metric.isProxyConnection).beFalsy(); + expect(metric.isReusedConnection).beFalsy(); + } + [expectation1 fulfill]; + }]; + + [self waitForExpectationsWithCommonTimeoutUsingHandler:^(NSError * _Nullable error) { + [downloader invalidateSessionAndCancel:YES]; + }]; +} + #pragma mark - SDWebImageLoader - (void)test30CustomImageLoaderWorks { XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];