Showing
3 changed files
with
138 additions
and
9 deletions
| @@ -21,7 +21,7 @@ | @@ -21,7 +21,7 @@ | ||
| 21 | #import "ASIInputStream.h" | 21 | #import "ASIInputStream.h" |
| 22 | 22 | ||
| 23 | // Automatically set on build | 23 | // Automatically set on build |
| 24 | -NSString *ASIHTTPRequestVersion = @"v1.2-24 2009-12-18"; | 24 | +NSString *ASIHTTPRequestVersion = @"v1.2-25 2010-01-06"; |
| 25 | 25 | ||
| 26 | // We use our own custom run loop mode as CoreAnimation seems to want to hijack our threads otherwise | 26 | // We use our own custom run loop mode as CoreAnimation seems to want to hijack our threads otherwise |
| 27 | static CFStringRef ASIHTTPRequestRunMode = CFSTR("ASIHTTPRequest"); | 27 | static CFStringRef ASIHTTPRequestRunMode = CFSTR("ASIHTTPRequest"); |
| @@ -10,6 +10,8 @@ | @@ -10,6 +10,8 @@ | ||
| 10 | #import "ASITestCase.h" | 10 | #import "ASITestCase.h" |
| 11 | 11 | ||
| 12 | @interface PerformanceTests : ASITestCase { | 12 | @interface PerformanceTests : ASITestCase { |
| 13 | + NSURL *testURL; | ||
| 14 | + | ||
| 13 | NSDate *testStartDate; | 15 | NSDate *testStartDate; |
| 14 | int requestsComplete; | 16 | int requestsComplete; |
| 15 | NSMutableArray *responseData; | 17 | NSMutableArray *responseData; |
| @@ -19,6 +21,7 @@ | @@ -19,6 +21,7 @@ | ||
| 19 | - (void)testASIHTTPRequestAsyncPerformance; | 21 | - (void)testASIHTTPRequestAsyncPerformance; |
| 20 | - (void)testNSURLConnectionAsyncPerformance; | 22 | - (void)testNSURLConnectionAsyncPerformance; |
| 21 | 23 | ||
| 24 | +@property (retain,nonatomic) NSURL *testURL; | ||
| 22 | @property (retain,nonatomic) NSDate *testStartDate; | 25 | @property (retain,nonatomic) NSDate *testStartDate; |
| 23 | @property (assign,nonatomic) int requestsComplete; | 26 | @property (assign,nonatomic) int requestsComplete; |
| 24 | @property (retain,nonatomic) NSMutableArray *responseData; | 27 | @property (retain,nonatomic) NSMutableArray *responseData; |
| @@ -9,6 +9,8 @@ | @@ -9,6 +9,8 @@ | ||
| 9 | #import "PerformanceTests.h" | 9 | #import "PerformanceTests.h" |
| 10 | #import "ASIHTTPRequest.h" | 10 | #import "ASIHTTPRequest.h" |
| 11 | 11 | ||
| 12 | +// IMPORTANT - these tests need to be run one at a time! | ||
| 13 | + | ||
| 12 | @interface NSURLConnectionSubclass : NSURLConnection { | 14 | @interface NSURLConnectionSubclass : NSURLConnection { |
| 13 | int tag; | 15 | int tag; |
| 14 | } | 16 | } |
| @@ -21,19 +23,142 @@ | @@ -21,19 +23,142 @@ | ||
| 21 | 23 | ||
| 22 | @implementation PerformanceTests | 24 | @implementation PerformanceTests |
| 23 | 25 | ||
| 26 | +- (void)setUp | ||
| 27 | +{ | ||
| 28 | + [self setTestURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_%28abridged%29.txt"]]; | ||
| 29 | + //[self setTestURL:[NSURL URLWithString:@"http://allseeing-i.com"]]; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +- (void)testASIHTTPRequestSynchronousPerformance | ||
| 33 | +{ | ||
| 34 | + [self performSelectorOnMainThread:@selector(runSynchronousASIHTTPRequests) withObject:nil waitUntilDone:YES]; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | + | ||
| 38 | +- (void)runSynchronousASIHTTPRequests | ||
| 39 | +{ | ||
| 40 | + int runTimes = 10; | ||
| 41 | + NSTimeInterval times[runTimes]; | ||
| 42 | + int i; | ||
| 43 | + for (i=0; i<runTimes; i++) { | ||
| 44 | + NSDate *startTime = [NSDate date]; | ||
| 45 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:testURL]; | ||
| 46 | + //Send the same headers as NSURLRequest | ||
| 47 | + [request addRequestHeader:@"Pragma" value:@"no-cache"]; | ||
| 48 | + [request addRequestHeader:@"Accept" value:@"*/*"]; | ||
| 49 | + [request addRequestHeader:@"Accept-Language" value:@"en/us"]; | ||
| 50 | + [request start]; | ||
| 51 | + if ([request error]) { | ||
| 52 | + NSLog(@"Request failed - cannot proceed with test"); | ||
| 53 | + return; | ||
| 54 | + } | ||
| 55 | + times[i] = [[NSDate date] timeIntervalSinceDate:startTime]; | ||
| 56 | + } | ||
| 57 | + NSTimeInterval bestTime = 1000; | ||
| 58 | + NSTimeInterval worstTime = 0; | ||
| 59 | + NSTimeInterval totalTime = 0; | ||
| 60 | + for (i=0; i<runTimes; i++) { | ||
| 61 | + if (times[i] < bestTime) { | ||
| 62 | + bestTime = times[i]; | ||
| 63 | + } | ||
| 64 | + if (times[i] > worstTime) { | ||
| 65 | + worstTime = times[i]; | ||
| 66 | + } | ||
| 67 | + totalTime += times[i]; | ||
| 68 | + } | ||
| 69 | + NSLog(@"Ran %hi requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime); | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | + | ||
| 73 | +- (void)testNSURLConnectionSynchronousPerformance | ||
| 74 | +{ | ||
| 75 | + [self performSelectorOnMainThread:@selector(runSynchronousNSURLConnections) withObject:nil waitUntilDone:YES]; | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | + | ||
| 79 | +- (void)runSynchronousNSURLConnections | ||
| 80 | +{ | ||
| 81 | + int runTimes = 10; | ||
| 82 | + NSTimeInterval times[runTimes]; | ||
| 83 | + int i; | ||
| 84 | + for (i=0; i<runTimes; i++) { | ||
| 85 | + NSDate *startTime = [NSDate date]; | ||
| 86 | + | ||
| 87 | + NSURLResponse *response = nil; | ||
| 88 | + NSError *error = nil; | ||
| 89 | + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; | ||
| 90 | + [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; | ||
| 91 | + if (error) { | ||
| 92 | + NSLog(@"Request failed - cannot proceed with test"); | ||
| 93 | + return; | ||
| 94 | + } | ||
| 95 | + times[i] = [[NSDate date] timeIntervalSinceDate:startTime]; | ||
| 96 | + } | ||
| 97 | + NSTimeInterval bestTime = 1000; | ||
| 98 | + NSTimeInterval worstTime = 0; | ||
| 99 | + NSTimeInterval totalTime = 0; | ||
| 100 | + for (i=0; i<runTimes; i++) { | ||
| 101 | + if (times[i] < bestTime) { | ||
| 102 | + bestTime = times[i]; | ||
| 103 | + } | ||
| 104 | + if (times[i] > worstTime) { | ||
| 105 | + worstTime = times[i]; | ||
| 106 | + } | ||
| 107 | + totalTime += times[i]; | ||
| 108 | + } | ||
| 109 | + NSLog(@"Ran %hi requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime); | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | + | ||
| 24 | - (void)testASIHTTPRequestAsyncPerformance | 113 | - (void)testASIHTTPRequestAsyncPerformance |
| 25 | { | 114 | { |
| 115 | + [self performSelectorOnMainThread:@selector(startASIHTTPRequests) withObject:nil waitUntilDone:NO]; | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +- (void)testQueuedASIHTTPRequestAsyncPerformance | ||
| 119 | +{ | ||
| 120 | + [self performSelectorOnMainThread:@selector(startASIHTTPRequestsWithQueue) withObject:nil waitUntilDone:NO]; | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | + | ||
| 124 | +- (void)startASIHTTPRequests | ||
| 125 | +{ | ||
| 26 | bytesDownloaded = 0; | 126 | bytesDownloaded = 0; |
| 27 | [self setRequestsComplete:0]; | 127 | [self setRequestsComplete:0]; |
| 28 | [self setTestStartDate:[NSDate date]]; | 128 | [self setTestStartDate:[NSDate date]]; |
| 29 | int i; | 129 | int i; |
| 30 | - for (i=0; i<5; i++) { | 130 | + for (i=0; i<10; i++) { |
| 31 | - ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]]; | 131 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:testURL]; |
| 132 | + //Send the same headers as NSURLRequest | ||
| 133 | + [request addRequestHeader:@"Pragma" value:@"no-cache"]; | ||
| 134 | + [request addRequestHeader:@"Accept" value:@"*/*"]; | ||
| 135 | + [request addRequestHeader:@"Accept-Language" value:@"en/us"]; | ||
| 32 | [request setDelegate:self]; | 136 | [request setDelegate:self]; |
| 33 | [request startAsynchronous]; | 137 | [request startAsynchronous]; |
| 34 | } | 138 | } |
| 35 | } | 139 | } |
| 36 | 140 | ||
| 141 | +- (void)startASIHTTPRequestsWithQueue | ||
| 142 | +{ | ||
| 143 | + bytesDownloaded = 0; | ||
| 144 | + [self setRequestsComplete:0]; | ||
| 145 | + [self setTestStartDate:[NSDate date]]; | ||
| 146 | + int i; | ||
| 147 | + NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; | ||
| 148 | + [queue setMaxConcurrentOperationCount:4]; | ||
| 149 | + for (i=0; i<10; i++) { | ||
| 150 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:testURL]; | ||
| 151 | + //Send the same headers as NSURLRequest | ||
| 152 | + [request addRequestHeader:@"Pragma" value:@"no-cache"]; | ||
| 153 | + [request addRequestHeader:@"Accept" value:@"*/*"]; | ||
| 154 | + [request addRequestHeader:@"Accept-Language" value:@"en/us"]; | ||
| 155 | + [request setUseCookiePersistance:NO]; | ||
| 156 | + [request setUseSessionPersistance:NO]; | ||
| 157 | + [request setDelegate:self]; | ||
| 158 | + [queue addOperation:request]; | ||
| 159 | + } | ||
| 160 | +} | ||
| 161 | + | ||
| 37 | - (void)requestFailed:(ASIHTTPRequest *)request | 162 | - (void)requestFailed:(ASIHTTPRequest *)request |
| 38 | { | 163 | { |
| 39 | GHFail(@"Cannot proceed with ASIHTTPRequest test - a request failed"); | 164 | GHFail(@"Cannot proceed with ASIHTTPRequest test - a request failed"); |
| @@ -43,8 +168,8 @@ | @@ -43,8 +168,8 @@ | ||
| 43 | { | 168 | { |
| 44 | bytesDownloaded += [[request responseData] length]; | 169 | bytesDownloaded += [[request responseData] length]; |
| 45 | requestsComplete++; | 170 | requestsComplete++; |
| 46 | - if (requestsComplete == 5) { | 171 | + if (requestsComplete == 10) { |
| 47 | - NSLog(@"ASIHTTPRequest: Completed 5 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]); | 172 | + NSLog(@"ASIHTTPRequest: Completed 10 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]); |
| 48 | } | 173 | } |
| 49 | } | 174 | } |
| 50 | 175 | ||
| @@ -61,8 +186,8 @@ | @@ -61,8 +186,8 @@ | ||
| 61 | [self setResponseData:[NSMutableArray arrayWithCapacity:5]]; | 186 | [self setResponseData:[NSMutableArray arrayWithCapacity:5]]; |
| 62 | 187 | ||
| 63 | int i; | 188 | int i; |
| 64 | - for (i=0; i<5; i++) { | 189 | + for (i=0; i<10; i++) { |
| 65 | - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; | 190 | + NSURLRequest *request = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; |
| 66 | [[self responseData] addObject:[NSMutableData data]]; | 191 | [[self responseData] addObject:[NSMutableData data]]; |
| 67 | NSURLConnectionSubclass *connection = [[[NSURLConnectionSubclass alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease]; | 192 | NSURLConnectionSubclass *connection = [[[NSURLConnectionSubclass alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease]; |
| 68 | [connection setTag:i]; | 193 | [connection setTag:i]; |
| @@ -88,11 +213,12 @@ | @@ -88,11 +213,12 @@ | ||
| 88 | { | 213 | { |
| 89 | bytesDownloaded += [[responseData objectAtIndex:[connection tag]] length]; | 214 | bytesDownloaded += [[responseData objectAtIndex:[connection tag]] length]; |
| 90 | requestsComplete++; | 215 | requestsComplete++; |
| 91 | - if (requestsComplete == 5) { | 216 | + if (requestsComplete == 10) { |
| 92 | - NSLog(@"NSURLConnection: Completed 5 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]); | 217 | + NSLog(@"NSURLConnection: Completed 10 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]); |
| 93 | } | 218 | } |
| 94 | } | 219 | } |
| 95 | 220 | ||
| 221 | +@synthesize testURL; | ||
| 96 | @synthesize requestsComplete; | 222 | @synthesize requestsComplete; |
| 97 | @synthesize testStartDate; | 223 | @synthesize testStartDate; |
| 98 | @synthesize responseData; | 224 | @synthesize responseData; |
-
Please register or login to post a comment