-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: concurrency and performance tests
Change-Id: I2c9532de20727306c8ac595905382da96f949ff0
- Loading branch information
liujl
committed
Aug 4, 2016
1 parent
a872406
commit e98c18f
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ DerivedData | |
*.perspectivev3 | ||
!default.perspectivev3 | ||
xcuserdata | ||
xcbaselines | ||
|
||
## Other | ||
*.xccheckout | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// YTKRequestConcurrencyTest.m | ||
// YTKNetwork | ||
// | ||
// Created by skyline on 16/8/3. | ||
// Copyright © 2016年 skyline. All rights reserved. | ||
// | ||
|
||
#import "YTKTestCase.h" | ||
#import "YTKBasicHTTPRequest.h" | ||
#import "YTKNetworkPrivate.h" | ||
|
||
@interface YTKConcurrencyTest : YTKTestCase | ||
|
||
@end | ||
|
||
@implementation YTKConcurrencyTest | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
} | ||
|
||
- (void)tearDown { | ||
[super tearDown]; | ||
} | ||
|
||
- (void)testBasicConcurrentRequestCreation { | ||
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); | ||
|
||
NSInteger dispatchTarget = 1000; | ||
__block NSInteger completionCount = 0; | ||
for (NSUInteger i = 0; i < dispatchTarget; i++) { | ||
dispatch_async(queue, ^{ | ||
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init]; | ||
[req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) { | ||
NSNumber *result = request.responseObject; | ||
XCTAssertTrue([result isEqualToNumber:@(i)]); | ||
} failure:nil]; | ||
// We just need to simulate concurrent request creation here. | ||
[req.requestTask cancel]; | ||
|
||
YTKBaseRequest *mockedSuccessResult = [[YTKBaseRequest alloc] init]; | ||
mockedSuccessResult.responseObject = @(i); | ||
req.successCompletionBlock(mockedSuccessResult); | ||
|
||
NSLog(@"Current req number: %zd", i); | ||
dispatch_sync(dispatch_get_main_queue(), ^{ | ||
completionCount++; | ||
}); | ||
}); | ||
} | ||
|
||
while (completionCount < dispatchTarget) { | ||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// YTKPerformanceTests.m | ||
// YTKNetwork | ||
// | ||
// Created by skyline on 16/8/3. | ||
// Copyright © 2016年 skyline. All rights reserved. | ||
// | ||
|
||
#import "YTKTestCase.h" | ||
#import "YTKBasicHTTPRequest.h" | ||
|
||
@interface YTKPerformanceTests : YTKTestCase | ||
|
||
@end | ||
|
||
@implementation YTKPerformanceTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
} | ||
|
||
- (void)tearDown { | ||
[super tearDown]; | ||
} | ||
|
||
- (void)testBaseRequestCreationPerformance { | ||
NSInteger targetCount = 1000; | ||
// The measure block will be called several times. | ||
[self measureBlock:^{ | ||
for (NSUInteger i = 0; i < targetCount; i++) { | ||
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init]; | ||
[req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) { | ||
NSNumber *result = request.responseObject; | ||
XCTAssertTrue([result isEqualToNumber:@(i)]); | ||
} failure:nil]; | ||
[req.requestTask cancel]; | ||
} | ||
}]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters