import XCTest import GRDB /// Here we test the decoding of string columns. The other fetch tests in /// this target use an all-INT model, and never decode a string. class FetchStringTests: XCTestCase { static let shortString = "foo" static let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur felis eget nibh aliquet ullamcorper. Nam sodales, tellus a cursus tincidunt, arcu purus suscipit elit, nec congue erat ipsum a purus." static let expectedRowCount = 200_000 static let personRowCount = 100_000 static let recordFetchesPerIteration = 5 static let fetchesPerIteration = 10 private struct Person: Codable, FetchableRecord, PersistableRecord { static let databaseTableName = "person" var firstName: String var lastName: String var email: String var address: String var age: Int var height: Double } func test_shortString_fetch_performance() throws { try measureFetch(of: String.self, from: "short") } func test_longString_fetch_performance() throws { try measureFetch(of: String.self, from: "long") } func test_shortString_databaseValue_performance() throws { try measureFetch(of: DatabaseValue.self, from: "short") } func test_longString_databaseValue_performance() throws { try measureFetch(of: DatabaseValue.self, from: "long") } func test_shortString_as_argument() throws { let dbQueue = try DatabaseQueue() try dbQueue.write { db in let statement = try db.makeStatement(sql: "SELECT ? WHERE 0") measure { for _ in 0..<100_000 { try! statement.execute(arguments: [FetchStringTests.shortString]) } } } } func test_longString_as_argument() throws { let dbQueue = try DatabaseQueue() try dbQueue.write { db in let statement = try db.makeStatement(sql: "SELECT ? WHERE 0") measure { for _ in 0..<100_000 { try! statement.execute(arguments: [FetchStringTests.longString]) } } } } func test_record_fetch_performance() throws { let dbQueue = try makePersonDatabase() try dbQueue.read { db in measure { for _ in 0..( of type: Value.Type, from column: String) throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in measure { for _ in 0..( of type: Value.Type, from column: String) throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in measure { for _ in 0.. DatabaseQueue { let url = URL(fileURLWithPath: NSTemporaryDirectory()) .appendingPathComponent("GRDBPerformancePeople.sqlite") let dbQueue = try DatabaseQueue(path: url.path) try dbQueue.write { db in if try db.tableExists("person"), try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM person") == Self.personRowCount { return } try db.execute(sql: "DROP TABLE IF EXISTS person") try db.create(table: "person") { table in table.column("firstName", .text) table.column("lastName", .text) table.column("email", .text) table.column("address", .text) table.column("age", .integer) table.column("height", .double) } let statement = try db.makeStatement(sql: """ INSERT INTO person (firstName, lastName, email, address, age, height) VALUES (?, ?, ?, ?, ?, ?) """) for index in 0.. DatabaseQueue { let url = URL(fileURLWithPath: NSTemporaryDirectory()) .appendingPathComponent("GRDBPerformanceStrings.sqlite") let dbQueue = try DatabaseQueue(path: url.path) try dbQueue.write { db in if try db.tableExists("item"), try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item") == Self.expectedRowCount { return } try db.execute(sql: "DROP TABLE IF EXISTS item") try db.execute(sql: "CREATE TABLE item (short TEXT, long TEXT)") let statement = try db.makeStatement(sql: "INSERT INTO item (short, long) VALUES (?, ?)") for _ in 0..