Skip to content

Commit

Permalink
Add new benchmarking scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters committed Apr 24, 2013
1 parent 250732a commit 3d9dcce
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 51 deletions.
2 changes: 1 addition & 1 deletion JSON/employees.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions JSON/employees_and_companies.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions JSON/employees_with_nested_companies.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions RKBenchmark.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
25499D2E172836B6004F0FDA /* _RKBEmployee.m in Sources */ = {isa = PBXBuildFile; fileRef = 25499D28172836B5004F0FDA /* _RKBEmployee.m */; };
25499D2F172836B6004F0FDA /* RKBCompany.m in Sources */ = {isa = PBXBuildFile; fileRef = 25499D2A172836B6004F0FDA /* RKBCompany.m */; };
25499D30172836B6004F0FDA /* RKBEmployee.m in Sources */ = {isa = PBXBuildFile; fileRef = 25499D2C172836B6004F0FDA /* RKBEmployee.m */; };
25499D3D172883F7004F0FDA /* employees_and_companies.json in Resources */ = {isa = PBXBuildFile; fileRef = 25499D3B172883F7004F0FDA /* employees_and_companies.json */; };
25499D3E172883F7004F0FDA /* employees_with_nested_companies.json in Resources */ = {isa = PBXBuildFile; fileRef = 25499D3C172883F7004F0FDA /* employees_with_nested_companies.json */; };
C70C120F09D6402D94DA29A0 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 156378862EAE4CE3B6940A66 /* libPods.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -67,6 +69,8 @@
25499D2A172836B6004F0FDA /* RKBCompany.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKBCompany.m; sourceTree = "<group>"; };
25499D2B172836B6004F0FDA /* RKBEmployee.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKBEmployee.h; sourceTree = "<group>"; };
25499D2C172836B6004F0FDA /* RKBEmployee.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKBEmployee.m; sourceTree = "<group>"; };
25499D3B172883F7004F0FDA /* employees_and_companies.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = employees_and_companies.json; sourceTree = "<group>"; };
25499D3C172883F7004F0FDA /* employees_with_nested_companies.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = employees_with_nested_companies.json; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -149,6 +153,8 @@
25499D21172831C7004F0FDA /* JSON */ = {
isa = PBXGroup;
children = (
25499D3B172883F7004F0FDA /* employees_and_companies.json */,
25499D3C172883F7004F0FDA /* employees_with_nested_companies.json */,
25499D22172831C7004F0FDA /* employees.json */,
);
path = JSON;
Expand Down Expand Up @@ -225,6 +231,8 @@
25499D1017281C56004F0FDA /* [email protected] in Resources */,
25499D1217281C56004F0FDA /* [email protected] in Resources */,
25499D23172831C7004F0FDA /* employees.json in Resources */,
25499D3D172883F7004F0FDA /* employees_and_companies.json in Resources */,
25499D3E172883F7004F0FDA /* employees_with_nested_companies.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
172 changes: 141 additions & 31 deletions RKBenchmark/RKBAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ typedef NS_ENUM(NSUInteger, RKBenchmarkPersistentStoreType) {

@interface RKBAppDelegate ()
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) RKEntityMapping *employeeMapping;
@end

@implementation RKBAppDelegate
Expand Down Expand Up @@ -60,7 +59,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
@"position": @"position",
@"salary": @"salary",
@"termination_date": @"terminationDate"}];
self.employeeMapping = employeeMapping;
employeeMapping.identificationAttributes = @[ @"employeeID" ];

RKEntityMapping *companyMapping = [[RKEntityMapping alloc] initWithEntity:self.managedObjectModel.entitiesByName[@"Company"]];
companyMapping.identificationAttributes = @[ @"companyID" ];
[companyMapping addAttributeMappingsFromDictionary:@{
@"active": @"active",
@"company_id": @"companyID",
@"founding_date": @"foundingDate",
@"industry": @"industry",
@"name": @"name",
@"tax_id": @"taxID",
@"url": @"url" }];

// Execute Benchmarks
void (^setFetchRequestCache)(RKManagedObjectImporter *) = ^(RKManagedObjectImporter *importer) {
Expand All @@ -75,39 +85,139 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
void (^importFiveThousandEmployees)(RKManagedObjectImporter *) = ^(RKManagedObjectImporter *importer) {
NSError *error = nil;
NSUInteger count = [importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"employees" ofType:@"json"]
withMapping:self.employeeMapping
withMapping:employeeMapping
keyPath:@"employees"
error:&error];
NSAssert(count == 5000 && error == nil, @"Import failed: %@", error);
};

[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setFetchRequestCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setInMemoryCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setNilCache
benchmark:importFiveThousandEmployees];

[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setFetchRequestCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setInMemoryCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setNilCache
benchmark:importFiveThousandEmployees];
BOOL success = [importer finishImporting:&error];
NSAssert(success, @"Failed to finish import due to error: %@", error);
};
void (^importFiveThousandEmployeesWithNestedCompanies)(RKManagedObjectImporter *) = ^(RKManagedObjectImporter *importer) {
NSError *error = nil;
NSUInteger count = [importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"employees_with_nested_companies" ofType:@"json"]
withMapping:employeeMapping
keyPath:@"employees"
error:&error];
NSAssert(count == 5000 && error == nil, @"Import failed: %@", error);
BOOL success = [importer finishImporting:&error];
NSAssert(success, @"Failed to finish import due to error: %@", error);
};
void (^importFiveThousandEmployeesWithThreeHundredAndFiftyConnectedCompanies)(RKManagedObjectImporter *) = ^(RKManagedObjectImporter *importer) {
NSError *error = nil;
NSUInteger count = [importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"employees_and_companies" ofType:@"json"]
withMapping:employeeMapping
keyPath:@"employees"
error:&error];
NSAssert(count == 5000 && error == nil, @"Import of %d employees failed: %@", count, error);
count = [importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"employees_and_companies" ofType:@"json"]
withMapping:companyMapping
keyPath:@"companies"
error:&error];
NSAssert(count == 350 && error == nil, @"Import of %d companies failed: %@", count, error);
BOOL success = [importer finishImporting:&error];
NSAssert(success, @"Failed to finish import due to error: %@", error);
};

exit(0);
// Benchmark on a dispatch queue for easy thread separation in Instruments
dispatch_queue_t benchmarkingQueue = dispatch_queue_create("org.restkit.benchmarking", NULL);
dispatch_async(benchmarkingQueue, ^{
// Benchmark Attributes Only
[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setFetchRequestCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setInMemoryCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with In Memory Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setNilCache
benchmark:importFiveThousandEmployees];

[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setFetchRequestCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setInMemoryCache
benchmark:importFiveThousandEmployees];
[self benchmarkImportWithName:@"Import 5000 Employees with SQLite Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setNilCache
benchmark:importFiveThousandEmployees];


// Benchmark Nested Relationships
[employeeMapping addRelationshipMappingWithSourceKeyPath:@"company" mapping:companyMapping];
[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via In Memory Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setFetchRequestCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via In Memory Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setInMemoryCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via In Memory Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setNilCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];

[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via SQLite Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setFetchRequestCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via SQLite Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setInMemoryCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Nested Company via SQLite Store + nil Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setNilCache
benchmark:importFiveThousandEmployeesWithNestedCompanies];

// Benchmark Connected Relationships
// NOTE: You cannot map an object with connections with a `nil` cache
RKRelationshipMapping *companyMapping = [employeeMapping propertyMappingsBySourceKeyPath][@"company"];
[employeeMapping removePropertyMapping:companyMapping];
[employeeMapping addConnectionForRelationship:@"company" connectedBy:@"companyID"];
[self benchmarkImportWithName:@"Import 5000 Employees with Connected Company via In Memory Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setFetchRequestCache
benchmark:importFiveThousandEmployeesWithThreeHundredAndFiftyConnectedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Connected Company via In Memory Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setInMemoryCache
benchmark:importFiveThousandEmployeesWithThreeHundredAndFiftyConnectedCompanies];

[self benchmarkImportWithName:@"Import 5000 Employees with Connected Company via SQLite Store + Fetch Request Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setFetchRequestCache
benchmark:importFiveThousandEmployeesWithThreeHundredAndFiftyConnectedCompanies];
[self benchmarkImportWithName:@"Import 5000 Employees with Connected Company via SQLite Store + In Memory Cache"
persistentStoreType:RKBenchmarkPersistentStoreTypeSQLite
configure:setInMemoryCache
benchmark:importFiveThousandEmployeesWithThreeHundredAndFiftyConnectedCompanies];

// Benchmark Attributes w/ Modification Detection Enabled
employeeMapping.identificationAttributes = @[ @"employeeID" ];
employeeMapping.modificationKey = @"createdAt";
[self benchmarkImportWithName:@"Import 5000 Employees twice with In Memory Store, In Memory Cache + Modification Key"
persistentStoreType:RKBenchmarkPersistentStoreTypeInMemory
configure:setInMemoryCache
benchmark:^(RKManagedObjectImporter *importer) {
[RKBenchmark report:@"Initial Import" executionBlock:^{
importFiveThousandEmployees(importer);
}];
[RKBenchmark report:@"Secondary Import" executionBlock:^{
importFiveThousandEmployees(importer);
}];
importFiveThousandEmployees(importer);
}];

exit(0);
});
return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="2061" systemVersion="12D78" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="Company" representedClassName="RKBCompany" syncable="YES">
<attribute name="active" optional="YES" attributeType="Boolean" syncable="YES"/>
<attribute name="companyID" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
<attribute name="companyID" optional="YES" attributeType="Integer 16" defaultValueString="0" indexed="YES" syncable="YES"/>
<attribute name="foundingDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="industry" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
Expand All @@ -16,7 +16,7 @@
<attribute name="companyID" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
<attribute name="createdAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="emailAddress" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="employeeID" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
<attribute name="employeeID" optional="YES" attributeType="Integer 16" defaultValueString="0" indexed="YES" syncable="YES"/>
<attribute name="hireDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="position" optional="YES" attributeType="String" syncable="YES"/>
Expand Down
Loading

0 comments on commit 3d9dcce

Please sign in to comment.