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