Performance tests for Codable records

This commit is contained in:
Gwendal Roué committed 2018-08-12 14:10:38 +02:00
1 parent 46bba98f0f
commit e415a93d7e
5 files changed
+110 -1

No files matched your search

+12
View File
@@ -374,6 +374,10 @@
56873BEC1F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
56873BEF1F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
56873BF21F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
5690AFD82120589A001530EA /* InsertRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */; };
5690AFD92120589A001530EA /* InsertRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */; };
5690AFDB212058CB001530EA /* FetchRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */; };
5690AFDC212058CB001530EA /* FetchRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */; };
5690C32A1D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; };
5690C33B1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C3401D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
@@ -961,6 +965,8 @@
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = "<group>"; };
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Fixits-1.2.swift"; sourceTree = "<group>"; };
5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordCodableTests.swift; sourceTree = "<group>"; };
5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordCodableTests.swift; sourceTree = "<group>"; };
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
@@ -1717,10 +1723,12 @@
56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */,
56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */,
56DE7B2B1C41311900861EB8 /* FetchRecordClassTests.swift */,
5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */,
56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */,
56DE7B251C412FDA00861EB8 /* InsertNamedValuesTests.swift */,
56DE7B231C412F7E00861EB8 /* InsertPositionalValuesTests.swift */,
56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */,
5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */,
56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */,
56CA22211BB41565009A04C5 /* PerformanceTests.swift */,
56B8C2401CA1758F00510325 /* Realm.framework */,
@@ -2278,6 +2286,8 @@
56707201208A509C006AD95A /* DateParsingTests.swift in Sources */,
56DE7B2C1C41311900861EB8 /* FetchRecordClassTests.swift in Sources */,
56DE7B281C41302500861EB8 /* FetchNamedValuesTests.swift in Sources */,
5690AFD82120589A001530EA /* InsertRecordCodableTests.swift in Sources */,
5690AFDB212058CB001530EA /* FetchRecordCodableTests.swift in Sources */,
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */,
56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */,
560C98241C0E23BB00BF8471 /* PerformanceTests.swift in Sources */,
@@ -2300,10 +2310,12 @@
56D507841F6D7B2F00AE1C5B /* InsertRecordStructTests.swift in Sources */,
56439B3A1F4CA1DC0066043F /* FMDatabaseQueue.m in Sources */,
56439B3B1F4CA1DC0066043F /* FMDatabasePool.m in Sources */,
5690AFD92120589A001530EA /* InsertRecordCodableTests.swift in Sources */,
56439B3C1F4CA1DC0066043F /* PerformanceTests.swift in Sources */,
56D3BE721F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */,
56439B3D1F4CA1DC0066043F /* InsertNamedValuesTests.swift in Sources */,
56439B3E1F4CA1DC0066043F /* PerformanceModel.xcdatamodeld in Sources */,
5690AFDC212058CB001530EA /* FetchRecordCodableTests.swift in Sources */,
56439B3F1F4CA1DC0066043F /* FetchPositionalValuesTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -0,0 +1,27 @@
import XCTest
import SQLite3
import GRDB
#if GRDB_COMPARE
import SQLite
#endif
private let expectedRowCount = 100_000
/// Here we test the extraction of models from rows
class FetchRecordCodableTests: XCTestCase {
func testGRDB() throws {
let databasePath = Bundle(for: type(of: self)).path(forResource: "PerformanceTests", ofType: "sqlite")!
let dbQueue = try DatabaseQueue(path: databasePath)
measure {
let items = try! dbQueue.inDatabase { db in
try ItemCodable.fetchAll(db, "SELECT * FROM items")
}
XCTAssertEqual(items.count, expectedRowCount)
XCTAssertEqual(items[0].i0, 0)
XCTAssertEqual(items[1].i1, 1)
XCTAssertEqual(items[expectedRowCount-1].i9, expectedRowCount-1)
}
}
}
@@ -0,0 +1,39 @@
import XCTest
import GRDB
private let insertedRowCount = 20_000
// Here we insert records.
class InsertRecordCodableTests: XCTestCase {
func testGRDB() {
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
_ = try? FileManager.default.removeItem(atPath: databasePath)
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
}
try! FileManager.default.removeItem(atPath: databasePath)
}
measure {
_ = try? FileManager.default.removeItem(atPath: databasePath)
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
try db.execute("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
}
try! dbQueue.inTransaction { db in
for i in 0..<insertedRowCount {
try ItemCodable(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
}
return .commit
}
}
}
}
+15
View File
@@ -74,6 +74,21 @@ struct ItemStruct : FetchableRecord, PersistableRecord {
}
}
struct ItemCodable : Codable, FetchableRecord, PersistableRecord {
var i0: Int
var i1: Int
var i2: Int
var i3: Int
var i4: Int
var i5: Int
var i6: Int
var i7: Int
var i8: Int
var i9: Int
static let databaseTableName = "items"
}
class ItemClass : Record {
var i0: Int
var i1: Int
+17 -1
View File
@@ -37,7 +37,7 @@ puts <<-REPORT
Below are performance benchmarks made on for [GRDB #{grdb_version}](https://github.com/groue/GRDB.swift), [FMDB #{fmdb_version}](https://github.com/ccgus/fmdb), and [SQLite.swift #{sqlite_swift_version}](https://github.com/stephencelis/SQLite.swift). They are compared to Core Data, [Realm #{realm_version}](https://realm.io) and the raw use of the SQLite C API from Swift.
This report was generated on a MacBook Pro (15-inch, Late 2016), with Xcode 9.2, by running the following command:
This report was generated on a MacBook Pro (15-inch, Late 2016), with Xcode 9.4.1, by running the following command:
```sh
make test_performance | Tests/parsePerformanceTests.rb | Tests/generatePerformanceReport.rb
@@ -48,6 +48,7 @@ All tests use the default settings of each library. For each library, we:
- Build and consume database rows with raw SQL and column indexes (aiming at the best performance)
- Build and consume database rows with column names (sacrificing performance for maintainability)
- Build and consume records values to and from database rows (aiming at the shortest code from database to records)
- Build and consume records values to and from database rows, with help from the Codable standard protocol
- Build and consume records values to and from database rows, with [change tracking](https://github.com/groue/GRDB.swift/blob/master/README.md#record-comparison) (records know if they have unsaved changes)
As a bottom line, the raw SQLite C API is used as efficiently as possible, without any error checking.
@@ -63,6 +64,9 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho
| **Records** | | | | | | |
| Fetch | #{formatted_samples(samples, 'FetchRecordStruct').join(" | ")} |
| Insert | #{formatted_samples(samples, 'InsertRecordStruct').join(" | ")} |
| **Codable Records** | | | | | | |
| Fetch | #{formatted_samples(samples, 'FetchRecordCodable').join(" | ")} |
| Insert | #{formatted_samples(samples, 'InsertRecordCodable').join(" | ")} |
| **Records with change tracking** | | | | | | |
| Fetch | #{formatted_samples(samples, 'FetchRecordClass').join(" | ")} |
| Insert | #{formatted_samples(samples, 'InsertRecordClass').join(" | ")} |
@@ -109,6 +113,18 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho
This tests inserts 20000 records with the persistence method provided by GRDB's [PersistableRecord](https://github.com/groue/GRDB.swift/blob/master/README.md#persistablerecord-protocol) protocol.
- **Codable Records**:
- **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/FetchRecordCodableTests.swift))
This test fetches an array of 100000 record objects initiated from rows of 10 ints.
It builds records from GRDB's built-in support for the [Decodable standard protocols](https://github.com/groue/GRDB.swift/blob/master/README.md#codable-records).
- **Insert** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/InsertRecordCodableTests.swift))
This tests inserts 20000 records with the persistence method provided by GRDB's built-in support for the [Encodable standard protocols](https://github.com/groue/GRDB.swift/blob/master/README.md#codable-records).
- **Records with change tracking**:
- **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/FetchRecordClassTests.swift))