Performance tests for string fetching and function results

This commit is contained in:
Chris Kolbu authored and Gwendal Roué committed 2026-09-26 12:34:37 +02:00
1 parent 23083c4668
commit 8957148d4d
3 files changed
+186

No files matched your search

@@ -0,0 +1,118 @@
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 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_record_fetch_performance() throws {
let dbQueue = try makePersonDatabase()
measure {
try! dbQueue.read { db in
for _ in 0..<Self.recordFetchesPerIteration {
let people = try Person.fetchAll(db)
XCTAssertEqual(people.count, Self.personRowCount)
}
}
}
}
private func measureFetch<Value: DatabaseValueConvertible>(
of type: Value.Type,
from column: String) throws
{
let dbQueue = try makeDatabaseQueue()
measure {
try! dbQueue.read { db in
for _ in 0..<Self.fetchesPerIteration {
let values = try Value.fetchAll(db, sql: "SELECT \(column) FROM item")
XCTAssertEqual(values.count, Self.expectedRowCount)
}
}
}
}
private func makePersonDatabase() throws -> 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..<Self.personRowCount {
try statement.execute(arguments: [
"Arthur", "Dubois", "arthur.dubois@example.com",
"12 Rue de la Paix, 75002 Paris", 20 + index % 60, 1.60 + Double(index % 40) / 100,
])
}
}
return dbQueue
}
private func makeDatabaseQueue() throws -> 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..<Self.expectedRowCount {
try statement.execute(arguments: [ArgumentsTests.shortString, ArgumentsTests.longString])
}
}
return dbQueue
}
}
@@ -0,0 +1,60 @@
import XCTest
import GRDB
/// Here we test the return of string values from a custom SQL function.
class FunctionResultTests: XCTestCase {
private let rowCount = 100_000
private let queriesPerIteration = 20
var dbDirectoryPath: String!
var dbQueue: DatabaseQueue!
static let echo = DatabaseFunction("echo", argumentCount: 1, pure: true) { dbValues in
String.fromDatabaseValue(dbValues[0])
}
override func setUpWithError() throws {
let dbDirectoryName = "FunctionResultTests-\(ProcessInfo.processInfo.globallyUniqueString)"
dbDirectoryPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(dbDirectoryName)
try FileManager.default.createDirectory(atPath: dbDirectoryPath, withIntermediateDirectories: true)
let dbPath = (dbDirectoryPath as NSString).appendingPathComponent("db.sqlite")
var configuration = Configuration()
configuration.prepareDatabase { db in
db.add(function: Self.echo)
}
dbQueue = try DatabaseQueue(path: dbPath, configuration: configuration)
}
override func tearDownWithError() throws {
dbQueue = nil
try FileManager.default.removeItem(atPath: dbDirectoryPath)
}
func test_shortString_functionResult_performance() throws {
try measureFunctionResults(over: ArgumentsTests.shortString)
}
func test_longString_functionResult_performance() throws {
try measureFunctionResults(over: ArgumentsTests.longString)
}
private func measureFunctionResults(over string: String) throws {
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE t(a TEXT)")
let statement = try db.makeStatement(sql: "INSERT INTO t(a) VALUES (?)")
for _ in 0..<rowCount {
try statement.execute(arguments: [string])
}
}
measure {
try! dbQueue.read { db in
for _ in 0..<queriesPerIteration {
_ = try Int.fetchOne(db, sql: "SELECT COUNT(echo(a)) FROM t")
}
}
}
}
}
@@ -31,6 +31,8 @@
5690AFDC212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */; };
56A3FEAA27A85F4800B0292E /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 56A3FEA927A85F4800B0292E /* RealmSwift */; };
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */; };
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0002000000000000E002 /* FunctionResultTests.swift */; };
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0004000000000000E004 /* FetchStringTests.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 */; };
@@ -100,6 +102,8 @@
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>"; };
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentsTests.swift; sourceTree = "<group>"; };
AA1F0002000000000000E002 /* FunctionResultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionResultTests.swift; sourceTree = "<group>"; };
AA1F0004000000000000E004 /* FetchStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchStringTests.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>"; };
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementPreparationPerformanceTests.swift; sourceTree = "<group>"; };
@@ -165,6 +169,8 @@
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */,
56DE7B2D1C42B23B00861EB8 /* PerformanceModel.xcdatamodeld */,
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */,
AA1F0002000000000000E002 /* FunctionResultTests.swift */,
AA1F0004000000000000E004 /* FetchStringTests.swift */,
567071FA208A509C006AD95A /* DateParsingTests.swift */,
56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */,
56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */,
@@ -352,6 +358,8 @@
5690AFD82120589A001530EA /* InsertRecordEncodableTests.swift in Sources */,
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */,
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */,
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */,
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */,
5690AFDB212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */,
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */,
56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */,