diff --git a/Tests/GRDBTests/DatabasePoolBackupTests.swift b/Tests/GRDBTests/DatabasePoolBackupTests.swift index 45e681893..c59afd57d 100644 --- a/Tests/GRDBTests/DatabasePoolBackupTests.swift +++ b/Tests/GRDBTests/DatabasePoolBackupTests.swift @@ -9,25 +9,25 @@ class DatabasePoolBackupTests: GRDBTestCase { let destination = try makeDatabasePool(filename: "destination.sqlite", configuration: Configuration()) try source.write { db in - try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)") - try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") - XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 1) + try db.execute(sql: "CREATE TABLE item (id INTEGER PRIMARY KEY)") + try db.execute(sql: "INSERT INTO item (id) VALUES (NULL)") + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, 1) } try source.backup(to: destination) try destination.read { db in - XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 1) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, 1) } try source.write { db in - try db.execute(sql: "DROP TABLE items") + try db.execute(sql: "DROP TABLE item") } try source.backup(to: destination) try destination.read { db in - XCTAssertFalse(try db.tableExists("items")) + XCTAssertFalse(try db.tableExists("item")) } } @@ -37,9 +37,9 @@ class DatabasePoolBackupTests: GRDBTestCase { // let destination = try makeDatabasePool(filename: "destination.sqlite") // // try source.write { db in -// try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)") -// try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") -// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 1) +// try db.execute(sql: "CREATE TABLE item (id INTEGER PRIMARY KEY)") +// try db.execute(sql: "INSERT INTO item (id) VALUES (NULL)") +// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, 1) // } // // let s1 = DispatchSemaphore(value: 0) @@ -47,7 +47,7 @@ class DatabasePoolBackupTests: GRDBTestCase { // DispatchQueue.global().async { // _ = s1.wait(timeout: .distantFuture) // try! source.writeInTransaction(.immediate) { db in -// try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") +// try db.execute(sql: "INSERT INTO item (id) VALUES (NULL)") // s2.signal() // return .commit // } @@ -62,19 +62,19 @@ class DatabasePoolBackupTests: GRDBTestCase { // }, // afterBackupStep: { // try! source.write { db in -// try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") +// try db.execute(sql: "INSERT INTO item (id) VALUES (NULL)") // } // }) // } // // try source.read { db in -// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 3) +// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, 3) // } // try destination.read { db in // // TODO: understand why the fix for https://github.com/groue/GRDB.swift/issues/102 // // had this value change from 2 to 1. // // TODO: Worse, this test is fragile. I've seen not 1 but 2 once. -// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 1) +// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, 1) // } // } } diff --git a/Tests/Performance/GRDBPerformance/FetchNamedValuesTests.swift b/Tests/Performance/GRDBPerformance/FetchNamedValuesTests.swift index eda7f0767..31accf5c2 100644 --- a/Tests/Performance/GRDBPerformance/FetchNamedValuesTests.swift +++ b/Tests/Performance/GRDBPerformance/FetchNamedValuesTests.swift @@ -6,7 +6,7 @@ import SQLite private let expectedRowCount = 100_000 -/// Here we test the extraction of values by column name. +/// Here we test the extraction of row values by column name. class FetchNamedValuesTests: XCTestCase { func testGRDB() throws { @@ -18,7 +18,7 @@ class FetchNamedValuesTests: XCTestCase { var count = 0 try! dbQueue.inDatabase { db in - let rows = try Row.fetchCursor(db, sql: "SELECT * FROM items") + let rows = try Row.fetchCursor(db, sql: "SELECT * FROM item") while let row = try rows.next() { _ = row["i0"] as Int _ = row["i1"] as Int @@ -49,7 +49,7 @@ class FetchNamedValuesTests: XCTestCase { var count = 0 dbQueue.inDatabase { db in - let rs = try! db.executeQuery("SELECT * FROM items", values: nil) + let rs = try! db.executeQuery("SELECT * FROM item", values: nil) while rs.next() { _ = rs.long(forColumn: "i0") _ = rs.long(forColumn: "i1") @@ -78,17 +78,17 @@ class FetchNamedValuesTests: XCTestCase { measure { var count = 0 - for item in try! db.prepare(itemsTable) { - _ = item[i0Column] - _ = item[i1Column] - _ = item[i2Column] - _ = item[i3Column] - _ = item[i4Column] - _ = item[i5Column] - _ = item[i6Column] - _ = item[i7Column] - _ = item[i8Column] - _ = item[i9Column] + for row in try! db.prepare(itemTable) { + _ = row[i0Column] + _ = row[i1Column] + _ = row[i2Column] + _ = row[i3Column] + _ = row[i4Column] + _ = row[i5Column] + _ = row[i6Column] + _ = row[i7Column] + _ = row[i8Column] + _ = row[i9Column] count += 1 } diff --git a/Tests/Performance/GRDBPerformance/FetchPositionalValuesTests.swift b/Tests/Performance/GRDBPerformance/FetchPositionalValuesTests.swift index 610cf2cd2..7c7196c4e 100644 --- a/Tests/Performance/GRDBPerformance/FetchPositionalValuesTests.swift +++ b/Tests/Performance/GRDBPerformance/FetchPositionalValuesTests.swift @@ -6,7 +6,7 @@ import SQLite private let expectedRowCount = 100_000 -/// Here we test the extraction of values by column index. +/// Here we test the extraction of row values by column index. class FetchPositionalValuesTests: XCTestCase { func testSQLite() throws { @@ -18,7 +18,7 @@ class FetchPositionalValuesTests: XCTestCase { measure { var count = 0 var statement: OpaquePointer? = nil - sqlite3_prepare_v2(connection, "SELECT * FROM items", -1, &statement, nil) + sqlite3_prepare_v2(connection, "SELECT * FROM item", -1, &statement, nil) loop: while true { switch sqlite3_step(statement) { @@ -60,7 +60,7 @@ class FetchPositionalValuesTests: XCTestCase { var count = 0 try! dbQueue.inDatabase { db in - let rows = try Row.fetchCursor(db, sql: "SELECT * FROM items") + let rows = try Row.fetchCursor(db, sql: "SELECT * FROM item") while let row = try rows.next() { _ = row[0] as Int _ = row[1] as Int @@ -91,7 +91,7 @@ class FetchPositionalValuesTests: XCTestCase { var count = 0 dbQueue.inDatabase { db in - let rs = try! db.executeQuery("SELECT * FROM items", values: nil) + let rs = try! db.executeQuery("SELECT * FROM item", values: nil) while rs.next() { _ = rs.long(forColumnIndex: 0) _ = rs.long(forColumnIndex: 1) @@ -120,7 +120,7 @@ class FetchPositionalValuesTests: XCTestCase { measure { var count = 0 - for row in try! db.prepare("SELECT * FROM items") { + for row in try! db.prepare("SELECT * FROM item") { // Direct Int extraction is not supported. _ = Int(row[0] as! Int64) _ = Int(row[1] as! Int64) diff --git a/Tests/Performance/GRDBPerformance/FetchRecordClassTests.swift b/Tests/Performance/GRDBPerformance/FetchRecordClassTests.swift index 67230aece..96257b0d6 100644 --- a/Tests/Performance/GRDBPerformance/FetchRecordClassTests.swift +++ b/Tests/Performance/GRDBPerformance/FetchRecordClassTests.swift @@ -7,17 +7,51 @@ import RealmSwift private let expectedRowCount = 100_000 -/// Here we test the extraction of models from rows +/// Here we test the extraction of model objects able to tell if they were +/// modified since last fetched from the database. class FetchRecordClassTests: XCTestCase { func testGRDB() throws { + /// Record is the superclass of objects able to tell if they were + /// modified since last fetched from the database. + class Item: Record { + 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 + + override class var databaseTableName: String { + "item" + } + + required init(row: GRDB.Row) { + i0 = row["i0"] + i1 = row["i1"] + i2 = row["i2"] + i3 = row["i3"] + i4 = row["i4"] + i5 = row["i5"] + i6 = row["i6"] + i7 = row["i7"] + i8 = row["i8"] + i9 = row["i9"] + super.init(row: row) + } + } + let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite") try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount) let dbQueue = try DatabaseQueue(path: url.path) measure { let items = try! dbQueue.inDatabase { db in - try ItemClass.fetchAll(db, sql: "SELECT * FROM items") + try Item.fetchAll(db) } XCTAssertEqual(items.count, expectedRowCount) XCTAssertEqual(items[0].i0, 0) diff --git a/Tests/Performance/GRDBPerformance/FetchRecordCodableTests.swift b/Tests/Performance/GRDBPerformance/FetchRecordDecodableTests.swift similarity index 59% rename from Tests/Performance/GRDBPerformance/FetchRecordCodableTests.swift rename to Tests/Performance/GRDBPerformance/FetchRecordDecodableTests.swift index c08d45cf2..1deb6f675 100644 --- a/Tests/Performance/GRDBPerformance/FetchRecordCodableTests.swift +++ b/Tests/Performance/GRDBPerformance/FetchRecordDecodableTests.swift @@ -1,22 +1,32 @@ import XCTest 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 { +/// Here we test the extraction of Decodable GRDB records. +class FetchRecordDecodableTests: XCTestCase { func testGRDB() throws { + struct Item: Decodable, FetchableRecord, TableRecord { + 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 + } + let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite") try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount) let dbQueue = try DatabaseQueue(path: url.path) measure { let items = try! dbQueue.inDatabase { db in - try ItemCodable.fetchAll(db, sql: "SELECT * FROM items") + try Item.fetchAll(db) } XCTAssertEqual(items.count, expectedRowCount) XCTAssertEqual(items[0].i0, 0) diff --git a/Tests/Performance/GRDBPerformance/FetchRecordOptimizedTests.swift b/Tests/Performance/GRDBPerformance/FetchRecordOptimizedTests.swift new file mode 100644 index 000000000..2c024f5f0 --- /dev/null +++ b/Tests/Performance/GRDBPerformance/FetchRecordOptimizedTests.swift @@ -0,0 +1,63 @@ +import XCTest +import GRDB + +private let expectedRowCount = 100_000 + +/// A record optimized for fetching performance +private struct Item: 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 + + init(row: Row) { + i0 = row[0] + i1 = row[1] + i2 = row[2] + i3 = row[3] + i4 = row[4] + i5 = row[5] + i6 = row[6] + i7 = row[7] + i8 = row[8] + i9 = row[9] + } + + static let databaseSelection: [SQLSelectable] = [ + Column("i0"), + Column("i1"), + Column("i2"), + Column("i3"), + Column("i4"), + Column("i5"), + Column("i6"), + Column("i7"), + Column("i8"), + Column("i9"), + ] +} + +/// Here we test the extraction of a plain Swift struct +class FetchRecordOptimizedTests: XCTestCase { + func testGRDB() throws { + let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite") + try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount) + let dbQueue = try DatabaseQueue(path: url.path) + + measure { + let items = try! dbQueue.inDatabase { db in + try Item.fetchAll(db) + } + XCTAssertEqual(items.count, expectedRowCount) + XCTAssertEqual(items[0].i0, 0) + XCTAssertEqual(items[1].i1, 1) + XCTAssertEqual(items[expectedRowCount-1].i9, expectedRowCount-1) + } + } +} diff --git a/Tests/Performance/GRDBPerformance/FetchRecordStructTests.swift b/Tests/Performance/GRDBPerformance/FetchRecordStructTests.swift index 433b917e9..7b78b61b7 100644 --- a/Tests/Performance/GRDBPerformance/FetchRecordStructTests.swift +++ b/Tests/Performance/GRDBPerformance/FetchRecordStructTests.swift @@ -6,7 +6,36 @@ import SQLite private let expectedRowCount = 100_000 -/// Here we test the extraction of models from rows +private struct Item { + 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 +} + +// GRDB support +extension Item: FetchableRecord, TableRecord { + init(row: GRDB.Row) { + i0 = row["i0"] + i1 = row["i1"] + i2 = row["i2"] + i3 = row["i3"] + i4 = row["i4"] + i5 = row["i5"] + i6 = row["i6"] + i7 = row["i7"] + i8 = row["i8"] + i9 = row["i9"] + } +} + +/// Here we test the extraction of a plain Swift struct class FetchRecordStructTests: XCTestCase { func testSQLite() throws { @@ -17,7 +46,7 @@ class FetchRecordStructTests: XCTestCase { measure { var statement: OpaquePointer? = nil - sqlite3_prepare_v2(connection, "SELECT * FROM items", -1, &statement, nil) + sqlite3_prepare_v2(connection, "SELECT * FROM item", -1, &statement, nil) let columnNames = (Int32(0)..<10).map { String(cString: sqlite3_column_name(statement, $0)) } let index0 = Int32(columnNames.firstIndex(of: "i0")!) @@ -31,13 +60,13 @@ class FetchRecordStructTests: XCTestCase { let index8 = Int32(columnNames.firstIndex(of: "i8")!) let index9 = Int32(columnNames.firstIndex(of: "i9")!) - var items = [ItemStruct]() + var items = [Item]() loop: while true { switch sqlite3_step(statement) { case 101 /*SQLITE_DONE*/: break loop case 100 /*SQLITE_ROW*/: - let item = ItemStruct( + let item = Item( i0: Int(sqlite3_column_int64(statement, index0)), i1: Int(sqlite3_column_int64(statement, index1)), i2: Int(sqlite3_column_int64(statement, index2)), @@ -73,7 +102,7 @@ class FetchRecordStructTests: XCTestCase { measure { let items = try! dbQueue.inDatabase { db in - try ItemStruct.fetchAll(db, sql: "SELECT * FROM items") + try Item.fetchAll(db) } XCTAssertEqual(items.count, expectedRowCount) XCTAssertEqual(items[0].i0, 0) @@ -91,11 +120,22 @@ class FetchRecordStructTests: XCTestCase { let dbQueue = FMDatabaseQueue(path: url.path)! measure { - var items = [ItemStruct]() + var items = [Item]() dbQueue.inDatabase { db in - let rs = try! db.executeQuery("SELECT * FROM items", values: nil) + let rs = try! db.executeQuery("SELECT * FROM item", values: nil) while rs.next() { - let item = ItemStruct(dictionary: rs.resultDictionary!) + let dict = rs.resultDictionary! + let item = Item( + i0: dict["i0"] as! Int, + i1: dict["i1"] as! Int, + i2: dict["i2"] as! Int, + i3: dict["i3"] as! Int, + i4: dict["i4"] as! Int, + i5: dict["i5"] as! Int, + i6: dict["i6"] as! Int, + i7: dict["i7"] as! Int, + i8: dict["i8"] as! Int, + i9: dict["i9"] as! Int) items.append(item) } } @@ -112,9 +152,9 @@ class FetchRecordStructTests: XCTestCase { let db = try Connection(url.path) measure { - var items = [ItemStruct]() - for row in try! db.prepare(itemsTable) { - let item = ItemStruct( + var items = [Item]() + for row in try! db.prepare(itemTable) { + let item = Item( i0: row[i0Column], i1: row[i1Column], i2: row[i2Column], diff --git a/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.pbxproj b/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.pbxproj index 889ee5996..94644bf5e 100755 --- a/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.pbxproj +++ b/Tests/Performance/GRDBPerformance/GRDBPerformance.xcodeproj/project.pbxproj @@ -27,10 +27,14 @@ 56707201208A509C006AD95A /* DateParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567071FA208A509C006AD95A /* DateParsingTests.swift */; }; 5679870223A37A6A0076902D /* GRDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 567986B923A378CD0076902D /* GRDB.framework */; }; 5679870523A37A790076902D /* GRDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 567986B923A378CD0076902D /* GRDB.framework */; }; - 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 */; }; + 5690AFD82120589A001530EA /* InsertRecordEncodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */; }; + 5690AFD92120589A001530EA /* InsertRecordEncodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */; }; + 5690AFDB212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */; }; + 5690AFDC212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */; }; + 56B6D0E52618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; }; + 56B6D0E62618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; }; + 56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; }; + 56B6D0EB2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; }; 56D3BE711F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; }; 56D3BE721F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; }; 56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */; }; @@ -133,8 +137,10 @@ 565BC5E72517645D00D2B53E /* Generation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Generation.swift; sourceTree = ""; }; 567071FA208A509C006AD95A /* DateParsingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateParsingTests.swift; sourceTree = ""; }; 567986AD23A378CD0076902D /* GRDB.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GRDB.xcodeproj; path = ../../../GRDB.xcodeproj; sourceTree = ""; }; - 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordCodableTests.swift; sourceTree = ""; }; - 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordCodableTests.swift; sourceTree = ""; }; + 5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordEncodableTests.swift; sourceTree = ""; }; + 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordDecodableTests.swift; sourceTree = ""; }; + 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordOptimizedTests.swift; sourceTree = ""; }; + 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordOptimizedTests.swift; sourceTree = ""; }; 56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordClassTests.swift; sourceTree = ""; }; 56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBPerformanceComparisonTests-Bridging.h"; sourceTree = ""; }; 56CA22211BB41565009A04C5 /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = ""; }; @@ -206,13 +212,15 @@ 56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */, 56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */, 56DE7B2B1C41311900861EB8 /* FetchRecordClassTests.swift */, - 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */, + 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */, + 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */, 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */, 565BC5E72517645D00D2B53E /* Generation.swift */, 56DE7B251C412FDA00861EB8 /* InsertNamedValuesTests.swift */, 56DE7B231C412F7E00861EB8 /* InsertPositionalValuesTests.swift */, 56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */, - 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */, + 5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */, + 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */, 56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */, 56CA22211BB41565009A04C5 /* PerformanceTests.swift */, 56DE7B341C42B37E00861EB8 /* CoreData.framework */, @@ -426,11 +434,13 @@ 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 */, + 5690AFD82120589A001530EA /* InsertRecordEncodableTests.swift in Sources */, + 56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */, + 5690AFDB212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */, 56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */, 56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */, 560C98241C0E23BB00BF8471 /* PerformanceTests.swift in Sources */, + 56B6D0E52618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */, 56DE7B261C412FDA00861EB8 /* InsertNamedValuesTests.swift in Sources */, 56DE7B2A1C4130AF00861EB8 /* FetchPositionalValuesTests.swift in Sources */, ); @@ -446,12 +456,14 @@ 56439B381F4CA1DC0066043F /* FetchNamedValuesTests.swift in Sources */, 56439B391F4CA1DC0066043F /* InsertPositionalValuesTests.swift in Sources */, 56D507841F6D7B2F00AE1C5B /* InsertRecordStructTests.swift in Sources */, - 5690AFD92120589A001530EA /* InsertRecordCodableTests.swift in Sources */, + 5690AFD92120589A001530EA /* InsertRecordEncodableTests.swift in Sources */, + 56B6D0EB2618C00C003CC455 /* InsertRecordOptimizedTests.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 */, + 56B6D0E62618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */, + 5690AFDC212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */, 56439B3F1F4CA1DC0066043F /* FetchPositionalValuesTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Tests/Performance/GRDBPerformance/Generation.swift b/Tests/Performance/GRDBPerformance/Generation.swift index a0bf32070..128bce917 100644 --- a/Tests/Performance/GRDBPerformance/Generation.swift +++ b/Tests/Performance/GRDBPerformance/Generation.swift @@ -8,17 +8,17 @@ import RealmSwift func generateSQLiteDatabaseIfMissing(at url: URL, insertedRowCount: Int) throws { try DatabaseQueue(path: url.path).write { db in - if try db.tableExists("items") { - let count = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")! + if try db.tableExists("item") { + let count = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")! if count == insertedRowCount { return } else { - try db.execute(sql: "DROP TABLE items") + try db.execute(sql: "DROP TABLE item") } } - try db.execute(sql: "CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)") + try db.execute(sql: "CREATE TABLE item (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)") - let statement = try! db.makeUpdateStatement(sql: "INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)") + let statement = try! db.makeUpdateStatement(sql: "INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)") for i in 0.. Void in db.shouldCacheStatements = true for i in 0.. Void in db.shouldCacheStatements = true for i in 0.. UpdateStatement { + try db.makeUpdateStatement(literal: """ + INSERT INTO \(self) ( + \(CodingKeys.i0), + \(CodingKeys.i1), + \(CodingKeys.i2), + \(CodingKeys.i3), + \(CodingKeys.i4), + \(CodingKeys.i5), + \(CodingKeys.i6), + \(CodingKeys.i7), + \(CodingKeys.i8), + \(CodingKeys.i9)) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """) + } + + func insert(with statement: UpdateStatement) throws { + statement.setUncheckedArguments( + i0, + i1, + i2, + i3, + i4, + i5, + i6, + i7, + i8, + i9) + try statement.execute() + } +} + +class InsertRecordOptimizedTests: 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, sql: "SELECT COUNT(*) FROM item")!, insertedRowCount) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM item")!, 0) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM item")!, 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(sql: "CREATE TABLE item (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 + let statement = try Item.optimizedInsertStatement(db) + for i in 0..("i0") let i1Column = Expression("i1") let i2Column = Expression("i2") @@ -164,30 +21,9 @@ let i8Column = Expression("i8") let i9Column = Expression("i9") -// MARK:- FMDB - -extension ItemClass { - - convenience init(dictionary: [AnyHashable: Any]) { - self.init( - i0: dictionary["i0"] as! Int, - i1: dictionary["i1"] as! Int, - i2: dictionary["i2"] as! Int, - i3: dictionary["i3"] as! Int, - i4: dictionary["i4"] as! Int, - i5: dictionary["i5"] as! Int, - i6: dictionary["i6"] as! Int, - i7: dictionary["i7"] as! Int, - i8: dictionary["i8"] as! Int, - i9: dictionary["i9"] as! Int) - } - -} - - // MARK: - Realm -class RealmItem : RealmSwift.Object { +class RealmItem: RealmSwift.Object { @objc dynamic var i0: Int = 0 @objc dynamic var i1: Int = 0 @objc dynamic var i2: Int = 0 diff --git a/Tests/generatePerformanceReport.rb b/Tests/generatePerformanceReport.rb index 1c23c5623..1dcb8cd83 100755 --- a/Tests/generatePerformanceReport.rb +++ b/Tests/generatePerformanceReport.rb @@ -100,8 +100,11 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho | 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(" | ")} | +| Fetch | #{formatted_samples(samples, 'FetchRecordDecodable').join(" | ")} | +| Insert | #{formatted_samples(samples, 'InsertRecordEncodable').join(" | ")} | +| **Optimized Records** | | | | | | | +| Fetch | #{formatted_samples(samples, 'FetchRecordOptimized').join(" | ")} | +| Insert | #{formatted_samples(samples, 'InsertRecordOptimized').join(" | ")} | | **Records with change tracking** | | | | | | | | Fetch | #{formatted_samples(samples, 'FetchRecordClass').join(" | ")} | | Insert | #{formatted_samples(samples, 'InsertRecordClass').join(" | ")} | @@ -150,16 +153,26 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho - **Codable Records**: - - **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/FetchRecordCodableTests.swift)) + - **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/FetchRecordDecodableTests.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/GRDBPerformance/InsertRecordCodableTests.swift)) + - **Insert** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/InsertRecordEncodableTests.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). +- **Optimized Records**: + + - **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/FetchRecordDecodableTests.swift)) + + This test shows how to optimize Decodable Records for fetching. + + - **Insert** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/InsertRecordEncodableTests.swift)) + + This test shows how to optimize Encodable Records for batch inserts. + - **Records with change tracking**: - **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/GRDBPerformance/FetchRecordClassTests.swift))