]> git.saurik.com Git - apple/security.git/blob - keychain/analytics/TestResourceUsage.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / analytics / TestResourceUsage.m
1 /*
2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "TestResourceUsage.h"
25 #import <libproc.h>
26 #import <mach/mach_time.h>
27 #import <mach/message.h>
28 #import <os/assumes.h>
29 #import <stdio.h>
30
31 @interface TestResourceUsage () <XCTestObservation>
32 @property (assign) rusage_info_current startResource;
33 @property (assign) uint64_t startMachTime;
34 @property (assign) uint64_t stopMachTime;
35 @end
36
37
38 @implementation TestResourceUsage
39
40 + (TestResourceUsage *)sharedManager
41 {
42 static TestResourceUsage *manager = nil;
43 static dispatch_once_t onceToken;
44 dispatch_once(&onceToken, ^{
45 manager = [TestResourceUsage new];
46 });
47 return manager;
48 }
49
50 + (void)monitorTestResourceUsage
51 {
52 static dispatch_once_t onceToken;
53 TestResourceUsage *manager = [TestResourceUsage sharedManager];
54 dispatch_once(&onceToken, ^{
55 [[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:manager];
56 });
57 }
58
59 - (void)testCaseWillStart:(XCTestCase *)testCase
60 {
61 proc_pid_rusage(getpid(), RUSAGE_INFO_CURRENT, (rusage_info_t *)&_startResource);
62 _startMachTime = mach_absolute_time();
63 }
64
65 - (void)testCaseDidFinish:(XCTestCase *)testCase
66 {
67 _stopMachTime = mach_absolute_time();
68
69 rusage_info_current endResource;
70 proc_pid_rusage(getpid(), RUSAGE_INFO_CURRENT, (rusage_info_t *)&endResource);
71
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];
76
77 uint64_t diskUsage = endResource.ri_logical_writes - self.startResource.ri_logical_writes;
78
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];
83 }
84
85 - (void)BATSUnitTestToken:(NSString *)name value:(id)value testcase:(XCTestCase *)testCase
86 {
87 printf("%s", [[NSString stringWithFormat:@"[RESULT_KEY] TestResourceUsage:%s:%@\n[RESULT_VALUE] %@\n",
88 object_getClassName([testCase class]), name, value] UTF8String]);
89 }
90
91 - (uint64_t)nsecondsFromMachTime:(uint64_t)machTime
92 {
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;
99 } else {
100 ratio = 1;
101 }
102 });
103
104 return (machTime * ratio);
105 }
106
107
108
109 @end