-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathFetchPositionalValuesTests.swift
148 lines (124 loc) · 5.36 KB
/
FetchPositionalValuesTests.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
import XCTest
import GRDB
import SQLite3
#if GRDB_COMPARE
import SQLite
#endif
private let expectedRowCount = 200_000
/// Here we test the extraction of row values by column index.
class FetchPositionalValuesTests: XCTestCase {
func testSQLite() throws {
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite")
try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount)
var connection: OpaquePointer? = nil
sqlite3_open_v2(url.path, &connection, 0x00000004 /*SQLITE_OPEN_CREATE*/ | 0x00000002 /*SQLITE_OPEN_READWRITE*/, nil)
let options = XCTMeasureOptions()
options.iterationCount = 50
measure(options: options) {
var count = 0
var statement: OpaquePointer? = nil
sqlite3_prepare_v2(connection, "SELECT * FROM item", -1, &statement, nil)
loop: while true {
switch sqlite3_step(statement) {
case 101 /*SQLITE_DONE*/:
break loop
case 100 /*SQLITE_ROW*/:
_ = sqlite3_column_int64(statement, 0)
_ = sqlite3_column_int64(statement, 1)
_ = sqlite3_column_int64(statement, 2)
_ = sqlite3_column_int64(statement, 3)
_ = sqlite3_column_int64(statement, 4)
_ = sqlite3_column_int64(statement, 5)
_ = sqlite3_column_int64(statement, 6)
_ = sqlite3_column_int64(statement, 7)
_ = sqlite3_column_int64(statement, 8)
_ = sqlite3_column_int64(statement, 9)
break
default:
XCTFail()
}
count += 1
}
sqlite3_finalize(statement)
XCTAssertEqual(count, expectedRowCount)
}
sqlite3_close(connection)
}
func testGRDB() throws {
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite")
try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount)
let dbQueue = try DatabaseQueue(path: url.path)
let options = XCTMeasureOptions()
options.iterationCount = 50
measure(options: options) {
var count = 0
try! dbQueue.inDatabase { db in
let rows = try Row.fetchCursor(db, sql: "SELECT * FROM item")
while let row = try rows.next() {
_ = row[0] as Int
_ = row[1] as Int
_ = row[2] as Int
_ = row[3] as Int
_ = row[4] as Int
_ = row[5] as Int
_ = row[6] as Int
_ = row[7] as Int
_ = row[8] as Int
_ = row[9] as Int
count += 1
}
}
XCTAssertEqual(count, expectedRowCount)
}
}
#if GRDB_COMPARE
func testFMDB() throws {
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite")
try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount)
let dbQueue = FMDatabaseQueue(path: url.path)!
measure {
var count = 0
dbQueue.inDatabase { db in
let rs = try! db.executeQuery("SELECT * FROM item", values: nil)
while rs.next() {
_ = rs.long(forColumnIndex: 0)
_ = rs.long(forColumnIndex: 1)
_ = rs.long(forColumnIndex: 2)
_ = rs.long(forColumnIndex: 3)
_ = rs.long(forColumnIndex: 4)
_ = rs.long(forColumnIndex: 5)
_ = rs.long(forColumnIndex: 6)
_ = rs.long(forColumnIndex: 7)
_ = rs.long(forColumnIndex: 8)
_ = rs.long(forColumnIndex: 9)
count += 1
}
}
XCTAssertEqual(count, expectedRowCount)
}
}
func testSQLiteSwift() throws {
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("GRDBPerformanceTests.sqlite")
try generateSQLiteDatabaseIfMissing(at: url, insertedRowCount: expectedRowCount)
let db = try Connection(url.path)
measure {
var count = 0
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)
_ = Int(row[2] as! Int64)
_ = Int(row[3] as! Int64)
_ = Int(row[4] as! Int64)
_ = Int(row[5] as! Int64)
_ = Int(row[6] as! Int64)
_ = Int(row[7] as! Int64)
_ = Int(row[8] as! Int64)
_ = Int(row[9] as! Int64)
count += 1
}
XCTAssertEqual(count, expectedRowCount)
}
}
#endif
}