Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
getsentry-bot committed Jul 7, 2022
1 parent 6794cf9 commit 65b4385
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
12 changes: 8 additions & 4 deletions Samples/iOS-Swift/PerformanceBenchmarks/SentryProcessInfo.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#import "SentryProcessInfo.h"
#import <sys/sysctl.h>
#import <UIKit/UIKit.h>
#import <sys/sysctl.h>
#import <unistd.h>

BOOL isDebugging() {
BOOL
isDebugging()
{
struct kinfo_proc info;

// Initialize the flags so that, if sysctl fails for some bizarre
Expand All @@ -30,8 +32,10 @@ BOOL isDebugging() {
return (info.kp_proc.p_flag & P_TRACED) != 0;
}

BOOL isSimulator() {
NSOperatingSystemVersion ios9 = {9, 0, 0};
BOOL
isSimulator()
{
NSOperatingSystemVersion ios9 = { 9, 0, 0 };
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "SentryProcessInfo.h"
#import <objc/runtime.h>
#import <XCTest/XCTest.h>
#import <objc/runtime.h>

// To get around the 15 minute timeout per test case on Sauce Labs.
static NSUInteger SentrySDKPerformanceBenchmarkTestCases = 4;
Expand All @@ -21,7 +21,8 @@ @implementation SentrySDKPerformanceBenchmarkTests
* Dynamically add a test method to an XCTestCase class.
* @see https://www.gaige.net/dynamic-xctests.html
*/
+ (BOOL)addInstanceMethodWithSelectorName:(NSString *)selectorName block:(void (^)(id))block {
+ (BOOL)addInstanceMethodWithSelectorName:(NSString *)selectorName block:(void (^)(id))block
{
NSParameterAssert(selectorName);
NSParameterAssert(block);

Expand All @@ -33,41 +34,53 @@ + (BOOL)addInstanceMethodWithSelectorName:(NSString *)selectorName block:(void (
return class_addMethod(self, selector, myIMP, "v@:");
}

+ (void)initialize {
+ (void)initialize
{
allResults = [NSMutableArray array];
for (NSUInteger i = 0; i < SentrySDKPerformanceBenchmarkTestCases; i++) {
[self addInstanceMethodWithSelectorName:[NSString stringWithFormat:@"testCPUBenchmark%lu", (unsigned long)i] block:^(XCTestCase *testCase) {
[allResults addObjectsFromArray:[self _testCPUBenchmark]];
}];
[self addInstanceMethodWithSelectorName:[NSString stringWithFormat:@"testCPUBenchmark%lu",
(unsigned long)i]
block:^(XCTestCase *testCase) {
[allResults
addObjectsFromArray:[self _testCPUBenchmark]];
}];
}
}

- (void)tearDown {
if (allResults.count == SentrySDKPerformanceBenchmarkTestCases * SentrySDKPerformanceBenchmarkIterationsPerTestCase) {
- (void)tearDown
{
if (allResults.count
== SentrySDKPerformanceBenchmarkTestCases
* SentrySDKPerformanceBenchmarkIterationsPerTestCase) {
NSUInteger index = (NSUInteger)ceil(0.9 * allResults.count);
NSNumber *p90 = [allResults sortedArrayUsingComparator:^NSComparisonResult(NSNumber *a, NSNumber *b) {
return [a compare:b];
}][index >= allResults.count ? allResults.count - 1 : index];
XCTAssertLessThanOrEqual(p90.doubleValue, 5, @"Profiling P90 overhead should remain under 5%%.");
NSNumber *p90 =
[allResults sortedArrayUsingComparator:^NSComparisonResult(NSNumber *a, NSNumber *b) {
return [a compare:b];
}][index >= allResults.count ? allResults.count - 1 : index];
XCTAssertLessThanOrEqual(
p90.doubleValue, 5, @"Profiling P90 overhead should remain under 5%%.");
checkedAssertions = YES;
}

[super tearDown];
}

+ (void)tearDown {
+ (void)tearDown
{
if (!checkedAssertions) {
@throw @"Did not perform assertion checks, might not have completed all benchmark trials.";
}
}

+ (NSArray<NSNumber *> *)_testCPUBenchmark {
+ (NSArray<NSNumber *> *)_testCPUBenchmark
{
XCTSkipIf(isSimulator() && !isDebugging());

NSMutableArray *results = [NSMutableArray array];
for (NSUInteger j = 0; j < SentrySDKPerformanceBenchmarkIterationsPerTestCase; j++) {
XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchArguments = [app.launchArguments arrayByAddingObject:@"--io.sentry.test.benchmarking"];
app.launchArguments =
[app.launchArguments arrayByAddingObject:@"--io.sentry.test.benchmarking"];
[app launch];
[app.buttons[@"Performance scenarios"] tap];

Expand All @@ -91,8 +104,10 @@ + (void)tearDown {
}

NSString *benchmarkValueString = textField.value;
// SentryBenchmarking.retrieveBenchmarks returns nil if there aren't at least 2 samples to use for calculating deltas
XCTAssertFalse([benchmarkValueString isEqualToString:@"nil"], @"Failure to record enough CPU samples to calculate benchmark.");
// SentryBenchmarking.retrieveBenchmarks returns nil if there aren't at least 2 samples to
// use for calculating deltas
XCTAssertFalse([benchmarkValueString isEqualToString:@"nil"],
@"Failure to record enough CPU samples to calculate benchmark.");
if (benchmarkValueString == nil) {
XCTFail(@"No benchmark value received from the app.");
}
Expand All @@ -104,10 +119,12 @@ + (void)tearDown {
NSInteger appSystemTime = [values[2] integerValue];
NSInteger appUserTime = [values[3] integerValue];

NSLog(@"[Sentry Benchmark] %ld,%ld,%ld,%ld", profilerSystemTime, profilerUserTime, appSystemTime, appUserTime);
NSLog(@"[Sentry Benchmark] %ld,%ld,%ld,%ld", profilerSystemTime, profilerUserTime,
appSystemTime, appUserTime);

double usagePercentage
= 100.0 * (profilerUserTime + profilerSystemTime) / (appUserTime + appSystemTime);

double usagePercentage = 100.0 * (profilerUserTime + profilerSystemTime) / (appUserTime + appSystemTime);

[results addObject:@(usagePercentage)];
}

Expand Down
10 changes: 5 additions & 5 deletions Samples/iOS-Swift/iOS-Swift/Tools/SentryBenchmarking.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ NS_ASSUME_NONNULL_BEGIN
@interface SentryBenchmarking : NSObject

/**
* Start a Sentry transaction, which will start the profiler, and also spin up an in-app sampling profiler to
* gather information on thread CPU usages throughout the benchmark.
* Start a Sentry transaction, which will start the profiler, and also spin up an in-app sampling
* profiler to gather information on thread CPU usages throughout the benchmark.
*/
+ (void)startBenchmark;

/**
* Stop the profiled transaction and return statistics on CPU usage by the profiler and test app for
* downstream processing.
* @return A dictionary serialized to a string, containing the values for profiler system time, profiler user
* time, app system time and app user time, which can be used to calculate the overhead of the profiler; or,
* if an error occurred, returns @c nil .
* @return A dictionary serialized to a string, containing the values for profiler system time,
* profiler user time, app system time and app user time, which can be used to calculate the
* overhead of the profiler; or, if an error occurred, returns @c nil .
*/
+ (nullable NSString *)stopBenchmark;

Expand Down
3 changes: 2 additions & 1 deletion Samples/iOS-Swift/iOS-Swift/Tools/SentryBenchmarking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ + (NSString *)stopBenchmark
const auto appUserTime
= ((NSNumber *)[userTimeTotals.allValues valueForKeyPath:@"@sum.self"]).integerValue;

return [NSString stringWithFormat:@"%ld,%ld,%ld,%ld", profilerSystemTime, profilerUserTime, appSystemTime, appUserTime];
return [NSString stringWithFormat:@"%ld,%ld,%ld,%ld", profilerSystemTime, profilerUserTime,
appSystemTime, appUserTime];
}

@end

0 comments on commit 65b4385

Please sign in to comment.