-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathEDOServicePerfTest.m
More file actions
257 lines (221 loc) · 9.75 KB
/
EDOServicePerfTest.m
File metadata and controls
257 lines (221 loc) · 9.75 KB
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
//
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <XCTest/XCTest.h>
#import "Channel/Sources/EDOHostPort.h"
#import "Service/Sources/EDOClientService.h"
#import "Service/Sources/EDOHostService+Private.h"
#import "Service/Sources/EDOHostService.h"
#import "Service/Sources/EDOServicePort.h"
#import "Service/Sources/NSObject+EDOValueObject.h"
#import "Service/Tests/TestsBundle/EDOTestDummy.h"
#import <OCMock/OCMock.h>
// IWYU pragma: no_include "OCMArg.h"
// IWYU pragma: no_include "OCMFunctions.h"
// IWYU pragma: no_include "OCMLocation.h"
// IWYU pragma: no_include "OCMMacroState.h"
// IWYU pragma: no_include "OCMRecorder.h"
// IWYU pragma: no_include "OCMStubRecorder.h"
// IWYU pragma: no_include "OCMockObject.h"
/**
* Expose the benchmark API.
* It executes the given @c block @count times and then returns the average number of nanoseconds
* per execution.
* @see https://www.unix.com/man-page/All/3/dispatch_benchmark
*
* @param count Number of times to run.
* @param block The block to measure.
*
* @return Nanoseconds per execution.
*/
extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
// Currently a single remote call should be less or equal than 15ms under iOS 12 and 25ms after iOS
// 12.
static const uint64_t kRemoteInvocationThresholdInNano = 25e6;
// The number of times to execute the measured blocks.
static const size_t kNumOfBenchmarkExecutions = 100;
@interface EDOUITestAppPerfTests : XCTestCase
@property(readonly) EDOTestDummy *remoteDummy;
@property(readonly) Class remoteClass;
@property(readonly) id serviceBackgroundMock;
@property(readonly) id serviceMainMock;
@property(readonly) EDOHostService *serviceOnBackground;
@property(readonly) EDOHostService *serviceOnMain;
@property(readonly) EDOTestDummy *rootObject;
@property(readonly) dispatch_queue_t executionQueue;
@property(readonly) EDOTestDummy *rootObjectOnBackground;
@end
@implementation EDOUITestAppPerfTests
- (void)setUp {
[super setUp];
NSString *queueName = [NSString stringWithFormat:@"com.google.edotest.%@", self.name];
_executionQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
_rootObject = [[EDOTestDummy alloc] init];
_serviceOnBackground = [EDOHostService serviceWithPort:0
rootObject:self.rootObject
queue:self.executionQueue];
_serviceOnMain = [EDOHostService serviceWithPort:0
rootObject:[[EDOTestDummy alloc] init]
queue:dispatch_get_main_queue()];
_serviceBackgroundMock = OCMPartialMock(_serviceOnBackground);
_serviceMainMock = OCMPartialMock(_serviceOnMain);
OCMStub([_serviceBackgroundMock isObjectAliveWithPort:OCMOCK_ANY remoteAddress:0])
.ignoringNonObjectArgs()
.andReturn(NO);
OCMStub([_serviceMainMock isObjectAliveWithPort:OCMOCK_ANY remoteAddress:0])
.ignoringNonObjectArgs()
.andReturn(NO);
}
- (void)tearDown {
[self.serviceMainMock stopMocking];
[self.serviceBackgroundMock stopMocking];
_serviceMainMock = nil;
_serviceBackgroundMock = nil;
[self.serviceOnMain invalidate];
[self.serviceOnBackground invalidate];
_executionQueue = nil;
_serviceOnMain = nil;
_serviceOnBackground = nil;
_rootObject = nil;
[super tearDown];
}
- (EDOTestDummy *)remoteDummy {
return [EDOClientService rootObjectWithPort:self.serviceOnBackground.port.hostPort.port];
}
- (Class)remoteClass {
return EDO_REMOTE_CLASS(EDOTestDummy, self.serviceOnBackground.port.hostPort.port);
}
- (void)testSimpleMethodLotsTimes {
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy voidWithValuePlusOne];
}];
}
- (void)testMethodWithVariablesLotsTimes {
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy voidWithStruct:(EDOTestDummyStruct){}];
}];
}
- (void)testMethodWithOutVarLotsTimes {
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
NSError *error;
[remoteDummy voidWithErrorOut:&error];
}];
}
- (void)testMethodWithReturnLotsTimes {
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnInt];
}];
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnData];
}];
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnSelf];
}];
}
- (void)testComplicatedMethodLotsTimes {
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnIdWithInt:10];
}];
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
NSError *error;
[remoteDummy returnBoolWithError:&error];
}];
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy structWithStruct:(EDOTestDummyStruct){}];
}];
[self assertPerformBlockWithWeight:2
block:^(EDOTestDummy *remoteDummy) {
EDOTestDummy *localDummy = [[EDOTestDummy alloc] init];
[remoteDummy voidWithOutObject:&localDummy];
}]; // two remote calls
}
- (void)testClassMethodLotsTimes {
Class remoteClass = self.remoteClass;
[self assertPerformBlockWithWeight:1
block:^(EDOTestDummy *remoteDummy) {
[remoteClass classMethodWithNumber:@10];
}];
}
- (void)testIteratingReturnByValueResultLotsTimes {
uint64_t byValueResult = [self
assertPerformBlockWithWeight:1
executions:10
block:^(EDOTestDummy *remoteDummy) {
NSArray<NSNumber *> *result = [[remoteDummy returnByValue] returnLargeArray];
for (NSInteger i = 0; i < 1000; i++) {
XCTAssert(((NSNumber *)result[i]).integerValue == i);
}
}];
uint64_t byReferenceResult =
[self assertPerformBlockWithWeight:1000
executions:10
block:^(EDOTestDummy *remoteDummy) {
NSArray<NSNumber *> *result = [remoteDummy returnLargeArray];
for (NSInteger i = 0; i < 1000; i++) {
XCTAssert(((NSNumber *)result[i]).integerValue == i);
}
}];
XCTAssertLessThan(byValueResult * 100, byReferenceResult);
}
- (void)testIteratingPassByValueParameterLotsTimes {
NSMutableArray<NSNumber *> *array = [[NSMutableArray alloc] initWithCapacity:1000];
for (int i = 0; i < 1000; i++) {
[array addObject:@(i)];
}
uint64_t byValueResult =
[self assertPerformBlockWithWeight:1
executions:10
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnSumWithArray:[[array copy] passByValue]];
}];
uint64_t byReferenceResult = [self
assertPerformBlockWithWeight:1000
executions:10
block:^(EDOTestDummy *remoteDummy) {
[remoteDummy returnSumWithArray:[NSArray arrayWithArray:array]];
}];
XCTAssertLessThan(byValueResult * 100, byReferenceResult);
}
/**
* Assert the block is performed within the @weight multiple of threshold.
*/
- (uint64_t)assertPerformBlockWithWeight:(uint64_t)weight block:(void (^)(EDOTestDummy *))block {
return [self assertPerformBlockWithWeight:weight
executions:kNumOfBenchmarkExecutions
block:block];
}
/**
* Assert the block is performed @excutions times within the @weight multiple of threshold.
*/
- (uint64_t)assertPerformBlockWithWeight:(uint64_t)weight
executions:(NSInteger)executions
block:(void (^)(EDOTestDummy *))block {
EDOTestDummy *remoteDummy = self.remoteDummy;
uint64_t result = dispatch_benchmark(executions, ^{
block(remoteDummy);
});
XCTAssertLessThanOrEqual(result, kRemoteInvocationThresholdInNano * weight);
return result;
}
@end