Created
March 21, 2017 12:25
-
-
Save jamiebullock/33570362da88d04621abe2ae2d444a9c to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
@interface SimpleURL : NSObject<NSURLSessionDataDelegate> { | |
NSURLSession *session; | |
dispatch_semaphore_t semaphore; | |
} | |
-(void)connectWithDelegate:(NSString *)url; | |
-(void)connectWithCompletionHandler:(NSString *)url; | |
-(void)handleResponse:(NSURLResponse *)response; | |
@end | |
@implementation SimpleURL | |
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask | |
didReceiveResponse:(NSURLResponse *)response | |
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler | |
{ | |
completionHandler(NSURLSessionResponseAllow); | |
dispatch_semaphore_signal(semaphore); | |
} | |
-(void)connectWithDelegate:(NSString *)url | |
{ | |
semaphore = dispatch_semaphore_create(0); | |
session = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration] | |
delegate: self | |
delegateQueue: nil]; | |
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: url] | |
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData | |
timeoutInterval: 60.0]; | |
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest: req]; | |
[dataTask resume]; | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
} | |
- (void)connectWithCompletionHandler:(NSString *)url | |
{ | |
semaphore = dispatch_semaphore_create(0); | |
session = [NSURLSession sharedSession]; | |
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: url] | |
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData | |
timeoutInterval: 60.0]; | |
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:req completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error){ | |
if (!error) | |
dispatch_semaphore_signal(semaphore); | |
}]; | |
[dataTask resume]; | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
} | |
@end | |
int main(int argc, const char * argv[]) | |
{ | |
SimpleURL *simple = [[SimpleURL alloc] init]; | |
double count = 0.0; | |
int iterations = 20; | |
NSString *url = @"https://s3.amazonaws.com"; | |
for (int i = 0; i < iterations; ++i) | |
{ | |
NSLog(@"****** Running test %d ******", i); | |
NSDate *start = [NSDate date]; | |
[simple connectWithCompletionHandler:url]; | |
NSTimeInterval timeInterval = [start timeIntervalSinceNow]; | |
NSLog(@"Time: %f", fabs(timeInterval)); | |
count += timeInterval; | |
} | |
NSLog(@"AVERAGE: %f", fabs(count) / (double)iterations); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment