├── InstrumentsParserApp.h ├── InstrumentsParserApp.m ├── PFTTrackSegment.h ├── PFTTrackSegment.m ├── Products ├── InstrumentsParser ├── InstrumentsParser_6.3 ├── InstrumentsParser_7.0 └── InstrumentsParser_7.1 ├── README.md ├── UIARun.h ├── UIARun.m ├── Utils.h ├── Utils.m ├── XRActivityInstrumentRun.h ├── XRActivityInstrumentRun.m ├── XRObjectAllocRun.h ├── XRObjectAllocRun.m ├── XRRun.h ├── XRRun.m ├── XRStreamedPowerRun.h ├── XRStreamedPowerRun.m ├── XRVideoCardRun.h ├── XRVideoCardRun.m └── main.m /InstrumentsParserApp.h: -------------------------------------------------------------------------------- 1 | // 2 | // InstrumentsParserApp.h 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-30. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | //singleton ,store app global info 10 | 11 | #import 12 | 13 | @interface InstrumentsParserApp : NSObject{ 14 | 15 | } 16 | 17 | @property (nonatomic,retain) NSString* appname; 18 | 19 | + (InstrumentsParserApp *) getInstance; 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /InstrumentsParserApp.m: -------------------------------------------------------------------------------- 1 | // 2 | // InstrumentsParserApp.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-30. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "InstrumentsParserApp.h" 11 | 12 | static InstrumentsParserApp *sharedInstance = nil; 13 | 14 | @implementation InstrumentsParserApp 15 | 16 | @synthesize appname; 17 | 18 | +(InstrumentsParserApp *)getInstance 19 | { 20 | @synchronized(self) { 21 | if (sharedInstance == nil) 22 | sharedInstance = [[self alloc] init]; 23 | } 24 | return sharedInstance; 25 | } 26 | 27 | + (id)allocWithZone:(NSZone *)zone 28 | { 29 | @synchronized(self) { 30 | if (sharedInstance == nil) { 31 | sharedInstance = [super allocWithZone:zone]; 32 | return sharedInstance; 33 | } 34 | } 35 | return nil; 36 | } 37 | 38 | - (id)copyWithZone:(NSZone *)zone 39 | { 40 | return self; 41 | } 42 | 43 | 44 | @end -------------------------------------------------------------------------------- /PFTTrackSegment.h: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface PFTTrackSegment : NSObject 12 | { 13 | double durationTime; 14 | } 15 | 16 | @end 17 | 18 | @interface XRTrackSegment : PFTTrackSegment 19 | @end 20 | -------------------------------------------------------------------------------- /PFTTrackSegment.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "PFTTrackSegment.h" 10 | 11 | @implementation PFTTrackSegment 12 | 13 | - (id)initWithCoder:(NSCoder *)decoder 14 | { 15 | if((self = [super init])) 16 | { 17 | [decoder decodeObject]; 18 | NSNumber *number = [decoder decodeObject]; 19 | // Fix for Xcode 7.3 20 | if (strcmp([number objCType], @encode(double)) == 0) { 21 | durationTime = [number doubleValue]; 22 | } else { 23 | [decoder decodeObject]; 24 | durationTime = [[decoder decodeObject] doubleValue]; 25 | } 26 | [decoder decodeObject]; 27 | } 28 | 29 | return self; 30 | } 31 | 32 | @end 33 | 34 | @implementation XRTrackSegment 35 | 36 | - (id)initWithCoder:(NSCoder *)decoder 37 | { 38 | if((self = [super initWithCoder:decoder])) 39 | { 40 | } 41 | 42 | return self; 43 | } 44 | 45 | @end 46 | 47 | -------------------------------------------------------------------------------- /Products/InstrumentsParser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powerli/InstrumentsParser/85b81a85cf5d8270ff27bc5e4189ebea999e3ea5/Products/InstrumentsParser -------------------------------------------------------------------------------- /Products/InstrumentsParser_6.3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powerli/InstrumentsParser/85b81a85cf5d8270ff27bc5e4189ebea999e3ea5/Products/InstrumentsParser_6.3 -------------------------------------------------------------------------------- /Products/InstrumentsParser_7.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powerli/InstrumentsParser/85b81a85cf5d8270ff27bc5e4189ebea999e3ea5/Products/InstrumentsParser_7.0 -------------------------------------------------------------------------------- /Products/InstrumentsParser_7.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powerli/InstrumentsParser/85b81a85cf5d8270ff27bc5e4189ebea999e3ea5/Products/InstrumentsParser_7.1 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InstrumentsParser 2 | xcode version 3 | * under 6.3 Products/InstrumentsParser 4 | * 6.3* Products/InstrumentsParser_6.3 5 | * 7.0 Products/InstrumentsParser_7.0 6 | * 7.1 Products/InstrumentsParser_7.1 7 | -------------------------------------------------------------------------------- /UIARun.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIARun.h 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | //for uiautomation 12 | @interface UIARun : XRRun 13 | { 14 | NSMutableArray *sampleData; 15 | } 16 | 17 | - (NSString *) toJsonString; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /UIARun.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIARun.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "UIARun.h" 11 | 12 | 13 | @implementation UIARun 14 | 15 | 16 | - (NSString *)formattedSample:(NSUInteger)index 17 | { 18 | NSDictionary *data = sampleData[index]; 19 | 20 | NSString *LogType = data[@"LogType"]; 21 | long Type = [data[@"Type"] longValue]; 22 | NSString *Message = data[@"Message"]; 23 | NSString *Timestamp = data[@"Timestamp"]; 24 | 25 | NSString *result = [NSString stringWithFormat:@"LogType: %@ Type %lu Message: %@ Timestamp: %@",LogType,Type,Message,Timestamp]; 26 | 27 | return result; 28 | } 29 | 30 | - (NSString *)description 31 | { 32 | NSString *start = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:startTime]]; 33 | NSString *end = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:endTime]]; 34 | NSMutableString *result = [NSMutableString stringWithFormat:@"Run %u, starting at %@, running until %@\n", (unsigned int)runNumber, start, end]; 35 | for(NSUInteger i=0; i<[sampleData count]; i++) 36 | { 37 | [result appendFormat:@"Sample %u: %@\n", (unsigned int)i, [self formattedSample:i]]; 38 | } 39 | return result; 40 | } 41 | 42 | - (void) filterData:(NSMutableDictionary *)dict 43 | { 44 | NSMutableDictionary *newdict = dict; 45 | 46 | if ([newdict objectForKey:@"Timestamp"]) { 47 | NSDate *date = newdict[@"Timestamp"]; 48 | newdict[@"Timestamp"] = [NSNumber numberWithDouble: [date timeIntervalSince1970]]; 49 | newdict[@"Date"] = [dateFormatter stringFromDate:date]; 50 | } 51 | 52 | if ([newdict objectForKey:@"ExceptionData"]) { 53 | [newdict removeObjectForKey:@"ExceptionData"]; 54 | } 55 | 56 | if ([newdict objectForKey:@"children"]) { 57 | NSMutableArray *childarray = [newdict objectForKey:@"children"]; 58 | for (NSInteger i=0; i < [childarray count]; i++) { 59 | [self filterData:childarray[i]]; 60 | } 61 | } 62 | } 63 | 64 | - (NSString *) toJsonString 65 | { 66 | NSMutableArray *newArray = [[NSMutableArray alloc] init]; 67 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 68 | for(NSUInteger i=0; i<[sampleData count]; i++){ 69 | //for(NSUInteger i=0; i<1; i++){ 70 | dict = sampleData[i]; 71 | [self filterData:dict]; 72 | newArray[i] = dict; 73 | } 74 | NSError *error = nil; 75 | NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newArray options:NSJSONWritingPrettyPrinted error:&error]; 76 | if([jsonData length] >0 && error == nil){ 77 | NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 78 | return jsonString; 79 | }else{ 80 | return nil; 81 | } 82 | 83 | } 84 | 85 | 86 | - (id)initWithCoder:(NSCoder *)decoder 87 | { 88 | if((self = [super initWithCoder:decoder])) 89 | { 90 | sampleData = [decoder decodeObject]; 91 | [decoder decodeObject]; 92 | [decoder decodeObject]; 93 | [decoder decodeObject]; 94 | } 95 | return self; 96 | } 97 | 98 | - (void)dealloc 99 | { 100 | } 101 | 102 | /* 103 | dict element like this 104 | [0] __NSDictionaryM * 4 key/value pairs 0x0000000100300b50 105 | { 106 | [0] (null) @"LogType" : @"Debug" 107 | [1] (null) @"Type" : (long)0 108 | [2] (null) @"Message" : @"target.tapWithOptions({x:\"92\", y:\"266\"}, {touchCount:\"1\", duration:\"0\", tapCount:\"1\"})" 109 | [3] (null) @"Timestamp" : 2014-07-29 15:50:14 CST 110 | } 111 | */ 112 | 113 | @end -------------------------------------------------------------------------------- /Utils.h: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #define ZIP_FAILED @"Unzip Failed" 11 | 12 | @interface Utils : NSObject 13 | + (NSString *)unzipFile:(NSString *)filePath; 14 | + (BOOL) grepFile: (NSString *)filePath searchKeyword:(NSString*)keyword; 15 | @end 16 | -------------------------------------------------------------------------------- /Utils.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "Utils.h" 10 | 11 | @implementation NSString (TrimmingAdditions) 12 | 13 | - (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet { 14 | NSUInteger location = 0; 15 | NSUInteger length = [self length]; 16 | unichar charBuffer[length]; 17 | [self getCharacters:charBuffer]; 18 | 19 | for (location; location < length; location++) { 20 | if (![characterSet characterIsMember:charBuffer[location]]) { 21 | break; 22 | } 23 | } 24 | 25 | return [self substringWithRange:NSMakeRange(location, length - location)]; 26 | } 27 | 28 | - (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet { 29 | NSUInteger location = 0; 30 | NSUInteger length = [self length]; 31 | unichar charBuffer[length]; 32 | [self getCharacters:charBuffer]; 33 | 34 | for (length; length > 0; length--) { 35 | if (![characterSet characterIsMember:charBuffer[length - 1]]) { 36 | break; 37 | } 38 | } 39 | 40 | return [self substringWithRange:NSMakeRange(location, length - location)]; 41 | } 42 | 43 | @end 44 | 45 | @implementation Utils 46 | 47 | + (NSString *)unzipFile:(NSString *)filePath { 48 | // Unzip file 49 | NSTask *task; 50 | task = [[NSTask alloc] init]; 51 | [task setLaunchPath: @"/usr/bin/unzip"]; 52 | 53 | NSArray *arguments; 54 | arguments = @[@"-o", filePath]; 55 | [task setArguments: arguments]; 56 | 57 | NSPipe *pipe; 58 | pipe = [NSPipe pipe]; 59 | [task setStandardOutput: pipe]; 60 | 61 | NSFileHandle *file; 62 | file = [pipe fileHandleForReading]; 63 | 64 | [task launch]; 65 | 66 | NSData *data; 67 | data = [file readDataToEndOfFile]; 68 | 69 | NSString *string; 70 | string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 71 | 72 | NSRange startRange = [string rangeOfString:@"inflating: "]; 73 | if (startRange.location == NSNotFound) { 74 | return ZIP_FAILED; 75 | } 76 | NSString *unzipedFile = [[string substringFromIndex:(startRange.location + startRange.length)] stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 77 | //printf ("\nunzip trace file:\n%s\n", [string UTF8String]); 78 | return unzipedFile; 79 | } 80 | 81 | + (BOOL) grepFile:(NSString *)filePath searchKeyword:(NSString *)keyword{ 82 | NSTask *task = [[NSTask alloc] init]; 83 | [task setLaunchPath:@"/usr/bin/grep"]; 84 | 85 | NSArray *arguments = [NSArray arrayWithObjects: keyword, filePath,nil]; 86 | [task setArguments:arguments]; 87 | 88 | NSPipe *pipe = [NSPipe pipe]; 89 | [task setStandardOutput:pipe]; 90 | 91 | NSFileHandle *file = [pipe fileHandleForReading]; 92 | 93 | [task launch]; 94 | 95 | NSData *data = [file readDataToEndOfFile]; 96 | 97 | NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 98 | 99 | NSRange startRange = [string rangeOfString:@"matches"]; 100 | if(startRange.location == NSNotFound){ 101 | return NO; 102 | }else{ 103 | return YES; 104 | } 105 | } 106 | 107 | @end 108 | -------------------------------------------------------------------------------- /XRActivityInstrumentRun.h: -------------------------------------------------------------------------------- 1 | // 2 | // XRActivityInstrumentRun.h 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | //for activity monitor 12 | @interface XRActivityInstrumentRun : XRRun 13 | { 14 | NSMutableArray *sampleData; 15 | } 16 | 17 | - (NSString *) toJsonString; 18 | 19 | @end 20 | 21 | -------------------------------------------------------------------------------- /XRActivityInstrumentRun.m: -------------------------------------------------------------------------------- 1 | // 2 | // XRActivityInstrumentRun.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XRActivityInstrumentRun.h" 11 | #import "InstrumentsParserApp.h" 12 | 13 | 14 | //@implementation XRActivityInstrumentRun 15 | @implementation XRActivityInstrumentRun 16 | 17 | 18 | - (NSString *)formattedSample:(NSUInteger)index processName:targetProcess 19 | { 20 | NSDictionary *data = sampleData[index]; 21 | NSMutableString *result = [NSMutableString string]; 22 | double relativeTimestamp = [data[@"XRActivityClientTraceRelativeTimestamp"] doubleValue]; 23 | double seconds = relativeTimestamp / 1000.0 / 1000.0 / 1000.0; 24 | NSTimeInterval timestamp = startTime + seconds; 25 | [result appendFormat:@"Process: %@ ", targetProcess]; 26 | 27 | NSArray *processData = data[@"Processes"]; 28 | for (NSDictionary *process in processData) { 29 | if ([process[@"Command"] isEqualToString:targetProcess]) { 30 | double cpuUsage = [process[@"CPUUsage"] doubleValue]; 31 | double residentSize = [process[@"ResidentSize"] doubleValue] / 1024; 32 | double virtualSize = [process[@"VirtualSize"] doubleValue] / 1024; 33 | [result appendFormat:@"CPU Usage: %.2f%% ", cpuUsage]; 34 | [result appendFormat:@"Res Size: %.2f KiB ", residentSize]; 35 | [result appendFormat:@"Virt Size: %.2f KiB ", virtualSize]; 36 | break; 37 | } 38 | } 39 | [result appendFormat:@"Timestamp: %@ ", [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timestamp]]]; 40 | return result; 41 | } 42 | 43 | - (NSString *)description 44 | { 45 | InstrumentsParserApp *shareAppData = [InstrumentsParserApp getInstance]; 46 | NSString *appname = [shareAppData appname]; 47 | if (!appname) { 48 | NSLog(@"get appname nil,cannot dump XRActivityInstrumentRun data"); 49 | return nil; 50 | } 51 | 52 | NSString *start = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:startTime]]; 53 | NSString *end = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:endTime]]; 54 | NSMutableString *result = [NSMutableString stringWithFormat:@"Run %u, starting at %@, running until %@\n", (unsigned int)runNumber, start, end]; 55 | //NSLog(@"sampledata length is %lu",(unsigned long)[sampleData count]); 56 | for(NSUInteger i=0; i<[sampleData count]; i++) 57 | { 58 | [result appendFormat:@"Sample %u: %@\n", (unsigned int)i, [self formattedSample:i processName:appname]]; 59 | } 60 | return result; 61 | } 62 | 63 | - (NSString *) toJsonString 64 | { 65 | InstrumentsParserApp *shareAppData = [InstrumentsParserApp getInstance]; 66 | NSString *appname = [shareAppData appname]; 67 | if (!appname) { 68 | NSLog(@"get appname nil,cannot dump XRActivityInstrumentRun data"); 69 | return nil; 70 | } 71 | 72 | //instrument get process name max length = 16 73 | if ([appname length] >= 16) { 74 | NSString *tmpname = [appname substringToIndex:16]; 75 | appname = tmpname; 76 | } 77 | 78 | NSMutableArray *newArray = [[NSMutableArray alloc] init]; 79 | 80 | for (NSUInteger i=0; i<[sampleData count]; i++) { 81 | NSDictionary *data = sampleData[i]; 82 | 83 | double relativeTimestamp = [data[@"XRActivityClientTraceRelativeTimestamp"] doubleValue]; 84 | double seconds = relativeTimestamp / 1000.0 / 1000.0 / 1000.0; 85 | NSTimeInterval timestamp = startTime + seconds; 86 | 87 | NSArray *processData = data[@"Processes"]; 88 | for (NSDictionary *process in processData) { 89 | if ([process[@"Command"] isEqualToString:appname]) { 90 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 91 | [dict addEntriesFromDictionary:process]; 92 | [dict setObject:[NSNumber numberWithDouble:timestamp] forKey:@"Timestamp"]; 93 | [dict setObject:[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timestamp]] forKey:@"Date"]; 94 | newArray[i] = dict; 95 | 96 | break; 97 | } 98 | } 99 | } 100 | 101 | NSError *error = nil; 102 | NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newArray options:NSJSONWritingPrettyPrinted error:&error]; 103 | if([jsonData length] >0 && error == nil){ 104 | NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 105 | return jsonString; 106 | }else{ 107 | return nil; 108 | } 109 | 110 | } 111 | 112 | 113 | - (id)initWithCoder:(NSCoder *)decoder 114 | { 115 | if((self = [super initWithCoder:decoder])) 116 | { 117 | sampleData = [decoder decodeObject]; 118 | [decoder decodeObject]; 119 | } 120 | return self; 121 | } 122 | 123 | - (void)dealloc 124 | { 125 | } 126 | 127 | @end 128 | -------------------------------------------------------------------------------- /XRObjectAllocRun.h: -------------------------------------------------------------------------------- 1 | // 2 | // XRObjectAllocRun.h 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/11/14. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | @interface XRObjectAllocRun : XRRun 12 | @end 13 | -------------------------------------------------------------------------------- /XRObjectAllocRun.m: -------------------------------------------------------------------------------- 1 | // 2 | // XRObjectAllocRun.m 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/11/14. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "XRObjectAllocRun.h" 11 | 12 | @implementation XRObjectAllocRun 13 | 14 | - (NSString *)description 15 | { 16 | NSString *start = [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:startTime] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle]; 17 | NSString *end = [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:endTime] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle]; 18 | 19 | NSMutableString *result = [NSMutableString stringWithFormat:@"Run %u, starting at %@, running until %@. trackSegments %@, pid %@.\n", (unsigned int)runNumber, start, end, trackSegments, pid]; 20 | [result appendFormat:@"_StartTime_:%lf,_EndTime_:%lf.\n", startTime, endTime]; 21 | return result; 22 | } 23 | 24 | @end -------------------------------------------------------------------------------- /XRRun.h: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //instruments run data base class 12 | @interface XRRun : NSObject 13 | { 14 | NSDateFormatter *dateFormatter; 15 | NSUInteger runNumber; 16 | NSTimeInterval startTime; 17 | NSTimeInterval endTime; 18 | 19 | NSMutableArray *trackSegments; 20 | NSMutableDictionary *runData; 21 | 22 | NSNumber *pid; 23 | BOOL launchControlProperties; 24 | BOOL args; 25 | BOOL envVals; 26 | BOOL execname; 27 | BOOL terminateTaskAtStop; 28 | } 29 | 30 | - (id)initWithCoder:(NSCoder *)decoder; 31 | 32 | @end 33 | 34 | -------------------------------------------------------------------------------- /XRRun.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | #define targetProcess @"Recipes" 12 | 13 | @implementation XRRun 14 | 15 | - (id)initWithCoder:(NSCoder *)decoder 16 | { 17 | if((self = [super init])) 18 | { 19 | dateFormatter = [[NSDateFormatter alloc] init]; 20 | [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"]; 21 | startTime = [[decoder decodeObject] doubleValue]; 22 | endTime = [[decoder decodeObject] doubleValue]; 23 | runNumber = [[decoder decodeObject] unsignedIntegerValue]; 24 | 25 | trackSegments = [decoder decodeObject]; 26 | 27 | // Totally not sure about these 28 | envVals = [[decoder decodeObject] boolValue]; 29 | execname = [[decoder decodeObject] boolValue]; 30 | terminateTaskAtStop = [[decoder decodeObject] boolValue]; 31 | pid = [decoder decodeObject][@"_pid"]; 32 | launchControlProperties = [[decoder decodeObject] boolValue]; 33 | args = [[decoder decodeObject] boolValue]; 34 | [decoder decodeObject]; 35 | [decoder decodeObject]; 36 | [decoder decodeObject]; //patch for XCode 7 37 | } 38 | return self; 39 | } 40 | 41 | - (void)dealloc 42 | { 43 | } 44 | 45 | 46 | @end -------------------------------------------------------------------------------- /XRStreamedPowerRun.h: -------------------------------------------------------------------------------- 1 | // 2 | // XRStreamedPowerRun.h 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/11/5. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | @interface XRStreamedPowerRun : XRRun 12 | 13 | - (double) getStartTime; 14 | 15 | @end -------------------------------------------------------------------------------- /XRStreamedPowerRun.m: -------------------------------------------------------------------------------- 1 | // 2 | // XRStreamedPowerRun.m 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/11/5. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRStreamedPowerRun.h" 10 | 11 | @implementation XRStreamedPowerRun 12 | 13 | - (NSString *)description 14 | { 15 | NSString *start = [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:startTime] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle]; 16 | NSString *end = [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:endTime] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle]; 17 | 18 | NSMutableString *result = [NSMutableString stringWithFormat:@"Run %u, starting at %@, running until %@. trackSegments %@, pid %@.\n", (unsigned int)runNumber, start, end, trackSegments, pid]; 19 | [result appendFormat:@"_StartTime_:%lf,_EndTime_:%lf.\n", startTime, endTime]; 20 | return result; 21 | } 22 | 23 | - (double) getStartTime 24 | { 25 | return startTime; 26 | } 27 | 28 | @end -------------------------------------------------------------------------------- /XRVideoCardRun.h: -------------------------------------------------------------------------------- 1 | // 2 | // XRVideoCardRun.h 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/12/8. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRRun.h" 10 | 11 | @interface XRVideoCardRun : XRRun 12 | { 13 | NSMutableArray *sampleData; 14 | } 15 | 16 | - (NSString *) toJsonString; 17 | 18 | - (double) getStartTime; 19 | 20 | @end 21 | 22 | -------------------------------------------------------------------------------- /XRVideoCardRun.m: -------------------------------------------------------------------------------- 1 | // 2 | // XRVideoCardRun.m 3 | // InstrumentsParser 4 | // 5 | // Created by baidu on 14/12/8. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import "XRVideoCardRun.h" 10 | 11 | @implementation XRVideoCardRun 12 | 13 | - (id)initWithCoder:(NSCoder *)decoder 14 | { 15 | if((self = [super initWithCoder:decoder])) 16 | { 17 | sampleData = [decoder decodeObject]; 18 | 19 | [decoder decodeObject];//id 20 | [decoder decodeObject];//id 21 | [decoder decodeObject];//id 22 | [decoder decodeObject];//id 23 | } 24 | return self; 25 | } 26 | 27 | - (NSString *)description 28 | { 29 | NSString * baseResult = [super description]; 30 | NSMutableString *result = [NSMutableString stringWithFormat:@"XRRun:%@\n", baseResult]; 31 | [result appendFormat:@"XRVideoCardRunSampleData:%@\n", sampleData]; 32 | return result; 33 | } 34 | 35 | - (NSString *) toJsonString 36 | { 37 | NSMutableArray *newArray = [[NSMutableArray alloc] init]; 38 | for(NSUInteger i=0; i<[sampleData count]; i++){ 39 | NSDictionary *data = sampleData[i]; 40 | double relativeTimestamp = [data[@"XRVideoCardRunTimeStamp"] doubleValue]; 41 | double seconds = relativeTimestamp / 1000.0 / 1000.0; 42 | NSTimeInterval timestamp = startTime + seconds; 43 | int fps = [data[@"FramesPerSecond"] intValue]; 44 | 45 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 46 | [dict setObject:[NSNumber numberWithDouble:timestamp] forKey:@"XRVideoCardRunTimeStamp"]; 47 | [dict setObject:[NSNumber numberWithInt:fps] forKey:@"FramesPerSecond"]; 48 | newArray[i] = dict; 49 | //NSLog(@"%@", newArray[i]); 50 | } 51 | NSError *error = nil; 52 | NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newArray options:NSJSONWritingPrettyPrinted error:&error]; 53 | if([jsonData length] >0 && error == nil){ 54 | NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 55 | return jsonString; 56 | }else{ 57 | return nil; 58 | } 59 | 60 | } 61 | 62 | - (double) getStartTime 63 | { 64 | return startTime; 65 | } 66 | 67 | @end -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InstrumentsParser 4 | // 5 | // Created by pantengfei on 14-7-29. 6 | // Copyright (c) 2014年 ___bidu___. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Utils.h" 11 | #import "XRRun.h" 12 | #import "XRActivityInstrumentRun.h" 13 | #import "UIARun.h" 14 | #import "InstrumentsParserApp.h" 15 | #import "XRStreamedPowerRun.h" 16 | #import "XRObjectAllocRun.h" 17 | #import "XRVideoCardRun.h" 18 | 19 | void parseNetworkActivity(NSString *fileBasename, NSString *inputTraceFile, double testStartTime, NSString *outputdir) { 20 | NSFileManager *fileManager = [NSFileManager defaultManager]; 21 | NSError *error = nil; 22 | 23 | //printf("\n%lf\n", [run getStartTime]); 24 | NSString *networkResultJsonString = nil; 25 | NSString *networkResultFile = [NSString stringWithFormat:@"%@/Trace%@.run/network_activity.dat.archive",inputTraceFile,fileBasename]; 26 | if(![fileManager fileExistsAtPath:networkResultFile]) { 27 | //NSLog(@"no network result exists!!!"); 28 | } else { 29 | //NSLog(@"network result file exists!!!"); 30 | NSDictionary *dict = [fileManager attributesOfItemAtPath:networkResultFile error:&error]; 31 | //NSLog(@"size=%lld",[dict fileSize]); 32 | NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:networkResultFile]; 33 | int size = 208; 34 | int lineCount = (int)[dict fileSize]/size; 35 | NSMutableArray *outArray = [[NSMutableArray alloc] init]; 36 | NSArray *nameArray= [NSArray arrayWithObjects:@"startTime",@"duration",@"wifiPacketsIn",@"wifiPacketsOut",@"wifiBytesIn",@"wifiBytesOut",nil]; 37 | for(int i =1 ;i0 && error == nil){ 79 | NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 80 | networkResultJsonString = jsonString; 81 | //NSString *jsonfile = [NSString stringWithFormat:@"%@/networkresult",outputdir]; 82 | //[jsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 83 | //NSLog(@"%@",jsonString); 84 | } 85 | } 86 | if(networkResultJsonString!=nil) { 87 | NSString *jsonfile = [NSString stringWithFormat:@"%@/NetworkActivity-%@",outputdir,fileBasename]; 88 | [networkResultJsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 89 | } 90 | } 91 | 92 | void parseEnergyLevel(NSString *fileBasename, NSString *inputTraceFile, double testStartTime, NSString *outputdir) { 93 | NSFileManager *fileManager = [NSFileManager defaultManager]; 94 | NSError *error = nil; 95 | 96 | //printf("\n%lf\n", [run getStartTime]); 97 | NSString *levelResultJsonString = nil; 98 | NSString *levelResultFile = [NSString stringWithFormat:@"%@/Trace%@.run/level.dat.archive",inputTraceFile,fileBasename]; 99 | if(![fileManager fileExistsAtPath:levelResultFile]) { 100 | //NSLog(@"no network result exists!!!"); 101 | } else { 102 | //NSLog(@"network result file exists!!!"); 103 | NSDictionary *dict = [fileManager attributesOfItemAtPath:levelResultFile error:&error]; 104 | //NSLog(@"size=%lld",[dict fileSize]); 105 | NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:levelResultFile]; 106 | int size = 24; 107 | int lineCount = (int)[dict fileSize]/size; 108 | NSMutableArray *outArray = [[NSMutableArray alloc] init]; 109 | NSArray *nameArray= [NSArray arrayWithObjects:@"startTime", @"level", @"level",nil]; 110 | for(int i = 0; i < lineCount; i++) { 111 | //printf("line:%d\n",i); 112 | int dataCount = 3; 113 | NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 114 | for(int j = 0; j < dataCount; j++) { 115 | [fileHandle seekToFileOffset:i*size+j*8]; 116 | NSData *data = [fileHandle readDataOfLength:8]; 117 | //NSLog(@"%@",data); 118 | Byte *byteData = (Byte *)[data bytes]; 119 | 120 | Byte temp = byteData[0]; 121 | byteData[0] = byteData[7]; 122 | byteData[7] = temp; 123 | temp = byteData[1]; 124 | byteData[1] = byteData[6]; 125 | byteData[6] = temp; 126 | temp = byteData[2]; 127 | byteData[2] = byteData[5]; 128 | byteData[5] = temp; 129 | temp = byteData[3]; 130 | byteData[3] = byteData[4]; 131 | byteData[4] = temp; 132 | 133 | double* array_of_doubles = (double*)byteData; 134 | if(j < [nameArray count] && ![nameArray[j] isEqualToString:@""] ) { 135 | NSString *str = [NSString stringWithFormat:@"%lf",array_of_doubles[0]]; 136 | double number = [str doubleValue]; 137 | [tempDict setObject:[NSNumber numberWithDouble:number] forKey:nameArray[j]]; 138 | } 139 | } 140 | NSString *duration = [tempDict objectForKey:@"duration"]; 141 | NSString *startTime = [tempDict objectForKey:@"startTime"]; 142 | double endtime = [duration doubleValue] + [startTime doubleValue] + testStartTime; 143 | //NSLog(@"%lf",endtime); 144 | //NSString *endStr = [NSString stringWithFormat:@"%.0lf",endtime]; 145 | [tempDict setObject:[NSNumber numberWithDouble:endtime] forKey:@"Timestamp"]; 146 | [outArray addObject:tempDict]; 147 | //NSLog(@"%@",tempDict); 148 | } 149 | //NSLog(@"%@",outArray); 150 | NSData *jsonData = [NSJSONSerialization dataWithJSONObject:outArray options:NSJSONWritingPrettyPrinted error:&error]; 151 | if([jsonData length] >0 && error == nil){ 152 | NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 153 | levelResultJsonString = jsonString; 154 | //NSString *jsonfile = [NSString stringWithFormat:@"%@/networkresult",outputdir]; 155 | //[jsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 156 | //NSLog(@"%@",jsonString); 157 | } 158 | } 159 | if(levelResultJsonString!=nil) { 160 | NSString *jsonfile = [NSString stringWithFormat:@"%@/EnergyLevel-%@",outputdir,fileBasename]; 161 | [levelResultJsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 162 | } 163 | } 164 | 165 | int main(int argc, const char * argv[]) { 166 | @autoreleasepool { 167 | 168 | NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 169 | NSString *appname = [standardDefaults stringForKey:@"p"]; 170 | NSString *inputdir = [standardDefaults stringForKey:@"i"]; 171 | NSString *outputdir = [standardDefaults stringForKey:@"o"]; 172 | 173 | //NSLog(@"appname[%@] inputfile[%@] outputfile[%@]",appname,inputfile,outputfile); 174 | 175 | //appname = @"IphoneCom"; 176 | //inputdir = @"/Users/scottwu/Downloads/newenergy.trace"; 177 | 178 | if (appname == nil || inputdir == nil ) { 179 | printf("InstrumentsParser -p process_name -i result.trace -o /a/b/c \nor InstrumentsParser -p process_name -i result.trace\n"); 180 | exit(1); 181 | } 182 | 183 | InstrumentsParserApp *shareAppData = [InstrumentsParserApp getInstance]; 184 | [shareAppData setAppname:appname]; //share for XRActivityInsturmentRun.m 185 | 186 | NSFileManager *fileManager = [NSFileManager defaultManager]; 187 | NSString *workingDir = [fileManager currentDirectoryPath]; 188 | 189 | 190 | 191 | if (outputdir == nil) { 192 | outputdir = workingDir; 193 | }else{ 194 | if (![fileManager fileExistsAtPath:outputdir]) { 195 | NSLog(@"output dir: %@ is not exits",outputdir); 196 | exit(-2); 197 | }else{ 198 | outputdir = [outputdir stringByExpandingTildeInPath]; 199 | } 200 | } 201 | 202 | NSString *inputTraceFile = [inputdir stringByExpandingTildeInPath]; 203 | NSString *resultTemplateIdDirs = [NSString stringWithFormat:@"%@/instrument_data",inputTraceFile]; 204 | 205 | if (![fileManager fileExistsAtPath:inputTraceFile] 206 | || ![fileManager fileExistsAtPath:resultTemplateIdDirs]) { 207 | NSLog(@"input data: %@ or %@ is not exits",inputTraceFile,resultTemplateIdDirs); 208 | exit(-1); 209 | } 210 | 211 | NSError *error = nil; 212 | 213 | 214 | NSArray *templateDirs = [fileManager contentsOfDirectoryAtPath:resultTemplateIdDirs error:&error]; 215 | if (nil == templateDirs) { 216 | NSLog(@"ls %@ error or null!\n",resultTemplateIdDirs); 217 | } 218 | 219 | NSString *topUnZipDir = nil; 220 | 221 | // result.trace/instrument_data/0AD40D0C-10B3-4E36-B6BA-8ED8DF101118/run_data/1.run.zip 222 | for (NSString *templateName in templateDirs) { 223 | NSString *templateDir = [NSString stringWithFormat:@"%@/%@",resultTemplateIdDirs,templateName]; 224 | NSString *runZipDir = [NSString stringWithFormat:@"%@/run_data",templateDir]; 225 | 226 | NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:runZipDir error:&error]; 227 | if(nil == dirContents){ 228 | NSLog(@"ls %@ to find result zip file error or nil",runZipDir); 229 | }else{ 230 | for(NSString *zipFile in dirContents){ 231 | NSString *unzipedFile = [Utils unzipFile:[NSString stringWithFormat:@"%@/%@",runZipDir,zipFile]]; 232 | topUnZipDir = [[unzipedFile componentsSeparatedByString:@"/"] objectAtIndex:0]; 233 | 234 | if (![unzipedFile isEqualToString:ZIP_FAILED]) { 235 | NSString *resultUnzippedFile = [NSString stringWithFormat:@"%@/%@",workingDir, unzipedFile]; 236 | 237 | // Read the trace file into memory 238 | NSURL *traceFile = [NSURL fileURLWithPath:[resultUnzippedFile stringByExpandingTildeInPath]]; 239 | // NSLog(@"%@",traceFile); 240 | 241 | NSData *traceData = [NSData dataWithContentsOfURL:traceFile]; 242 | 243 | NSString *jsonString; 244 | NSString *fileBasename = [[zipFile componentsSeparatedByString:@"."] objectAtIndex:0]; 245 | 246 | //detect which instrument type 247 | if ([Utils grepFile:resultUnzippedFile searchKeyword:@"UIARun"]) { 248 | //NSLog(@"%@",traceFile); 249 | UIARun *run = [NSUnarchiver unarchiveObjectWithData:traceData]; 250 | //printf("\n%s\n", [[run description] UTF8String]); 251 | jsonString = [run toJsonString]; 252 | NSString *jsonfile = [NSString stringWithFormat:@"%@/UIAutomation-%@",outputdir,fileBasename]; 253 | [jsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 254 | }else if([Utils grepFile:resultUnzippedFile searchKeyword:@"XRObjectAllocRun"]){ 255 | //printf("\n%s\n","XRObjectAllocRun"); 256 | //XRObjectAllocRun *run = [NSUnarchiver unarchiveObjectWithData:traceData]; 257 | //printf("\n%s\n", [[run description] UTF8String]); 258 | }else if([Utils grepFile:resultUnzippedFile searchKeyword:@"XRActivityInstrumentRun"]){ 259 | XRActivityInstrumentRun *run = [NSUnarchiver unarchiveObjectWithData:traceData]; 260 | //printf("\n%s\n", [[run description] UTF8String]); 261 | jsonString = [run toJsonString]; 262 | NSString *jsonfile = [NSString stringWithFormat:@"%@/ActivityMonitor-%@",outputdir,fileBasename]; 263 | [jsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 264 | }else if ([Utils grepFile:resultUnzippedFile searchKeyword:@"XRVideoCardRun"]){ 265 | XRVideoCardRun *run = [NSUnarchiver unarchiveObjectWithData:traceData]; 266 | //NSLog(@"%@",run); 267 | //printf("\n%s\n", [[run description] UTF8String]); 268 | jsonString = [run toJsonString]; 269 | NSString *jsonfile = [NSString stringWithFormat:@"%@/CoreAnimation-%@",outputdir,fileBasename]; 270 | [jsonString writeToFile:jsonfile atomically:YES encoding:NSUTF8StringEncoding error:&error]; 271 | //NSLog(@"%@",jsonString); 272 | //printf("\n%s\n",jsonString); 273 | }else if([Utils grepFile:resultUnzippedFile searchKeyword:@"XRStreamedPowerRun"]){ 274 | XRStreamedPowerRun *run = [NSUnarchiver unarchiveObjectWithData:traceData]; 275 | double testStartTime = [run getStartTime]; 276 | parseNetworkActivity(fileBasename, inputTraceFile, testStartTime, outputdir); 277 | parseEnergyLevel(fileBasename, inputTraceFile, testStartTime, outputdir); 278 | } 279 | } 280 | } 281 | } 282 | } 283 | 284 | if( nil != topUnZipDir){ 285 | NSURL *tmpDir = [NSURL fileURLWithPath:[[NSString stringWithFormat:@"%@/%@",workingDir,topUnZipDir] stringByExpandingTildeInPath]]; 286 | [fileManager removeItemAtURL:tmpDir error:&error]; 287 | } 288 | 289 | } 290 | return 0; 291 | } 292 | --------------------------------------------------------------------------------