-
Notifications
You must be signed in to change notification settings - Fork 8
/
perf.mm
517 lines (447 loc) · 19 KB
/
perf.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#import <Foundation/Foundation.h>
#if TARGET_OS_IOS
#import <UIKit/UIKit.h>
#else
#import <AppKit/AppKit.h>
#endif
#import <mach/mach_time.h>
#import <pthread.h>
#import "perf.h"
#include <algorithm>
#include <vector>
#include <inttypes.h>
#include <stdio.h>
struct RawTestResult {
uint64_t iterations;
uint64_t testsPerIteration;
uint64_t startTime;
uint64_t endTime;
};
struct TestResult {
const char *name;
uint64_t iterations;
NSTimeInterval total;
NSTimeInterval each;
};
struct TestInfo {
const char *name;
RawTestResult (*fptr)(void);
};
static std::vector<TestInfo> AllTests;
TestInfo EmptyLoopTest;
TestInfo *RunOnlyTest;
void Test(void) {
struct mach_timebase_info tbinfo;
mach_timebase_info( &tbinfo );
auto absToNanos = [&](uint64_t abs) { return abs * tbinfo.numer / tbinfo.denom; };
auto overheadResult = EmptyLoopTest.fptr();
NSTimeInterval totalOverhead = absToNanos(overheadResult.endTime - overheadResult.startTime);
NSTimeInterval overheadPerIteration = totalOverhead / overheadResult.iterations;
if(RunOnlyTest != NULL) {
AllTests.clear();
AllTests.push_back(*RunOnlyTest);
}
std::vector<TestResult> results;
// size_t i = AllTests.size();
// while(i --> 0) {
// auto info = AllTests[i];
for(auto info : AllTests) {
@autoreleasepool {
NSLog(@"Beginning test %s", info.name);
auto rawResult = info.fptr();
NSTimeInterval totalTime = absToNanos(rawResult.endTime - rawResult.startTime);
NSTimeInterval totalMinusOverhead = totalTime - rawResult.iterations * overheadPerIteration;
NSTimeInterval timePerIteration = totalMinusOverhead / (rawResult.iterations * rawResult.testsPerIteration);
NSLog(@"Completed test %s in %f seconds total, %fns each", info.name, totalMinusOverhead / NSEC_PER_SEC, timePerIteration);
TestResult result;
result.name = info.name;
result.iterations = rawResult.iterations * rawResult.testsPerIteration;
result.total = totalMinusOverhead / NSEC_PER_SEC;
result.each = timePerIteration;
results.push_back(result);
}
}
printf("<table><tr><td>Name</td><td>Iterations</td><td>Total time (sec)</td><td>Time per (ns)</td></tr>\n");
std::sort(results.begin(), results.end(), [](TestResult &a, TestResult &b) { return a.each < b.each; });
for(auto result : results) {
printf("<tr><td>%s</td><td>%" PRIu64 "</td><td>%.1f</td><td>%.1f</td></tr>\n", result.name, result.iterations, result.total, result.each);
}
printf("</table>\n");
}
struct RegisterTest {
TestInfo info;
RegisterTest(const char *name, RawTestResult (*fptr)(void)) {
info.name = name;
info.fptr = fptr;
if(strcmp(name, "Empty loop") == 0) {
EmptyLoopTest = info;
} else {
AllTests.push_back(info);
}
}
/// Add a call to this after DECLARE_TEST to make this the only performance test that runs,
/// so you don't have to wait through the whole test cycle to check one while working on it.
RegisterTest runOnlyThis() {
RunOnlyTest = new TestInfo;
*RunOnlyTest = info;
return *this;
}
};
#define CONCAT2(x, y) x ## y
#define CONCAT(x, y) CONCAT2(x, y)
#define DECLARE_TEST(_name, _iterations, _testsPerIteration, _setupCode, _testCode, _cleanupCode) \
static RegisterTest CONCAT(test, __COUNTER__) = RegisterTest(_name, []() -> RawTestResult { \
RawTestResult info; \
info.iterations = _iterations; \
info.testsPerIteration = _testsPerIteration; \
_setupCode; \
info.startTime = mach_absolute_time(); \
for(uint64_t i = 1; i <= _iterations; i++) { \
/* NOTE: i starts from 1 so it can be used as a divisor for integer division testing, silly I know. */ \
_testCode; \
} \
info.endTime = mach_absolute_time(); \
_cleanupCode; \
return info; \
})
DECLARE_TEST("Empty loop", 1000000000, 1, {}, {}, {});
class StubClass
{
public:
virtual void stub() { }
};
DECLARE_TEST("C++ virtual method call", 100000000, 10,
class StubClass *obj = new StubClass,
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();
obj->stub();,
delete obj);
@interface DummyClass: NSObject
- (void)dummyMethod;
@end
@implementation DummyClass
- (void)dummyMethod {}
@end
DECLARE_TEST("Objective-C message send", 100000000, 10,
DummyClass *dummy = [[DummyClass alloc] init],
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];
[dummy dummyMethod];,
[dummy release]);
DECLARE_TEST("IMP-cached message send", 100000000, 10,
DummyClass *dummy = [[DummyClass alloc] init];
SEL sel = @selector(dummyMethod);
void (*imp)(id, SEL) = (void (*)(id, SEL))[dummy methodForSelector: sel],
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);
imp(dummy, sel);,
[dummy release]);
DECLARE_TEST("NSInvocation message send", 1000000, 10,
DummyClass *dummy = [[DummyClass alloc] init];
SEL sel = @selector(dummyMethod);
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [dummy methodSignatureForSelector: sel]];
[invocation setSelector: sel];
[invocation setTarget: dummy];,
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];
[invocation invoke];,
[dummy release]);
DECLARE_TEST("Integer division", 100000000, 10,
int x,
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;
x = 1000000000 / i;,
);
DECLARE_TEST("Floating-point division", 100000000, 10,
double x;
double y = 42.3;,
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;
x = 100000000.0 / y;,
);
DECLARE_TEST("Floating-point division with integer conversion", 100000000, 10,
double x,
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;
x = 100000000.0 / i;,
);
extern "C" void objc_release(id);
extern "C" void objc_retain(id);
DECLARE_TEST("ObjC retain and release", 10000000, 10,
id obj = [[NSObject alloc] init],
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);
objc_retain(obj); objc_release(obj);,
objc_release(obj));
DECLARE_TEST("Object creation", 1000000, 10, {},
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);
objc_release([[NSObject alloc] init]);,
);
DECLARE_TEST("Autorelease pool push/pop", 10000000, 10, {},
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {}
@autoreleasepool {},
);
DECLARE_TEST("16-byte malloc/free", 10000000, 10, {},
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));
free(malloc(16));,
);
DECLARE_TEST("16MB malloc/free", 1000000, 10, {},
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));
free(malloc(1 << 24));,
);
#define DECLARE_MEMCPY_TEST(humanSize, machineSize, count) \
DECLARE_TEST(humanSize " memcpy", count, 10, \
char *src = (char *)calloc((machineSize) + 16, 1); \
char *dst = (char *)malloc((machineSize) + 16); \
char *offsetSrc = src + 16; \
char *offsetDst = dst + 16;, \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize); \
memcpy(offsetDst, offsetSrc, machineSize);, \
free(src); \
free(dst))
DECLARE_MEMCPY_TEST("16 byte", 16, 100000000);
DECLARE_MEMCPY_TEST("1MB", 1 << 20, 10000);
static NSString *tmpFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent: @"testrand"];
#define DECLARE_WRITE_FILE_TEST(humanSize, machineSize, count, atomic) \
DECLARE_TEST(atomic ? "Write " humanSize " file (atomic)" : "Write " humanSize " file", count, 1, \
NSData *data = [[NSFileHandle fileHandleForReadingAtPath: @"/dev/random"] readDataOfLength: machineSize], \
[data writeToFile: tmpFilePath atomically: atomic], \
[[NSFileManager defaultManager] removeItemAtPath: tmpFilePath error: NULL])
DECLARE_WRITE_FILE_TEST("16 byte", 16, 10000, NO);
DECLARE_WRITE_FILE_TEST("16 byte", 16, 10000, YES);
DECLARE_WRITE_FILE_TEST("16MB", 1 << 24, 30, NO);
DECLARE_WRITE_FILE_TEST("16MB", 1 << 24, 30, YES);
#define DECLARE_READ_FILE_TEST(humanSize, machineSize, count) \
DECLARE_TEST("Read " humanSize " file", count, 1, \
NSData *data = [[NSFileHandle fileHandleForReadingAtPath: @"/dev/random"] readDataOfLength: machineSize]; \
[data writeToFile: tmpFilePath atomically: NO];, \
[[[NSData alloc] initWithContentsOfFile: tmpFilePath] release], \
[[NSFileManager defaultManager] removeItemAtPath: tmpFilePath error: NULL])
DECLARE_READ_FILE_TEST("16 byte", 16, 1000000);
DECLARE_READ_FILE_TEST("16MB", 1 << 24, 1000);
static void *stub_pthread( void * )
{
return NULL;
}
DECLARE_TEST("pthread create/join", 100000, 1, {},
pthread_t pt;
pthread_create(&pt, NULL, stub_pthread, NULL);
pthread_join(pt, NULL);,
{});
DECLARE_TEST("Dispatch queue create/destroy", 1000000, 10, {},
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));
dispatch_release(dispatch_queue_create("dummy testing queue", NULL));,
{});
DECLARE_TEST("Dispatch_sync", 10000000, 10,
dispatch_queue_t queue = dispatch_queue_create("dummy testing queue", NULL),
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});
dispatch_sync(queue, ^{});,
dispatch_release(queue));
DECLARE_TEST("Dispatch_async and wait", 100000, 10,
dispatch_queue_t queue = dispatch_queue_create("dummy testing queue", NULL);
__block volatile int done,
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);
done = 0; dispatch_async(queue, ^{ done = 1; }); while(!done);,
dispatch_release(queue));
@interface DelayedPerformClass: NSObject @end
@implementation DelayedPerformClass {
uint64_t _currentIteration;
@public
uint64_t _iterationLimit;
}
- (void)delayedPerform {
if(_currentIteration++ < _iterationLimit) {
[self performSelector: @selector(delayedPerform) withObject: nil afterDelay: 0];
} else {
CFRunLoopStop(CFRunLoopGetCurrent());
}
}
@end
DECLARE_TEST("Zero-zecond delayed perform", 100000, 1,
DelayedPerformClass *obj = [[DelayedPerformClass alloc] init];
obj->_iterationLimit = 100000;,
[obj performSelector: @selector(delayedPerform) withObject: nil afterDelay: 0];
CFRunLoopRun();
break;, /* We do our own loop, so break out of the testing loop. Uuuugly. */
[obj release]);
DECLARE_TEST("Simple JSON encode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"}),
@autoreleasepool {
[NSJSONSerialization dataWithJSONObject: dict options: 0 error: NULL];
},
{});
DECLARE_TEST("Simple JSON decode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"});
NSData *json = [NSJSONSerialization dataWithJSONObject: dict options: 0 error: NULL],
@autoreleasepool {
[NSJSONSerialization JSONObjectWithData: json options: 0 error: NULL];
},
{});
DECLARE_TEST("Simple XML plist encode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"}),
@autoreleasepool {
[NSPropertyListSerialization dataWithPropertyList:dict format: NSPropertyListXMLFormat_v1_0 options: 0 error: NULL];
},
{});
DECLARE_TEST("Simple binary plist encode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"}),
@autoreleasepool {
[NSPropertyListSerialization dataWithPropertyList:dict format: NSPropertyListBinaryFormat_v1_0 options: 0 error: NULL];
},
{});
DECLARE_TEST("Simple XML plist decode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"});
NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dict format: NSPropertyListXMLFormat_v1_0 options: 0 error: NULL],
@autoreleasepool {
[NSPropertyListSerialization propertyListWithData: plist options: 0 format: NULL error: NULL];
},
{});
DECLARE_TEST("Simple binary plist decode", 1000000, 1,
NSDictionary *dict = (@{@"one": @"won", @"two": @2, @"three": @"free"});
NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dict format: NSPropertyListBinaryFormat_v1_0 options: 0 error: NULL],
@autoreleasepool {
[NSPropertyListSerialization propertyListWithData: plist options: 0 format: NULL error: NULL];
},
{});
#pragma mark Mac-specific tests
#if !TARGET_OS_IOS
DECLARE_TEST("NSTask process spawn", 100, 1, {},
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/false"];
[task launch];
[task waitUntilExit];
[task release];,
);
DECLARE_TEST("NSWindow create/destroy", 1000, 1, {},
objc_release([[NSWindow alloc] init]),
{});
DECLARE_TEST("NSView create/destroy", 1000000, 1, {},
objc_release([[NSView alloc] init]),
{});
#endif
#pragma mark iOS-specific tests
#if TARGET_OS_IOS
DECLARE_TEST("UIView create/destroy", 1000000, 1, {},
objc_release([[UIView alloc] init]),
{});
#endif