Skip to content

Commit

Permalink
Add information collection function
Browse files Browse the repository at this point in the history
  • Loading branch information
wlll129 authored and huiguangjun committed Apr 29, 2024
1 parent 5a4f0d9 commit 2f62562
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions AliyunOSSSDK/OSSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ - (instancetype)initWithEndpoint:(NSString *)endpoint
netConf.maxConcurrentRequestCount = conf.maxConcurrentRequestCount;
netConf.enableFollowRedirects = conf.isFollowRedirectsEnable;
netConf.HTTPMaximumConnectionsPerHost = conf.HTTPMaximumConnectionsPerHost;
netConf.enableNetworkMetricInfo = conf.isAllowNetworkMetricInfo;
}
self.networking = [[OSSNetworking alloc] initWithConfiguration:netConf];
}
Expand Down
1 change: 1 addition & 0 deletions AliyunOSSSDK/OSSDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define OSSServerErrorDomain @"com.aliyun.oss.serverError"

#define OSSErrorMessageTOKEN @"ErrorMessage"
#define OSSNetworkTaskMetrics @"NetworkTaskMetrics"

#define OSSHttpHeaderContentDisposition @"Content-Disposition"
#define OSSHttpHeaderXOSSCallback @"x-oss-callback"
Expand Down
3 changes: 3 additions & 0 deletions AliyunOSSSDK/OSSModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ Sets the session Id for background file transmission
/// https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1407597-httpmaximumconnectionsperhost
@property (nonatomic, assign) uint32_t HTTPMaximumConnectionsPerHost;

/// Set whether to allow metric information
@property (nonatomic, assign) BOOL isAllowNetworkMetricInfo;

@end

@protocol OSSRequestInterceptor <NSObject>
Expand Down
1 change: 1 addition & 0 deletions AliyunOSSSDK/OSSModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ - (instancetype)init {
self.isFollowRedirectsEnable = YES;
// When the value <= 0, do not set HTTPMaximumConnectionsPerHost and use the default value of NSURLSessionConfiguration
self.HTTPMaximumConnectionsPerHost = 0;
self.isAllowNetworkMetricInfo = NO;
}
return self;
}
Expand Down
1 change: 1 addition & 0 deletions AliyunOSSSDK/OSSNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@property (nonatomic, strong) NSString * proxyHost;
@property (nonatomic, strong) NSNumber * proxyPort;
@property (nonatomic, assign) BOOL enableFollowRedirects;
@property (nonatomic, assign) BOOL enableNetworkMetricInfo;
@property (nonatomic, assign) uint32_t HTTPMaximumConnectionsPerHost;
@end

Expand Down
22 changes: 20 additions & 2 deletions AliyunOSSSDK/OSSNetworking.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ - (OSSTask *)sendRequest:(OSSNetworkingRequestDelegate *)request {
// 1.判断是否出错,如果出错的话,直接设置错误信息
if (error)
{
[taskCompletionSource setError:error];
if (weakRequest.metrics) {
NSMutableDictionary *userInfo = error.userInfo ? [NSMutableDictionary dictionaryWithDictionary:error.userInfo] : [NSMutableDictionary dictionary];
[userInfo oss_setObject:weakRequest.metrics forKey:OSSNetworkTaskMetrics];
[taskCompletionSource setError:[NSError errorWithDomain:error.domain
code:error.code
userInfo:userInfo]];
} else {
[taskCompletionSource setError:error];
}
}else
{
[self checkForCrc64WithResult:responseObject
Expand Down Expand Up @@ -379,7 +387,9 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)sessionTask

[self dataTaskWithDelegate:delegate];
} else {
delegate.completionHandler([delegate.responseParser constructResultObject], nil);
OSSResult *result = [delegate.responseParser constructResultObject];
result.metrics = delegate.metrics;
delegate.completionHandler(result, nil);
}
return nil;
}];
Expand Down Expand Up @@ -440,6 +450,14 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
}
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics {
if (self.configuration.enableNetworkMetricInfo) {
OSSNetworkingRequestDelegate *delegate = [self.sessionDelagateManager objectForKey:@(task.taskIdentifier)];
delegate.metrics = metrics;
OSSLogDebug(@"%@", metrics);
}
}

#pragma mark - NSURLSessionDataDelegate Methods

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
Expand Down
2 changes: 2 additions & 0 deletions AliyunOSSSDK/OSSNetworkingRequestDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

