From cd7bc33e901ca84efa53dd5a26c837b5918a3b10 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sat, 27 Jun 2009 14:27:10 +0000 Subject: Improve custom query timing and display: - Amend timing using clock() to timing based on mach_absolute_time() (see revision comment on Google code) - Ensure the time returned is for the requested query and not subsequent helper queries - Use NSNumberFormatter to return a localised "< 0.1 ms" rather than the hardcoded Euro-style "< 0,1 ms" --- Source/CMMCPConnection.h | 4 ++-- Source/CMMCPConnection.m | 12 ++++++++---- Source/CustomQuery.m | 10 +++++----- Source/SPStringAdditions.h | 2 +- Source/SPStringAdditions.m | 10 +++++----- 5 files changed, 21 insertions(+), 17 deletions(-) (limited to 'Source') diff --git a/Source/CMMCPConnection.h b/Source/CMMCPConnection.h index cebc3c50..7c6fc4d0 100644 --- a/Source/CMMCPConnection.h +++ b/Source/CMMCPConnection.h @@ -60,7 +60,7 @@ BOOL useKeepAlive; float keepAliveInterval; - int lastQueryExecutionTime; + double lastQueryExecutionTime; NSString *lastQueryErrorMessage; unsigned int lastQueryErrorId; my_ulonglong lastQueryAffectedRows; @@ -106,7 +106,7 @@ - (BOOL) selectDB:(NSString *) dbName; - (CMMCPResult *) queryString:(NSString *) query; - (CMMCPResult *) queryString:(NSString *) query usingEncoding:(NSStringEncoding) encoding; -- (float) lastQueryExecutionTime; +- (double) lastQueryExecutionTime; - (MCPResult *) listDBsLike:(NSString *) dbsName; - (BOOL) checkConnection; - (void) restoreConnectionDetails; diff --git a/Source/CMMCPConnection.m b/Source/CMMCPConnection.m index afba40a9..b9bffd9b 100644 --- a/Source/CMMCPConnection.m +++ b/Source/CMMCPConnection.m @@ -27,6 +27,7 @@ #import "SPStringAdditions.h" #include #include +#include static jmp_buf pingTimeoutJumpLocation; static void forcePingTimeout(int signalNumber); @@ -742,7 +743,8 @@ static void forcePingTimeout(int signalNumber); - (CMMCPResult *)queryString:(NSString *) query usingEncoding:(NSStringEncoding) encoding { CMMCPResult *theResult = nil; - int queryStartTime; + uint64_t queryStartTime, queryExecutionTime_t; + Nanoseconds queryExecutionTime; const char *theCQuery; unsigned long theCQueryLength; int queryResultCode; @@ -825,9 +827,10 @@ static void forcePingTimeout(int signalNumber); // Run (or re-run) the query, timing the execution time of the query - note // that this time will include network lag. - queryStartTime = clock(); + queryStartTime = mach_absolute_time(); queryResultCode = mysql_real_query(mConnection, theCQuery, theCQueryLength); - lastQueryExecutionTime = (clock() - queryStartTime); + queryExecutionTime_t = mach_absolute_time() - queryStartTime; + queryExecutionTime = AbsoluteToNanoseconds( *(AbsoluteTime *) &(queryExecutionTime_t) ); // On success, capture the results if (0 == queryResultCode) { @@ -876,6 +879,7 @@ static void forcePingTimeout(int signalNumber); [self setLastErrorMessage:queryErrorMessage?queryErrorMessage:@""]; if (queryErrorMessage) [queryErrorMessage release]; lastQueryAffectedRows = queryAffectedRows; + lastQueryExecutionTime = ((double) UnsignedWideToUInt64( queryExecutionTime )) * 1e-9; // If an error occurred, inform the delegate if (queryResultCode & delegateResponseToWillQueryString) @@ -892,7 +896,7 @@ static void forcePingTimeout(int signalNumber); * Return the time taken to execute the last query. This should be close to the time it took * the server to run the query, but will include network lag and some client library overhead. */ -- (float) lastQueryExecutionTime +- (double) lastQueryExecutionTime { return lastQueryExecutionTime; } diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 6bb65056..0b18848c 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -396,7 +396,7 @@ NSMutableString *errors = [NSMutableString string]; int i, totalQueriesRun = 0, totalAffectedRows = 0; - float executionTime = 0; + double executionTime = 0; int firstErrorOccuredInQuery = -1; BOOL suppressErrorSheet = NO; BOOL tableListNeedsReload = NO; @@ -593,26 +593,26 @@ if (totalAffectedRows==1) { [affectedRowsText setStringValue:[NSString stringWithFormat:NSLocalizedString(@"1 row affected in total, by %i queries taking %@", @"text showing one row has been affected by multiple queries"), totalQueriesRun, - [NSString stringForTimeInterval:executionTime intervalInClocks:YES] + [NSString stringForTimeInterval:executionTime] ]]; } else { [affectedRowsText setStringValue:[NSString stringWithFormat:NSLocalizedString(@"%i rows affected in total, by %i queries taking %@", @"text showing how many rows have been affected by multiple queries"), totalAffectedRows, totalQueriesRun, - [NSString stringForTimeInterval:executionTime intervalInClocks:YES] + [NSString stringForTimeInterval:executionTime] ]]; } } else { if (totalAffectedRows==1) { [affectedRowsText setStringValue:[NSString stringWithFormat:NSLocalizedString(@"1 row affected, taking %@", @"text showing one row has been affected by a single query"), - [NSString stringForTimeInterval:executionTime intervalInClocks:YES] + [NSString stringForTimeInterval:executionTime] ]]; } else { [affectedRowsText setStringValue:[NSString stringWithFormat:NSLocalizedString(@"%i rows affected, taking %@", @"text showing how many rows have been affected by a single query"), totalAffectedRows, - [NSString stringForTimeInterval:executionTime intervalInClocks:YES] + [NSString stringForTimeInterval:executionTime] ]]; } diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h index 540492c0..f6a49105 100644 --- a/Source/SPStringAdditions.h +++ b/Source/SPStringAdditions.h @@ -53,7 +53,7 @@ static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, int encodin @interface NSString (SPStringAdditions) + (NSString *)stringForByteSize:(int)byteSize; -+ (NSString *)stringForTimeInterval:(float)timeInterval intervalInClocks:(BOOL)inClocks; ++ (NSString *)stringForTimeInterval:(float)timeInterval; - (NSString *)backtickQuotedString; - (NSArray *)lineRangesForRange:(NSRange)aRange; diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index fef2d763..114ecf7d 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -74,17 +74,17 @@ // // Returns a human readable version string of the supplied time interval. // ------------------------------------------------------------------------------- -+ (NSString *)stringForTimeInterval:(float)timeInterval intervalInClocks:(BOOL)inClocks ++ (NSString *)stringForTimeInterval:(float)timeInterval { NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; - if(inClocks) - timeInterval = timeInterval/CLOCKS_PER_SEC; - [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; + // For time periods of less than one millisecond, display a localised "< 0.1 ms" if (timeInterval < 0.0001) { - return @"< 0,1 ms"; + [numberFormatter setFormat:@"< #,##0.0 ms"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:0.1]]; } if (timeInterval < 0.1) { -- cgit v1.2.3