SeqBench.m 4.5 KB

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