diff --git a/Samples/iOS-Swift/iOS-Swift-UITests/ProfilingUITests.swift b/Samples/iOS-Swift/iOS-Swift-UITests/ProfilingUITests.swift index 8b5f31bb903..dc8d8c27a1e 100644 --- a/Samples/iOS-Swift/iOS-Swift-UITests/ProfilingUITests.swift +++ b/Samples/iOS-Swift/iOS-Swift-UITests/ProfilingUITests.swift @@ -8,6 +8,7 @@ class ProfilingUITests: BaseUITest { func testProfiledAppLaunches() throws { if #available(iOS 16, *) { app.launchArguments.append("--io.sentry.wipe-data") + setDefaultLaunchArgs() launchApp() // First launch enables in-app profiling by setting traces/profiles sample rates to 1 (which is the default configuration in the sample app), but not launch profiling; assert that we did not write a config to allow the next launch to be profiled. @@ -162,6 +163,15 @@ extension ProfilingUITests { }) XCTAssert(try XCTUnwrap(sample["thread_id"] as? String) == "259") // the main thread is always ID 259 } + + /** + * These cause traces to run the profiler, which can overwrite the launch profile file we need to retrieve to make assertions in the UI test. + */ + func setDefaultLaunchArgs() { + app.launchArguments.append("--disable-swizzling") + app.launchArguments.append("--disable-auto-performance-tracing") + app.launchArguments.append("--disable-uiviewcontroller-tracing") + } /** * Performs the various operations for the launch profiler test case: @@ -216,16 +226,8 @@ extension ProfilingUITests { if shouldDisableTracing { app.launchArguments.append("--disable-tracing") } - if shouldDisableSwizzling { - app.launchArguments.append("--disable-swizzling") - } - if shouldDisableAutoPerformanceTracking { - app.launchArguments.append("--disable-auto-performance-tracing") - } - if shouldDisableUIViewControllerTracing { - app.launchArguments.append("--disable-uiviewcontroller-tracing") - } + setDefaultLaunchArgs() launchApp() let sdkOptionsConfigurationAllowsLaunchProfiling = !(shouldDisableTracing || shouldDisableSwizzling || shouldDisableAutoPerformanceTracking || shouldDisableUIViewControllerTracing) diff --git a/Samples/iOS-Swift/iOS-Swift/Profiling/ProfilingViewController.swift b/Samples/iOS-Swift/iOS-Swift/Profiling/ProfilingViewController.swift index bb25956119a..7c0a4504093 100644 --- a/Samples/iOS-Swift/iOS-Swift/Profiling/ProfilingViewController.swift +++ b/Samples/iOS-Swift/iOS-Swift/Profiling/ProfilingViewController.swift @@ -112,7 +112,7 @@ class ProfilingViewController: UIViewController, UITextFieldDelegate { @IBAction func viewLaunchProfile(_ sender: Any) { profilingUITestDataMarshalingTextField.text = "" - withProfile(fileName: "launchProfile") { file in + withProfile(fileName: "profile") { file in handleContents(file: file) } } diff --git a/Sources/Sentry/Profiling/SentryLaunchProfiling.m b/Sources/Sentry/Profiling/SentryLaunchProfiling.m index 8f9883eb585..e5f4f7eed1e 100644 --- a/Sources/Sentry/Profiling/SentryLaunchProfiling.m +++ b/Sources/Sentry/Profiling/SentryLaunchProfiling.m @@ -24,7 +24,6 @@ NS_ASSUME_NONNULL_BEGIN -BOOL isProfilingAppLaunch; NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate = @"traces"; NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate = @"profiles"; NSString *const kSentryLaunchProfileConfigKeyContinuousProfiling = @"continuous-profiling"; @@ -119,8 +118,7 @@ void _sentry_nondeduplicated_startLaunchProfile(void) { - sentry_isTracingAppLaunch = appLaunchProfileConfigFileExists(); - if (!sentry_isTracingAppLaunch) { + if (!appLaunchProfileConfigFileExists()) { return; } @@ -140,18 +138,19 @@ NSNumber *profilesRate = launchConfig[kSentryLaunchProfileConfigKeyProfilesSampleRate]; if (profilesRate == nil) { SENTRY_LOG_DEBUG(@"Received a nil configured launch profile sample rate, will not " - @"start continuous profiler for launch."); + @"start trace profiler for launch."); return; } NSNumber *tracesRate = launchConfig[kSentryLaunchProfileConfigKeyTracesSampleRate]; if (tracesRate == nil) { SENTRY_LOG_DEBUG(@"Received a nil configured launch trace sample rate, will not start " - @"a profiled launch trace."); + @"trace profiler for launch."); return; } - SENTRY_LOG_INFO(@"Starting app launch profile at %llu.", getAbsoluteTime()); + SENTRY_LOG_INFO(@"Starting app launch trace profile at %llu.", getAbsoluteTime()); + sentry_isTracingAppLaunch = YES; sentry_launchTracer = [[SentryTracer alloc] initWithTransactionContext:sentry_context(tracesRate) hub:nil @@ -213,6 +212,7 @@ { SENTRY_LOG_DEBUG(@"Finishing launch tracer."); [sentry_launchTracer finish]; + sentry_isTracingAppLaunch = NO; } NS_ASSUME_NONNULL_END diff --git a/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m b/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m index f1eb44fef1f..48f76c69892 100644 --- a/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m +++ b/Sources/Sentry/Profiling/SentryProfilerTestHelpers.m @@ -44,24 +44,15 @@ SENTRY_LOG_DEBUG(@"App support directory already exists."); } - NSString *pathToWrite; - if (sentry_isTracingAppLaunch) { - SENTRY_LOG_DEBUG(@"Writing app launch profile."); - pathToWrite = [appSupportDirPath stringByAppendingPathComponent:@"launchProfile"]; - } else { - SENTRY_LOG_DEBUG(@"Overwriting last non-launch profile."); - pathToWrite = [appSupportDirPath stringByAppendingPathComponent:@"profile"]; - } + NSString *pathToWrite = [appSupportDirPath stringByAppendingPathComponent:@"profile"]; if ([fm fileExistsAtPath:pathToWrite]) { - SENTRY_LOG_DEBUG(@"Already a %@ profile file present; make sure to remove them right after " + SENTRY_LOG_DEBUG(@"Already a profile file present; make sure to remove them right after " @"using them, and that tests clean state in between so there isn't " - @"leftover config producing one when it isn't expected.", - sentry_isTracingAppLaunch ? @" launch" : @""); - return; + @"leftover config producing one when it isn't expected."); } - SENTRY_LOG_DEBUG(@"Writing%@ profile to file.", sentry_isTracingAppLaunch ? @" launch" : @""); + SENTRY_LOG_DEBUG(@"Writing profile to file."); NSError *error; if (![data writeToFile:pathToWrite options:NSDataWritingAtomic error:&error]) { diff --git a/Sources/Sentry/SentryProfiler.mm b/Sources/Sentry/SentryProfiler.mm index a7e3a388672..7f0a5c6a9ed 100644 --- a/Sources/Sentry/SentryProfiler.mm +++ b/Sources/Sentry/SentryProfiler.mm @@ -157,6 +157,8 @@ - (void)backgroundAbort - (void)stopForReason:(SentryProfilerTruncationReason)reason { + // the following line makes unit tests pass, but ui tests fail because sentry_writeProfileFile + // needs it to be true to write to the correct path sentry_isTracingAppLaunch = NO; [_timeoutTimer invalidate]; [self.metricProfiler stop];