-
Notifications
You must be signed in to change notification settings - Fork 23
/
TestRevisionRewritingPerformance.m
260 lines (206 loc) · 8.75 KB
/
TestRevisionRewritingPerformance.m
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
/*
Copyright (C) 2024 Quentin Mathe
Date: January 2024
License: MIT (see COPYING)
*/
#import "TestCommon.h"
@interface NSString (RandomStringGeneration)
+ (NSString *)defaultAlphabet;
+ (NSString *)randomStringWithAlphabet: (NSString *)alphabet length: (NSUInteger)len;
+ (NSString *)randomString;
@end
@interface TestRevisionRewritingPerformance : EditingContextTestCase <UKTest>
{
int revCounter;
}
@end
@implementation TestRevisionRewritingPerformance
#define NUM_PERSISTENT_ROOTS 50
#define NUM_COMMITS 100
- (NSArray *)commitPersistentRootsWithEntityName: (NSString *)entityName
count: (int)nbOfPersistentRoots
studentCount: (int)studentCount
{
NSMutableArray *proots = [NSMutableArray new];
for (int i = 0; i < nbOfPersistentRoots; i++)
{
COPersistentRoot *proot = [ctx insertNewPersistentRootWithEntityName: entityName];
[self addNewStudents: studentCount toPerson: proot.rootObject];
[proots addObject: proot];
revCounter++;
}
[ctx commit];
return proots;
}
- (void)updatePerson: (Person *)person
{
uint32_t draw = arc4random_uniform(3);
switch (draw)
{
case 0:
person.name = [NSString randomString];
person.role = [NSString randomString];
break;
case 1:
person.summary = [NSString randomStringWithAlphabet: [NSString defaultAlphabet] length: 100];
person.age = arc4random() % 100;
person.iconData = [[NSString randomString] dataUsingEncoding: NSUTF8StringEncoding];
break;
case 2:
person.streetAddress = [NSString randomString];
person.city = [NSString randomString];
person.administrativeArea = [NSString randomString];
person.postalCode = [NSString randomString];
person.country = [NSString randomString];
break;
case 3:
person.phoneNumber = [NSString randomStringWithAlphabet: @"0123456789" length: 10];
person.emailAddress = [NSString randomString];
person.website = [NSURL URLWithString: [NSString stringWithFormat: @"http://www.%@.com",
[NSString randomString]]];
break;
default:
break;
}
}
- (void)addNewStudents: (int)count toPerson: (Person *)person
{
if (count == 0)
{
return;
}
NSMutableSet *students = [person.students mutableCopy];
for (int i = 0; i < count; i++)
{
Person *student = [[Person alloc] initWithObjectGraphContext: person.objectGraphContext];
[self updatePerson: person];
[students addObject: student];
}
person.students = students;
}
- (void)makeCommitToPersistentRoots: (NSArray *)proots studentCount: (NSInteger)count
{
for (COPersistentRoot *proot in proots)
{
Person *person = (Person *)[proot rootObject];
[self updatePerson: person];
[self addNewStudents: count toPerson: person];
revCounter++;
}
[ctx commit];
}
- (void)testSingleItem
{
ctx.recordingUndo = NO;
NSDate *startDate = [NSDate date];
NSArray *proots = [self commitPersistentRootsWithEntityName: @"Person"
count: NUM_PERSISTENT_ROOTS
studentCount: 0];
NSTimeInterval creationTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to commit %d new persistent roots: %0.2fs", NUM_PERSISTENT_ROOTS, creationTime);
startDate = [NSDate date];
for (int commit = 0; commit < (NUM_COMMITS - 1); commit++)
{
[self makeCommitToPersistentRoots: proots studentCount: 0];
}
NSTimeInterval commitTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to make %d commits: %0.2fs", (NUM_COMMITS - 1), commitTime);
startDate = [NSDate date];
NSInteger __block migrationCounter = 0;
[ctx.store migrateRevisionsToVersion: 1
withHandler: ^COItemGraph *(COItemGraph *oldItemGraph, int64_t oldVersion, int64_t newVersion)
{
NSMutableArray *newItems = [NSMutableArray array];
for (COItem *oldItem in oldItemGraph.items)
{
ETAssert(oldVersion == oldItem.packageVersion);
COMutableItem *newItem = [oldItem mutableCopy];
newItem.packageVersion = newVersion;
[newItems addObject: newItem];
}
migrationCounter += 1;
return [[COItemGraph alloc] initWithItems: newItems
rootItemUUID: oldItemGraph.rootItemUUID];
}];
UKIntsEqual(NUM_PERSISTENT_ROOTS * NUM_COMMITS, revCounter);
UKIntsEqual(revCounter, migrationCounter);
const NSTimeInterval rewriteTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to rewrite %d store revisions: %0.2fs", revCounter, rewriteTime);
}
- (void)testFixedItemCount
{
ctx.recordingUndo = NO;
NSDate *startDate = [NSDate date];
NSArray *proots = [self commitPersistentRootsWithEntityName: @"Person"
count: NUM_PERSISTENT_ROOTS
studentCount: 50];
NSTimeInterval creationTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to commit %d new persistent roots: %0.2fs", NUM_PERSISTENT_ROOTS, creationTime);
startDate = [NSDate date];
for (int commit = 0; commit < (NUM_COMMITS - 1); commit++)
{
[self makeCommitToPersistentRoots: proots studentCount: 0];
}
NSTimeInterval commitTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to make %d commits: %0.2fs", (NUM_COMMITS - 1), commitTime);
startDate = [NSDate date];
NSInteger __block migrationCounter = 0;
[ctx.store migrateRevisionsToVersion: 1
withHandler: ^COItemGraph *(COItemGraph *oldItemGraph, int64_t oldVersion, int64_t newVersion)
{
NSMutableArray *newItems = [NSMutableArray array];
for (COItem *oldItem in oldItemGraph.items)
{
ETAssert(oldVersion == oldItem.packageVersion);
COMutableItem *newItem = [oldItem mutableCopy];
newItem.packageVersion = newVersion;
[newItems addObject: newItem];
}
migrationCounter += 1;
return [[COItemGraph alloc] initWithItems: newItems
rootItemUUID: oldItemGraph.rootItemUUID];
}];
UKIntsEqual(NUM_PERSISTENT_ROOTS * NUM_COMMITS, revCounter);
UKIntsEqual(revCounter, migrationCounter);
const NSTimeInterval rewriteTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to rewrite %d store revisions: %0.2fs", revCounter, rewriteTime);
}
- (void)testGrowingItemCount
{
ctx.recordingUndo = NO;
NSDate *startDate = [NSDate date];
NSArray *proots = [self commitPersistentRootsWithEntityName: @"Person"
count: NUM_PERSISTENT_ROOTS
studentCount: 0];
NSTimeInterval creationTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to commit %d new persistent roots: %0.2fs", NUM_PERSISTENT_ROOTS, creationTime);
startDate = [NSDate date];
for (int commit = 0; commit < (NUM_COMMITS - 1); commit++)
{
[self makeCommitToPersistentRoots: proots studentCount: 1];
}
NSTimeInterval commitTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to make %d commits: %0.2fs", (NUM_COMMITS - 1), commitTime);
startDate = [NSDate date];
NSInteger __block migrationCounter = 0;
[ctx.store migrateRevisionsToVersion: 1
withHandler: ^COItemGraph *(COItemGraph *oldItemGraph, int64_t oldVersion, int64_t newVersion)
{
NSMutableArray *newItems = [NSMutableArray array];
for (COItem *oldItem in oldItemGraph.items)
{
ETAssert(oldVersion == oldItem.packageVersion);
COMutableItem *newItem = [oldItem mutableCopy];
newItem.packageVersion = newVersion;
[newItems addObject: newItem];
}
migrationCounter += 1;
return [[COItemGraph alloc] initWithItems: newItems
rootItemUUID: oldItemGraph.rootItemUUID];
}];
UKIntsEqual(NUM_PERSISTENT_ROOTS * NUM_COMMITS, revCounter);
UKIntsEqual(revCounter, migrationCounter);
const NSTimeInterval rewriteTime = [[NSDate date] timeIntervalSinceDate: startDate];
NSLog(@"Time to rewrite %d store revisions: %0.2fs", revCounter, rewriteTime);
}
@end