Skip to content

Commit

Permalink
test(app launch profiling): add integration and unit tests (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Feb 8, 2024
1 parent da88ff3 commit b625d72
Show file tree
Hide file tree
Showing 27 changed files with 1,168 additions and 267 deletions.
6 changes: 6 additions & 0 deletions Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
84B527BD28DD25E400475E8D /* SentryDevice.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84B527BC28DD25E400475E8D /* SentryDevice.mm */; };
84BE546F287503F100ACC735 /* SentrySDKPerformanceBenchmarkTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 84BE546E287503F100ACC735 /* SentrySDKPerformanceBenchmarkTests.m */; };
84BE547E287645B900ACC735 /* SentryProcessInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 84BE54792876451D00ACC735 /* SentryProcessInfo.m */; };
84DEE88E2B6A4D1200A7BC17 /* AppStartup.m in Sources */ = {isa = PBXBuildFile; fileRef = 84DEE88D2B6A4D1200A7BC17 /* AppStartup.m */; };
84FB812A284001B800F3A94A /* SentryBenchmarking.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84FB8129284001B800F3A94A /* SentryBenchmarking.mm */; };
84FB812B284001B800F3A94A /* SentryBenchmarking.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84FB8129284001B800F3A94A /* SentryBenchmarking.mm */; };
8E8C57AF25EF16E6001CEEFA /* TraceTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8C57AE25EF16E6001CEEFA /* TraceTestViewController.swift */; };
Expand Down Expand Up @@ -314,6 +315,8 @@
84BE546E287503F100ACC735 /* SentrySDKPerformanceBenchmarkTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySDKPerformanceBenchmarkTests.m; sourceTree = "<group>"; };
84BE54782876451D00ACC735 /* SentryProcessInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryProcessInfo.h; sourceTree = "<group>"; };
84BE54792876451D00ACC735 /* SentryProcessInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryProcessInfo.m; sourceTree = "<group>"; };
84DEE88C2B6A4D1200A7BC17 /* AppStartup.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppStartup.h; sourceTree = "<group>"; };
84DEE88D2B6A4D1200A7BC17 /* AppStartup.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppStartup.m; sourceTree = "<group>"; };
84FB8125284001B800F3A94A /* SentryBenchmarking.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryBenchmarking.h; sourceTree = "<group>"; };
84FB8129284001B800F3A94A /* SentryBenchmarking.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryBenchmarking.mm; sourceTree = "<group>"; };
84FB812C2840021B00F3A94A /* iOS-Swift-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "iOS-Swift-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -475,6 +478,8 @@
D8DBDA73274D4DF900007380 /* ViewControllers */,
63F93AA9245AC91600A500DB /* iOS-Swift.entitlements */,
637AFDA9243B02760034958B /* AppDelegate.swift */,
84DEE88C2B6A4D1200A7BC17 /* AppStartup.h */,
84DEE88D2B6A4D1200A7BC17 /* AppStartup.m */,
637AFDAD243B02760034958B /* TransactionsViewController.swift */,
84AB90782A50031B0054C99A /* Profiling */,
D80D021229EE93630084393D /* ErrorsViewController.swift */,
Expand Down Expand Up @@ -953,6 +958,7 @@
7B79000429028C7300A7F467 /* MetricKitManager.swift in Sources */,
D8D7BB4A2750067900044146 /* UIAssert.swift in Sources */,
D8F3D057274E574200B56F8C /* LoremIpsumViewController.swift in Sources */,
84DEE88E2B6A4D1200A7BC17 /* AppStartup.m in Sources */,
629EC8AD2B0B537400858855 /* ANRs.swift in Sources */,
D8DBDA78274D5FC400007380 /* SplitViewController.swift in Sources */,
84ACC43C2A73CB5900932A18 /* ProfilingNetworkScanner.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
buildConfiguration = "Test"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
Expand Down Expand Up @@ -73,6 +73,18 @@
argument = "--disable-file-io-tracing"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--disable-watchdog-tracking"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--disable-tracing"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--io.sentry.test.profilesAppLaunches"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--disable-crash-handler"
isEnabled = "NO">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Test"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
26 changes: 7 additions & 19 deletions Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
options.tracesSampleRate = tracesSampleRate

if let tracesSamplerValue = env["--io.sentry.tracersSamplerValue"] {
if let tracesSamplerValue = env["--io.sentry.tracesSamplerValue"] {
options.tracesSampler = { _ in
return NSNumber(value: (tracesSamplerValue as NSString).integerValue)
}
Expand All @@ -51,7 +51,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return NSNumber(value: (profilesSamplerValue as NSString).integerValue)
}
}


options.enableAppLaunchProfiling = args.contains("--profile-app-launches")

options.sessionTrackingIntervalMillis = 5_000
options.attachScreenshot = true
options.attachViewHierarchy = true
Expand Down Expand Up @@ -81,9 +83,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
options.enableNetworkBreadcrumbs = !args.contains("--disable-network-breadcrumbs")
options.enableSwizzling = !args.contains("--disable-swizzling")
options.enableCrashHandler = !args.contains("--disable-crash-handler")
options.enableTracing = !args.contains("--disable-tracing")

// because we run CPU for 15 seconds at full throttle, we trigger ANR issues being sent. disable such during benchmarks.
options.enableAppHangTracking = !isBenchmarking && !args.contains("--disable-anr-tracking")
options.enableWatchdogTerminationTracking = !isBenchmarking && !args.contains("--disable-watchdog-tracking")
options.appHangTimeoutInterval = 2
options.enableCaptureFailedRequests = true
let httpStatusCodeRange = HttpStatusCodeRange(min: 400, max: 599)
Expand All @@ -96,7 +100,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

options.initialScope = { scope in
let processInfoEnvironment = ProcessInfo.processInfo.environment["io.sentry.sdk-environment"]
let processInfoEnvironment = env["io.sentry.sdk-environment"]

if processInfoEnvironment != nil {
scope.setEnvironment(processInfoEnvironment)
Expand Down Expand Up @@ -129,7 +133,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print("[iOS-Swift] launch arguments: \(ProcessInfo.processInfo.arguments)")
print("[iOS-Swift] environment: \(ProcessInfo.processInfo.environment)")

maybeWipeData()
AppDelegate.startSentry()

if #available(iOS 15.0, *) {
Expand Down Expand Up @@ -159,18 +162,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// swiftlint:enable force_cast
}
}

private extension AppDelegate {
func maybeWipeData() {
if ProcessInfo.processInfo.arguments.contains("--io.sentry.wipe-data") {
print("[iOS-Swift] removing app data")
let appSupport = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
let cache = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
for path in [appSupport, cache] {
for item in FileManager.default.enumerator(atPath: path)! {
try! FileManager.default.removeItem(atPath: (path as NSString).appendingPathComponent((item as! String)))
}
}
}
}
}
17 changes: 17 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/AppStartup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppStartup.h
// iOS-Swift
//
// Created by Andrew McKnight on 1/31/24.
// Copyright © 2024 Sentry. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AppStartup : NSObject

@end

NS_ASSUME_NONNULL_END
26 changes: 26 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/AppStartup.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import "AppStartup.h"

@implementation AppStartup

// we must do this in objective c, because it's not permitted to be overridden in Swift
+ (void)load
{
if ([NSProcessInfo.processInfo.arguments containsObject:@"--io.sentry.wipe-data"]) {
NSLog(@"[iOS-Swift] removing app data");
NSString *appSupport = [NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, true) firstObject];
NSString *cache = [NSSearchPathForDirectoriesInDomains(
NSCachesDirectory, NSUserDomainMask, true) firstObject];
NSFileManager *fm = NSFileManager.defaultManager;
for (NSString *dir in @[ appSupport, cache ]) {
for (NSString *file in [fm enumeratorAtPath:dir]) {
NSError *error;
if (![fm removeItemAtPath:[dir stringByAppendingPathComponent:file] error:&error]) {
NSLog(@"[iOS-Swift] failed to remove data at app startup.");
}
}
}
}
}

@end
Loading

0 comments on commit b625d72

Please sign in to comment.