Ben Copsey

Added starting point for performance tests

This highlights the performance disparity between NSURLConnection and ASIHTTPRequest, which we need to solve :)
@@ -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-18 2009-12-17"; 24 +NSString *ASIHTTPRequestVersion = @"v1.2-19 2009-12-17";
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");
  1 +//
  2 +// PerformanceTests.h
  3 +// Mac
  4 +//
  5 +// Created by Ben Copsey on 17/12/2009.
  6 +// Copyright 2009 All-Seeing Interactive. All rights reserved.
  7 +//
  8 +
  9 +#import <Cocoa/Cocoa.h>
  10 +#import "ASITestCase.h"
  11 +
  12 +@interface PerformanceTests : ASITestCase {
  13 + NSDate *testStartDate;
  14 + int requestsComplete;
  15 + NSMutableArray *responseData;
  16 + unsigned long bytesDownloaded;
  17 +}
  18 +
  19 +- (void)testASIHTTPRequestAsyncPerformance;
  20 +- (void)testNSURLConnectionAsyncPerformance;
  21 +
  22 +@property (retain,nonatomic) NSDate *testStartDate;
  23 +@property (assign,nonatomic) int requestsComplete;
  24 +@property (retain,nonatomic) NSMutableArray *responseData;
  25 +@end
  1 +//
  2 +// PerformanceTests.m
  3 +// Mac
  4 +//
  5 +// Created by Ben Copsey on 17/12/2009.
  6 +// Copyright 2009 All-Seeing Interactive. All rights reserved.
  7 +//
  8 +
  9 +#import "PerformanceTests.h"
  10 +#import "ASIHTTPRequest.h"
  11 +
  12 +@interface NSURLConnectionSubclass : NSURLConnection {
  13 + int tag;
  14 +}
  15 +@property (assign) int tag;
  16 +@end
  17 +@implementation NSURLConnectionSubclass
  18 +@synthesize tag;
  19 +@end
  20 +
  21 +
  22 +@implementation PerformanceTests
  23 +
  24 +- (void)testASIHTTPRequestAsyncPerformance
  25 +{
  26 + bytesDownloaded = 0;
  27 + [self setRequestsComplete:0];
  28 + [self setTestStartDate:[NSDate date]];
  29 + int i;
  30 + for (i=0; i<5; i++) {
  31 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"]];
  32 + [request setDelegate:self];
  33 + [request startAsynchronous];
  34 + }
  35 +}
  36 +
  37 +- (void)requestFailed:(ASIHTTPRequest *)request
  38 +{
  39 + GHFail(@"Cannot proceed with ASIHTTPRequest test - a request failed");
  40 +}
  41 +
  42 +- (void)requestFinished:(ASIHTTPRequest *)request
  43 +{
  44 + bytesDownloaded += [[request responseData] length];
  45 + requestsComplete++;
  46 + if (requestsComplete == 5) {
  47 + NSLog(@"ASIHTTPRequest: Completed 5 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]);
  48 + }
  49 +}
  50 +
  51 +- (void)testNSURLConnectionAsyncPerformance
  52 +{
  53 + [self performSelectorOnMainThread:@selector(startNSURLConnections) withObject:nil waitUntilDone:NO];
  54 +}
  55 +
  56 +- (void)startNSURLConnections
  57 +{
  58 + bytesDownloaded = 0;
  59 + [self setRequestsComplete:0];
  60 + [self setTestStartDate:[NSDate date]];
  61 + [self setResponseData:[NSMutableArray arrayWithCapacity:5]];
  62 +
  63 + int i;
  64 + for (i=0; i<5; i++) {
  65 + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_(abridged).txt"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
  66 + [[self responseData] addObject:[NSMutableData data]];
  67 + NSURLConnectionSubclass *connection = [[[NSURLConnectionSubclass alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease];
  68 + [connection setTag:i];
  69 + }
  70 +}
  71 +
  72 +- (void)connection:(NSURLConnectionSubclass *)connection didReceiveResponse:(NSURLResponse *)response
  73 +{
  74 +}
  75 +
  76 +- (void)connection:(NSURLConnectionSubclass *)connection didFailWithError:(NSError *)error
  77 +{
  78 + GHFail(@"Cannot proceed with NSURLConnection test - a request failed");
  79 +}
  80 +
  81 +- (void)connection:(NSURLConnectionSubclass *)connection didReceiveData:(NSData *)data
  82 +{
  83 + [[[self responseData] objectAtIndex:[connection tag]] appendData:data];
  84 +
  85 +}
  86 +
  87 +- (void)connectionDidFinishLoading:(NSURLConnectionSubclass *)connection
  88 +{
  89 + bytesDownloaded += [[responseData objectAtIndex:[connection tag]] length];
  90 + requestsComplete++;
  91 + if (requestsComplete == 5) {
  92 + NSLog(@"NSURLConnection: Completed 5 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]);
  93 + }
  94 +}
  95 +
  96 +@synthesize requestsComplete;
  97 +@synthesize testStartDate;
  98 +@synthesize responseData;
  99 +@end
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.