]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_transform/misc/speed-test.mm
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / libsecurity_transform / misc / speed-test.mm
1 /*
2 * Copyright (c) 2010 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
25 #import "speed-test.h"
26 #include "SecTransform.h"
27 #include "SecExternalSourceTransform.h"
28 #include "SecNullTransform.h"
29 #include <assert.h>
30
31 @implementation speed_test
32
33 @end
34
35 UInt8 zeros[1024];
36
37 typedef void (^push_block_t)(CFDataRef d);
38
39 void timed_test(NSString *name, float seconds, SecTransformRef tr, push_block_t push) {
40 __block int num_out = 0;
41 __block int num_in = 0;
42 __block int timeout_out = -1;
43 __block int timeout_in = -1;
44 volatile __block bool done;
45 static CFDataRef z = CFDataCreateWithBytesNoCopy(NULL, zeros, sizeof(zeros), NULL);
46
47 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(seconds * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
48 done = true;
49 timeout_out = num_out;
50 timeout_in = num_in;
51 });
52
53 dispatch_group_t dg = dispatch_group_create();
54 dispatch_group_enter(dg);
55
56 dispatch_queue_t q = dispatch_queue_create("counter", NULL);
57
58 SecTransformExecuteAsync(tr, q, ^(CFTypeRef message, CFErrorRef error, Boolean isFinal) {
59 if (message) {
60 num_out++;
61 }
62 if (error) {
63 NSLog(@"Error %@ while running %@", error, name);
64 }
65 if (isFinal) {
66 dispatch_group_leave(dg);
67 }
68 });
69
70 while (!done) {
71 push(z);
72 num_in++;
73 }
74 push(NULL);
75 dispatch_group_wait(dg, DISPATCH_TIME_FOREVER);
76 NSString *m = [NSString stringWithFormat:@"%@ %d in, %d out times in %f seconds, %d stragglers\n", name, timeout_in, timeout_out, seconds, num_out - timeout_out];
77 [m writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:NULL];
78 }
79
80 int main(int argc, char *argv[]) {
81 NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
82 float seconds = 5.0;
83
84 {
85 SecTransformRef x = SecExternalSourceTransformCreate(NULL);
86 //SecTransformRef t = SecEncodeTransformCreate(kSecBase64Encoding, NULL);
87 SecTransformRef t = SecNullTransformCreate();
88 SecTransformRef g = SecTransformCreateGroupTransform();
89 assert(x && t && g);
90 SecTransformConnectTransforms(x, kSecTransformOutputAttributeName, t, kSecTransformInputAttributeName, g, NULL);
91
92 timed_test(@"external source", seconds, t, ^(CFDataRef d){
93 SecExternalSourceSetValue(x, d, NULL);
94 });
95
96 }
97
98 // This second test has issues with the stock transform framwork -- it don't think the graph is valid (missing input)
99 {
100 //SecTransformRef t = SecEncodeTransformCreate(kSecBase64Encoding, NULL);
101 SecTransformRef t = SecNullTransformCreate();
102 assert(t);
103
104 timed_test(@"set INPUT", seconds, t, ^(CFDataRef d){
105 SecTransformSetAttribute(t, kSecTransformInputAttributeName, d, NULL);
106 });
107 }
108 }