@property (atomic, strong) NSURLSessionDataTask *currentSessionTask;

@property (nonatomic, strong) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macos(10.12), ios(10.0));

@property (nonatomic, copy) OSSNetworkingUploadProgressBlock uploadProgress;
@property (nonatomic, copy) OSSNetworkingDownloadProgressBlock downloadProgress;
@property (nonatomic, copy) OSSNetworkingRetryBlock retryCallback;
Expand Down
3 changes: 3 additions & 0 deletions AliyunOSSSDK/OSSResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@
*/
@property (nonatomic, copy) NSString *localCRC64ecma;

/// statistics information for the task.
@property (nonatomic, strong) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macos(10.12), ios(10.0));

@end
54 changes: 54 additions & 0 deletions AliyunOSSiOSTests/OSSConfigurationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>
#import <AliyunOSSiOS/AliyunOSSiOS.h>
#import "OSSTestMacros.h"
#import "OSSTestUtils.h"

@interface OSSConfigurationTest : XCTestCase {
NSString *host;
Expand Down Expand Up @@ -149,4 +150,57 @@ - (void)testCustomPathPrefixEnableWithNullObject {
}] waitUntilFinished];
}

- (void)testAllowNetworkMetricInfo {
OSSClientConfiguration *config = [OSSClientConfiguration new];
OSSAuthCredentialProvider *credentialProvider = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:OSS_STSTOKEN_URL];
OSSClient *client = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credentialProvider clientConfiguration:config];

NSString *privateBucketName = [OSSTestUtils getBucketName];
OSSCreateBucketRequest *createBucket = [OSSCreateBucketRequest new];
createBucket.bucketName = privateBucketName;
OSSTask *task = [client createBucket:createBucket];
[task waitUntilFinished];
XCTAssertNil(((OSSResult *)task.result).metrics);

OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = privateBucketName;
put.objectKey = OSS_IMAGE_KEY;
put.uploadingFileURL = [[NSBundle mainBundle] URLForResource:@"hasky" withExtension:@"jpeg"];
task = [client putObject:put];
[task waitUntilFinished];
XCTAssertNil(((OSSResult *)task.result).metrics);

OSSGetObjectRequest *get = [OSSGetObjectRequest new];
get.bucketName = privateBucketName;
get.objectKey = @"error";
get.onRecieveData = ^(NSData * _Nonnull data) {
};
task = [client getObject:get];
[task waitUntilFinished];
XCTAssertNil(task.error.userInfo[OSSNetworkTaskMetrics]);

config = [OSSClientConfiguration new];
config.isAllowNetworkMetricInfo = YES;
client = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credentialProvider clientConfiguration:config];

put = [OSSPutObjectRequest new];
put.bucketName = privateBucketName;
put.objectKey = OSS_IMAGE_KEY;
put.uploadingFileURL = [[NSBundle mainBundle] URLForResource:@"hasky" withExtension:@"jpeg"];
task = [client putObject:put];
[task waitUntilFinished];
XCTAssertNotNil(((OSSResult *)task.result).metrics);

get = [OSSGetObjectRequest new];
get.bucketName = privateBucketName;
get.objectKey = @"error";
get.onRecieveData = ^(NSData * _Nonnull data) {
};
task = [client getObject:get];
[task waitUntilFinished];
XCTAssertNotNil(task.error.userInfo[OSSNetworkTaskMetrics]);

[OSSTestUtils cleanBucket:privateBucketName with:client];
}

@end
1 change: 1 addition & 0 deletions AliyunOSSiOSTests/OSSTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@interface OSSTestUtils : NSObject
+ (void)cleanBucket: (NSString *)bucket with: (OSSClient *)client;
+ (void) putTestDataWithKey: (NSString *)key withClient: (OSSClient *)client withBucket: (NSString *)bucket;
+ (NSString *)getBucketName;
@end

@interface OSSProgressTestUtils : NSObject
Expand Down
4 changes: 4 additions & 0 deletions AliyunOSSiOSTests/OSSTestUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ + (void) putTestDataWithKey: (NSString *)key withClient: (OSSClient *)client wit
[task waitUntilFinished];
}

+ (NSString *)getBucketName {
return [NSString stringWithFormat:@"bucket-%ld", @([NSDate date].timeIntervalSince1970).integerValue];
}

@end

@interface OSSProgressTestUtils()
Expand Down

0 comments on commit 2f62562

Please sign in to comment.