2 * Copyright (c) 2016 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@
26 @implementation SCTest
28 - (instancetype)initWithOptions:(NSDictionary *)options
32 self.options = options;
33 self.globalCPU = malloc(sizeof(CPUUsageInfo));
34 self.globalTimer = malloc(sizeof(timerInfo));
41 if (self.globalTimer != NULL) {
42 free(self.globalTimer);
44 if (self.globalCPU != NULL) {
54 + (NSString *)commandDescription
56 return @"This is a generic class";
59 + (NSString *)commandHelp
61 return @"This is a generic help";
69 - (void)waitFor:(double)seconds
71 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
72 dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
73 dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 0);
74 dispatch_source_set_event_handler(timer, ^{
75 dispatch_semaphore_signal(sem);
77 dispatch_resume(timer);
78 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
81 - (void)cleanupAndExitWithErrorCode:(int)error
83 if (self.options[kSCTestGlobalOptionTime] != nil) {
84 timerEnd(self.globalTimer);
85 SCTestLog("Time: %@ s", createUsageStringForTimer(self.globalTimer));
88 if (self.options[kSCTestGlobalOptionCPU] != nil) {
89 cpuEnd(self.globalCPU);
90 SCTestLog("CPU: %@", createUsageStringForCPU(self.globalCPU));
93 if (self.options[kSCTestGlobalOptionWait] != nil) {