Performance tests for optimized records

This commit is contained in:
Gwendal Roué committed 2021-04-03 18:02:15 +02:00
1 parent eaf830c05c
commit 5f9911ff0d
17 files changed
+470 -290

No files matched your search

+13 -13
View File
@@ -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)
// }
// }
}
@@ -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
}
@@ -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)
@@ -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)
@@ -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)
@@ -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)
}
}
}
@@ -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],
@@ -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 = "<group>"; };
567071FA208A509C006AD95A /* DateParsingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateParsingTests.swift; sourceTree = "<group>"; };
567986AD23A378CD0076902D /* GRDB.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GRDB.xcodeproj; path = ../../../GRDB.xcodeproj; 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>"; };
5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordEncodableTests.swift; sourceTree = "<group>"; };
5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordDecodableTests.swift; sourceTree = "<group>"; };
56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordOptimizedTests.swift; sourceTree = "<group>"; };
56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordOptimizedTests.swift; sourceTree = "<group>"; };
56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordClassTests.swift; sourceTree = "<group>"; };
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBPerformanceComparisonTests-Bridging.h"; sourceTree = "<group>"; };
56CA22211BB41565009A04C5 /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = "<group>"; };
@@ -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;
@@ -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..<insertedRowCount {
try statement.execute(arguments: [i, i, i, i, i, i, i, i, i, i])
}
@@ -15,9 +15,9 @@ class InsertNamedValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -27,11 +27,11 @@ class InsertNamedValuesTests: XCTestCase {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
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)")
}
try! dbQueue.inTransaction { db in
let statement = try! db.makeUpdateStatement(sql: "INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (:i0, :i1, :i2, :i3, :i4, :i5, :i6, :i7, :i8, :i9)")
let statement = try! db.makeUpdateStatement(sql: "INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (:i0, :i1, :i2, :i3, :i4, :i5, :i6, :i7, :i8, :i9)")
for i in 0..<insertedRowCount {
try statement.execute(arguments: ["i0": i, "i1": i, "i2": i, "i3": i, "i4": i, "i5": i, "i6": i, "i7": i, "i8": i, "i9": i])
}
@@ -47,9 +47,9 @@ class InsertNamedValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -59,13 +59,13 @@ class InsertNamedValuesTests: XCTestCase {
let dbQueue = FMDatabaseQueue(path: databasePath)!
dbQueue.inDatabase { db in
db.executeStatements("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
db.executeStatements("CREATE TABLE item (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
}
dbQueue.inTransaction { (db, rollback) -> Void in
db.shouldCacheStatements = true
for i in 0..<insertedRowCount {
db.executeUpdate("INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (:i0, :i1, :i2, :i3, :i4, :i5, :i6, :i7, :i8, :i9)", withParameterDictionary: ["i0": i, "i1": i, "i2": i, "i3": i, "i4": i, "i5": i, "i6": i, "i7": i, "i8": i, "i9": i])
db.executeUpdate("INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (:i0, :i1, :i2, :i3, :i4, :i5, :i6, :i7, :i8, :i9)", withParameterDictionary: ["i0": i, "i1": i, "i2": i, "i3": i, "i4": i, "i5": i, "i6": i, "i7": i, "i8": i, "i9": i])
}
}
}
@@ -77,9 +77,9 @@ class InsertNamedValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -88,7 +88,7 @@ class InsertNamedValuesTests: XCTestCase {
_ = try? FileManager.default.removeItem(atPath: databasePath)
let db = try! Connection(databasePath)
try! db.run(itemsTable.create { t in
try! db.run(itemTable.create { t in
t.column(i0Column)
t.column(i1Column)
t.column(i2Column)
@@ -103,7 +103,7 @@ class InsertNamedValuesTests: XCTestCase {
try! db.transaction {
for i in 0..<insertedRowCount {
_ = try db.run(itemsTable.insert(
_ = try db.run(itemTable.insert(
i0Column <- i,
i1Column <- i,
i2Column <- i,
@@ -15,9 +15,9 @@ class InsertPositionalValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -27,12 +27,12 @@ class InsertPositionalValuesTests: XCTestCase {
var connection: OpaquePointer? = nil
sqlite3_open_v2(databasePath, &connection, 0x00000004 /*SQLITE_OPEN_CREATE*/ | 0x00000002 /*SQLITE_OPEN_READWRITE*/, nil)
sqlite3_exec(connection, "CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)", nil, nil, nil)
sqlite3_exec(connection, "CREATE TABLE item (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)", nil, nil, nil)
sqlite3_exec(connection, "BEGIN TRANSACTION", nil, nil, nil)
var statement: OpaquePointer? = nil
sqlite3_prepare_v2(connection, "INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)", -1, &statement, nil)
sqlite3_prepare_v2(connection, "INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)", -1, &statement, nil)
for i in Int64(0)..<Int64(insertedRowCount) {
sqlite3_reset(statement)
@@ -61,9 +61,9 @@ class InsertPositionalValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -72,11 +72,11 @@ class InsertPositionalValuesTests: XCTestCase {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
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)")
}
try! dbQueue.inTransaction { db in
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..<insertedRowCount {
statement.setUncheckedArguments(i, i, i, i, i, i, i, i, i, i)
try statement.execute()
@@ -93,9 +93,9 @@ class InsertPositionalValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -104,13 +104,13 @@ class InsertPositionalValuesTests: XCTestCase {
let dbQueue = FMDatabaseQueue(path: databasePath)!
dbQueue.inDatabase { db in
db.executeStatements("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
db.executeStatements("CREATE TABLE item (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
}
dbQueue.inTransaction { (db, rollback) -> Void in
db.shouldCacheStatements = true
for i in 0..<insertedRowCount {
db.executeUpdate("INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)", withArgumentsIn: [i, i, i, i, i, i, i, i, i, i])
db.executeUpdate("INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)", withArgumentsIn: [i, i, i, i, i, i, i, i, i, i])
}
}
}
@@ -122,9 +122,9 @@ class InsertPositionalValuesTests: XCTestCase {
defer {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -132,7 +132,7 @@ class InsertPositionalValuesTests: XCTestCase {
_ = try? FileManager.default.removeItem(atPath: databasePath)
let db = try! Connection(databasePath)
try! db.run(itemsTable.create { t in
try! db.run(itemTable.create { t in
t.column(i0Column)
t.column(i1Column)
t.column(i2Column)
@@ -146,7 +146,7 @@ class InsertPositionalValuesTests: XCTestCase {
})
try! db.transaction {
let stmt = try! db.prepare("INSERT INTO items (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)")
let stmt = try! db.prepare("INSERT INTO item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)")
for i in 0..<insertedRowCount {
try stmt.run(i, i, i, i, i, i, i, i, i, i)
}
@@ -7,19 +7,67 @@ import RealmSwift
private let insertedRowCount = 20_000
// Here we insert records.
// Here we insert record classes.
class InsertRecordClassTests: XCTestCase {
func testGRDB() {
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"
}
init(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int, i7: Int, i8: Int, i9: Int) {
self.i0 = i0
self.i1 = i1
self.i2 = i2
self.i3 = i3
self.i4 = i4
self.i5 = i5
self.i6 = i6
self.i7 = i7
self.i8 = i8
self.i9 = i9
super.init()
}
required init(row: Row) {
fatalError("init(row:) has not been implemented")
}
override func encode(to container: inout PersistenceContainer) {
container["i0"] = i0
container["i1"] = i1
container["i2"] = i2
container["i3"] = i3
container["i4"] = i4
container["i5"] = i5
container["i6"] = i6
container["i7"] = i7
container["i8"] = i8
container["i9"] = i9
}
}
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 items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -29,12 +77,12 @@ class InsertRecordClassTests: XCTestCase {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
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)")
}
try! dbQueue.inTransaction { db in
for i in 0..<insertedRowCount {
try ItemClass(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
try Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
}
return .commit
}
@@ -4,18 +4,31 @@ import GRDB
private let insertedRowCount = 20_000
// Here we insert records.
class InsertRecordCodableTests: XCTestCase {
class InsertRecordEncodableTests: XCTestCase {
func testGRDB() {
struct Item: Encodable, 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
}
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 items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -25,12 +38,12 @@ class InsertRecordCodableTests: XCTestCase {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
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)")
}
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)
try Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
}
return .commit
}
@@ -0,0 +1,85 @@
import XCTest
import GRDB
private let insertedRowCount = 20_000
/// A record optimized for batch insert 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
static func optimizedInsertStatement(_ db: Database) throws -> 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..<insertedRowCount {
let item = Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i)
try item.insert(with: statement)
}
return .commit
}
}
}
}
@@ -7,15 +7,41 @@ private let insertedRowCount = 20_000
class InsertRecordStructTests: XCTestCase {
func testGRDB() {
struct Item: 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
func encode(to container: inout PersistenceContainer) {
container["i0"] = i0
container["i1"] = i1
container["i2"] = i2
container["i3"] = i3
container["i4"] = i4
container["i5"] = i5
container["i6"] = i6
container["i7"] = i7
container["i8"] = i8
container["i9"] = i9
}
}
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 items")!, insertedRowCount)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM items")!, 0)
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
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)
}
@@ -25,12 +51,12 @@ class InsertRecordStructTests: XCTestCase {
let dbQueue = try! DatabaseQueue(path: databasePath)
try! dbQueue.inDatabase { db in
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)")
}
try! dbQueue.inTransaction { db in
for i in 0..<insertedRowCount {
try ItemStruct(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
try Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
}
return .commit
}
@@ -1,157 +1,14 @@
import GRDB
#if GRDB_COMPARE
import SQLite
import RealmSwift
#endif
// MARK:- GRDB
struct ItemStruct : 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(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int, i7: Int, i8: Int, i9: Int) {
self.i0 = i0
self.i1 = i1
self.i2 = i2
self.i3 = i3
self.i4 = i4
self.i5 = i5
self.i6 = i6
self.i7 = i7
self.i8 = i8
self.i9 = i9
}
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"]
}
static let databaseTableName = "items"
func encode(to container: inout PersistenceContainer) {
container["i0"] = i0
container["i1"] = i1
container["i2"] = i2
container["i3"] = i3
container["i4"] = i4
container["i5"] = i5
container["i6"] = i6
container["i7"] = i7
container["i8"] = i8
container["i9"] = i9
}
init(dictionary: [AnyHashable: Any]) {
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
}
}
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
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(i0: Int, i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int, i7: Int, i8: Int, i9: Int) {
self.i0 = i0
self.i1 = i1
self.i2 = i2
self.i3 = i3
self.i4 = i4
self.i5 = i5
self.i6 = i6
self.i7 = i7
self.i8 = i8
self.i9 = i9
super.init()
}
override class var databaseTableName: String {
"items"
}
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)
}
override func encode(to container: inout PersistenceContainer) {
container["i0"] = i0
container["i1"] = i1
container["i2"] = i2
container["i3"] = i3
container["i4"] = i4
container["i5"] = i5
container["i6"] = i6
container["i7"] = i7
container["i8"] = i8
container["i9"] = i9
}
}
#if GRDB_COMPARE
// MARK:- SQLite
let itemsTable = Table("items")
let itemTable = Table("item")
let i0Column = Expression<Int>("i0")
let i1Column = Expression<Int>("i1")
let i2Column = Expression<Int>("i2")
@@ -164,30 +21,9 @@ let i8Column = Expression<Int>("i8")
let i9Column = Expression<Int>("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
+17 -4
View File
@@ -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))