github.com/F4RD1N/gomobile@v1.0.1/bind/objc/SeqBench.m (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  #import <Foundation/Foundation.h>
     8  #import <XCTest/XCTest.h>
     9  #import "benchmark/Benchmark.h"
    10  
    11  @interface AnI : NSObject <BenchmarkI> {
    12  }
    13  @end
    14  
    15  @implementation AnI
    16  - (void)f {
    17  }
    18  @end
    19  
    20  @interface Benchmarks : NSObject <BenchmarkBenchmarks> {
    21  }
    22  @end
    23  
    24  @implementation Benchmarks
    25  - (void)manyargs:(long)p0 p1:(long)p1 p2:(long)p2 p3:(long)p3 p4:(long)p4 p5:(long)p5 p6:(long)p6 p7:(long)p7 p8:(long)p8 p9:(long)p9 {
    26  }
    27  
    28  - (id<BenchmarkI>)newI {
    29  	return [[AnI alloc] init];
    30  }
    31  
    32  - (void)noargs {
    33  }
    34  
    35  - (void)onearg:(long)p0 {
    36  }
    37  
    38  - (long)oneret {
    39  	return 0;
    40  }
    41  
    42  - (void)ref:(id<BenchmarkI>)p0 {
    43  }
    44  
    45  - (void)slice:(NSData*)p0 {
    46  }
    47  
    48  - (void)string:(NSString*)p0 {
    49  }
    50  
    51  - (NSString*)stringRetLong {
    52  	return BenchmarkLongString;
    53  }
    54  
    55  - (NSString*)stringRetShort {
    56  	return BenchmarkShortString;
    57  }
    58  
    59  - (void (^)(void))lookupBenchmark:(NSString *)name {
    60  	if ([name isEqualToString:@"Empty"]) {
    61  		return ^() {
    62  		};
    63  	} else if ([name isEqualToString:@"Noargs"]) {
    64  		return ^() {
    65  			BenchmarkNoargs();
    66  		};
    67  	} else if ([name isEqualToString:@"Onearg"]) {
    68  		return ^() {
    69  			BenchmarkOnearg(0);
    70  		};
    71  	} else if ([name isEqualToString:@"Manyargs"]) {
    72  		return ^() {
    73  			BenchmarkManyargs(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    74  		};
    75  	} else if ([name isEqualToString:@"Oneret"]) {
    76  		return ^() {
    77  			BenchmarkOneret();
    78  		};
    79  	} else if ([name isEqualToString:@"Refforeign"]) {
    80  		id<BenchmarkI> objcRef = [[AnI alloc] init];
    81  		return ^() {
    82  			BenchmarkRef(objcRef);
    83  		};
    84  	} else if ([name isEqualToString:@"Refgo"]) {
    85  		id<BenchmarkI> goRef = BenchmarkNewI();
    86  		return ^() {
    87  			BenchmarkRef(goRef);
    88  		};
    89  	} else if ([name isEqualToString:@"StringShort"]) {
    90  		return ^() {
    91  			BenchmarkString(BenchmarkShortString);
    92  		};
    93  	} else if ([name isEqualToString:@"StringLong"]) {
    94  		return ^() {
    95  			BenchmarkString(BenchmarkLongString);
    96  		};
    97  	} else if ([name isEqualToString:@"StringShortUnicode"]) {
    98  		return ^() {
    99  			BenchmarkString(BenchmarkShortStringUnicode);
   100  		};
   101  	} else if ([name isEqualToString:@"StringLongUnicode"]) {
   102  		return ^() {
   103  			BenchmarkString(BenchmarkLongStringUnicode);
   104  		};
   105  	} else if ([name isEqualToString:@"StringRetShort"]) {
   106  		return ^() {
   107  			BenchmarkStringRetShort();
   108  		};
   109  	} else if ([name isEqualToString:@"StringRetLong"]) {
   110  		return ^() {
   111  			BenchmarkStringRetLong();
   112  		};
   113  	} else if ([name isEqualToString:@"SliceShort"]) {
   114  		NSData *s = [Benchmark shortSlice];
   115  		return ^() {
   116  			BenchmarkSlice(s);
   117  		};
   118  	} else if ([name isEqualToString:@"SliceLong"]) {
   119  		NSData *s = [Benchmark longSlice];
   120  		return ^() {
   121  			BenchmarkSlice(s);
   122  		};
   123  	} else {
   124  		return nil;
   125  	}
   126  }
   127  
   128  - (void)run:(NSString*)name n:(long)n {
   129  	void (^bench)(void) = [self lookupBenchmark:name];
   130  	if (bench == nil) {
   131  		NSLog(@"Error: no such benchmark: %@", name);
   132  		return;
   133  	}
   134  	for (int i = 0; i < n; i++) {
   135  		bench();
   136  	}
   137  }
   138  
   139  - (void)runDirect:(NSString*)name n:(long)n {
   140  	void (^bench)(void) = [self lookupBenchmark:name];
   141  	if (bench == nil) {
   142  		NSLog(@"Error: no such benchmark: %@", name);
   143  		return;
   144  	}
   145  	dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   146  		for (int i = 0; i < n; i++) {
   147  			bench();
   148  		}
   149  	});
   150  }
   151  
   152  @end
   153  
   154  @interface benchmarks : XCTestCase
   155  
   156  @end
   157  
   158  @implementation benchmarks
   159  
   160  - (void)setUp {
   161  	[super setUp];
   162  
   163  	// Put setup code here. This method is called before the invocation of each test method in the class.
   164  
   165  	// In UI tests it is usually best to stop immediately when a failure occurs.
   166  	self.continueAfterFailure = NO;
   167  	// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
   168  	[[[XCUIApplication alloc] init] launch];
   169  
   170  	// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
   171  }
   172  
   173  - (void)tearDown {
   174  	// Put teardown code here. This method is called after the invocation of each test method in the class.
   175  	[super tearDown];
   176  }
   177  
   178  - (void)testBenchmark {
   179  	// Long running unit tests seem to hang. Use an XCTestExpectation and run the Go
   180  	// benchmark suite on a GCD thread.
   181  	XCTestExpectation *expectation =
   182  		[self expectationWithDescription:@"Benchmark"];
   183  
   184  	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   185  		Benchmarks *b = [[Benchmarks alloc] init];
   186  		BenchmarkRunBenchmarks(b);
   187  		[expectation fulfill];
   188  	});
   189  
   190  	[self waitForExpectationsWithTimeout:5*60.0 handler:^(NSError *error) {
   191  		if (error) {
   192  			NSLog(@"Timeout Error: %@", error);
   193  		}
   194  	}];
   195  }
   196  @end