|
| 1 | +// |
| 2 | +// DetoxInstrumentsManager.m |
| 3 | +// Detox |
| 4 | +// |
| 5 | +// Created by Leo Natan (Wix) on 2/17/19. |
| 6 | +// Copyright © 2019 Wix. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "DetoxInstrumentsManager.h" |
| 10 | +#import "DTXLogging.h" |
| 11 | +#include <dlfcn.h> |
| 12 | + |
| 13 | +DTX_CREATE_LOG_PREFIX(DetoxInstrumentsManager, @"🥶") |
| 14 | + |
| 15 | +@interface NSObject () |
| 16 | + |
| 17 | +@property (class, nonatomic, strong, readonly) id defaultProfilingConfiguration; |
| 18 | +@property (nonatomic, readwrite) NSTimeInterval samplingInterval; |
| 19 | +@property (nonatomic, readwrite) BOOL recordNetwork; |
| 20 | +@property (nonatomic, readwrite) BOOL recordThreadInformation; |
| 21 | +@property (nonatomic, readwrite) BOOL collectStackTraces; |
| 22 | +@property (nonatomic, readwrite) BOOL symbolicateStackTraces; |
| 23 | +@property (atomic, assign, readonly, getter=isRecording) BOOL recording; |
| 24 | +@property (nonatomic, copy, null_resettable, readwrite) NSURL* recordingFileURL; |
| 25 | + |
| 26 | +- (void)startProfilingWithConfiguration:(id)configuration; |
| 27 | +- (void)continueProfilingWithConfiguration:(id)configuration; |
| 28 | +- (void)stopProfilingWithCompletionHandler:(void(^ __nullable)(NSError* __nullable error))completionHandler; |
| 29 | + |
| 30 | +@end |
| 31 | + |
| 32 | +typedef NS_ENUM(NSUInteger, __DTXEventStatus) { |
| 33 | + __DTXEventStatusCompleted, |
| 34 | + __DTXEventStatusError, |
| 35 | + __DTXEventStatusCancelled |
| 36 | +}; |
| 37 | + |
| 38 | +static Class __DTXProfiler; |
| 39 | +static Class __DTXMutableProfilingConfiguration; |
| 40 | + |
| 41 | +static void (*__DTXProfilerAddTag)(NSString* tag); |
| 42 | +static NSString* (*__DTXProfilerMarkEventIntervalBegin)(NSString* category, NSString* name, NSString* __nullable message); |
| 43 | +static void (*__DTXProfilerMarkEventIntervalEnd)(NSString* identifier, __DTXEventStatus eventStatus, NSString* __nullable endMessage); |
| 44 | +static void (*__DTXProfilerMarkEvent)(NSString* category, NSString* name, __DTXEventStatus eventStatus, NSString* __nullable startMessage); |
| 45 | + |
| 46 | +@implementation DetoxInstrumentsManager |
| 47 | +{ |
| 48 | + id _recorderInstance; |
| 49 | +} |
| 50 | + |
| 51 | ++ (void)_loadProfiler |
| 52 | +{ |
| 53 | + static dispatch_once_t onceToken; |
| 54 | + dispatch_once(&onceToken, ^{ |
| 55 | + __DTXProfiler = NSClassFromString(@"DTXProfiler"); |
| 56 | + |
| 57 | + if(__DTXProfiler == NULL) |
| 58 | + { |
| 59 | + //The user has not linked the Profiler framework. Load it manually. |
| 60 | + |
| 61 | + NSString* instrumentsPath = [NSUserDefaults.standardUserDefaults stringForKey:@"instrumentsPath"]; |
| 62 | + if(instrumentsPath == nil) |
| 63 | + { |
| 64 | + instrumentsPath = @"/Applications/Detox Instruments.app"; |
| 65 | + } |
| 66 | + |
| 67 | + NSURL* bundleURL = [[NSURL fileURLWithPath:instrumentsPath] URLByAppendingPathComponent:@"/Contents/SharedSupport/ProfilerFramework/DTXProfiler.framework"]; |
| 68 | + NSBundle* profilerBundle = [NSBundle bundleWithURL:bundleURL]; |
| 69 | + |
| 70 | + if(profilerBundle == nil) |
| 71 | + { |
| 72 | + dtx_log_error(@"Error loading Profiler framework bundle. Bundle not found at %@", bundleURL.path); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + NSError* error = nil; |
| 77 | + [profilerBundle loadAndReturnError:&error]; |
| 78 | + |
| 79 | + if(error != nil) |
| 80 | + { |
| 81 | + dtx_log_error(@"Error loading Profiler framework bundle: %@", error); |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + __DTXProfiler = NSClassFromString(@"DTXProfiler"); |
| 87 | + if(__DTXProfiler == NULL) |
| 88 | + { |
| 89 | + dtx_log_error(@"DTXProfiler class not found—this should not have happened!"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + __DTXMutableProfilingConfiguration = NSClassFromString(@"DTXMutableProfilingConfiguration"); |
| 94 | + if(__DTXMutableProfilingConfiguration == NULL) |
| 95 | + { |
| 96 | + dtx_log_error(@"DTXMutableProfilingConfiguration class not found—this should not have happened!"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + __DTXProfilerAddTag = dlsym(RTLD_DEFAULT, "DTXProfilerAddTag"); |
| 101 | + __DTXProfilerMarkEventIntervalBegin = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalBegin"); |
| 102 | + __DTXProfilerMarkEventIntervalEnd = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalEnd"); |
| 103 | + __DTXProfilerMarkEvent = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEvent"); |
| 104 | + |
| 105 | + if(__DTXProfilerAddTag == NULL || __DTXProfilerMarkEventIntervalBegin == NULL || __DTXProfilerMarkEventIntervalEnd == NULL || __DTXProfilerMarkEvent == NULL) |
| 106 | + { |
| 107 | + dtx_log_error(@"One or more DTXProfilerAPI functions are NULL—this should not have happened!"); |
| 108 | + return; |
| 109 | + } |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | ++ (NSString *)_sanitizeFileNameString:(NSString *)fileName |
| 114 | +{ |
| 115 | + NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@":/\\?%*|\"<>"]; |
| 116 | + return [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@"_"]; |
| 117 | +} |
| 118 | + |
| 119 | ++ (NSURL*)defaultURLForTestName:(NSString*)testName |
| 120 | +{ |
| 121 | + NSURL* documents = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject; |
| 122 | + NSURL* rv = [documents URLByAppendingPathComponent:[self _sanitizeFileNameString:testName]]; |
| 123 | + |
| 124 | + dtx_log_debug(@"Returning %@ as URL", rv.path); |
| 125 | + |
| 126 | + return rv; |
| 127 | +} |
| 128 | + |
| 129 | +- (instancetype)init |
| 130 | +{ |
| 131 | + [DetoxInstrumentsManager _loadProfiler]; |
| 132 | + |
| 133 | + self = [super init]; |
| 134 | + |
| 135 | + if(self) |
| 136 | + { |
| 137 | + _recorderInstance = [__DTXProfiler new]; |
| 138 | + } |
| 139 | + |
| 140 | + return self; |
| 141 | +} |
| 142 | + |
| 143 | +- (id)_configForDetoxRecordingWithURL:(NSURL*)URL |
| 144 | +{ |
| 145 | + id config = [__DTXMutableProfilingConfiguration defaultProfilingConfiguration]; |
| 146 | + [config setRecordingFileURL:URL]; |
| 147 | + |
| 148 | + //TODO: Finalize the actual config for Detox perf recording. |
| 149 | + [config setRecordNetwork:NO]; |
| 150 | + [config setRecordThreadInformation:YES]; |
| 151 | + [config setCollectStackTraces:YES]; |
| 152 | + [config setSymbolicateStackTraces:YES]; |
| 153 | + [config setSamplingInterval:0.1]; |
| 154 | + |
| 155 | + return config; |
| 156 | +} |
| 157 | + |
| 158 | +- (void)startRecordingAtURL:(NSURL*)URL |
| 159 | +{ |
| 160 | + [_recorderInstance startProfilingWithConfiguration:[self _configForDetoxRecordingWithURL:URL]]; |
| 161 | +} |
| 162 | + |
| 163 | +- (void)continueRecordingAtURL:(NSURL*)URL |
| 164 | +{ |
| 165 | + [_recorderInstance continueProfilingWithConfiguration:[self _configForDetoxRecordingWithURL:URL]]; |
| 166 | +} |
| 167 | + |
| 168 | +- (void)stopRecordingWithCompletionHandler:(void(^)(NSError* error))completionHandler |
| 169 | +{ |
| 170 | + if(_recorderInstance == nil || [_recorderInstance isRecording] == NO) |
| 171 | + { |
| 172 | + if(completionHandler != nil) |
| 173 | + { |
| 174 | + completionHandler(nil); |
| 175 | + } |
| 176 | + |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + [_recorderInstance stopProfilingWithCompletionHandler:completionHandler]; |
| 181 | +} |
| 182 | + |
| 183 | +@end |
0 commit comments