2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #import "TestResourceUsage.h"
26 #import <mach/mach_time.h>
27 #import <mach/message.h>
28 #import <os/assumes.h>
31 @interface TestResourceUsage () <XCTestObservation>
32 @property (assign) rusage_info_current startResource;
33 @property (assign) uint64_t startMachTime;
34 @property (assign) uint64_t stopMachTime;
38 @implementation TestResourceUsage
40 + (TestResourceUsage *)sharedManager
42 static TestResourceUsage *manager = nil;
43 static dispatch_once_t onceToken;
44 dispatch_once(&onceToken, ^{
45 manager = [TestResourceUsage new];
50 + (void)monitorTestResourceUsage
52 static dispatch_once_t onceToken;
53 TestResourceUsage *manager = [TestResourceUsage sharedManager];
54 dispatch_once(&onceToken, ^{
55 [[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:manager];
59 - (void)testCaseWillStart:(XCTestCase *)testCase
61 proc_pid_rusage(getpid(), RUSAGE_INFO_CURRENT, (rusage_info_t *)&_startResource);
62 _startMachTime = mach_absolute_time();
65 - (void)testCaseDidFinish:(XCTestCase *)testCase
67 _stopMachTime = mach_absolute_time();
69 rusage_info_current endResource;
70 proc_pid_rusage(getpid(), RUSAGE_INFO_CURRENT, (rusage_info_t *)&endResource);
72 uint64_t startUserTime = [self nsecondsFromMachTime:self.startResource.ri_user_time];
73 uint64_t endUserTime = [self nsecondsFromMachTime:endResource.ri_user_time];
74 uint64_t startSysrTime = [self nsecondsFromMachTime:self.startResource.ri_system_time];
75 uint64_t endSysTime = [self nsecondsFromMachTime:endResource.ri_system_time];
77 uint64_t diskUsage = endResource.ri_logical_writes - self.startResource.ri_logical_writes;
79 [self BATSUnitTestToken:@"DiskUsage" value:@(diskUsage) testcase:testCase];
80 [self BATSUnitTestToken:@"CPUUserTime" value:@(endUserTime - startUserTime) testcase:testCase];
81 [self BATSUnitTestToken:@"CPUSysTime" value:@(endSysTime - startSysrTime) testcase:testCase];
82 [self BATSUnitTestToken:@"WallTime" value:@(_stopMachTime - _startMachTime) testcase:testCase];
85 - (void)BATSUnitTestToken:(NSString *)name value:(id)value testcase:(XCTestCase *)testCase
87 printf("%s", [[NSString stringWithFormat:@"[RESULT_KEY] TestResourceUsage:%s:%@\n[RESULT_VALUE] %@\n",
88 object_getClassName([testCase class]), name, value] UTF8String]);
91 - (uint64_t)nsecondsFromMachTime:(uint64_t)machTime
93 static dispatch_once_t once;
94 static uint64_t ratio;
95 dispatch_once(&once, ^{
96 mach_timebase_info_data_t tbi;
97 if (os_assumes_zero(mach_timebase_info(&tbi)) == KERN_SUCCESS) {
98 ratio = tbi.numer / tbi.denom;
104 return (machTime * ratio);