-
-
Notifications
You must be signed in to change notification settings - Fork 727
/
Copy pathInsertPositionalValuesTests.swift
162 lines (144 loc) · 7.45 KB
/
InsertPositionalValuesTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import XCTest
import GRDB
import SQLite3
#if GRDB_COMPARE
import SQLite
#endif
private let insertedRowCount = 50_000
// Here we insert rows, referencing statement arguments by index.
class InsertPositionalValuesTests: XCTestCase {
func testSQLite() {
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
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)
}
let options = XCTMeasureOptions()
options.iterationCount = 50
measure(options: options) {
_ = try? FileManager.default.removeItem(atPath: databasePath)
var connection: OpaquePointer? = nil
sqlite3_open_v2(databasePath, &connection, 0x00000004 /*SQLITE_OPEN_CREATE*/ | 0x00000002 /*SQLITE_OPEN_READWRITE*/, 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 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)
sqlite3_bind_int64(statement, 1, i)
sqlite3_bind_int64(statement, 2, i)
sqlite3_bind_int64(statement, 3, i)
sqlite3_bind_int64(statement, 4, i)
sqlite3_bind_int64(statement, 5, i)
sqlite3_bind_int64(statement, 6, i)
sqlite3_bind_int64(statement, 7, i)
sqlite3_bind_int64(statement, 8, i)
sqlite3_bind_int64(statement, 9, i)
sqlite3_bind_int64(statement, 10, i)
sqlite3_step(statement)
}
sqlite3_finalize(statement)
sqlite3_exec(connection, "COMMIT", nil, nil, nil)
sqlite3_close(connection)
}
}
func testGRDB() {
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
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)
}
let options = XCTMeasureOptions()
options.iterationCount = 50
measure(options: options) {
_ = 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! db.makeStatement(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()
}
return .commit
}
}
}
#if GRDB_COMPARE
func testFMDB() {
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
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 = FMDatabaseQueue(path: databasePath)!
dbQueue.inDatabase { db in
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 item (i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) VALUES (?,?,?,?,?,?,?,?,?,?)", withArgumentsIn: [i, i, i, i, i, i, i, i, i, i])
}
}
}
}
func testSQLiteSwift() {
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
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 db = try! Connection(databasePath)
try! db.run(itemTable.create { t in
t.column(i0Column)
t.column(i1Column)
t.column(i2Column)
t.column(i3Column)
t.column(i4Column)
t.column(i5Column)
t.column(i6Column)
t.column(i7Column)
t.column(i8Column)
t.column(i9Column)
})
try! db.transaction {
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)
}
}
}
}
#endif
}