99
1010const NARROW_ROWS = 10000
1111const WIDE_ROWS = 300000
12+ const FRACTIONAL_ROWS = 100000
1213const POINT_READS = 1000
1314const CHUNK_SIZE = 1000
1415const NARROW_SCHEMA =
@@ -18,6 +19,35 @@ const WIDE_INSERT =
1819 'INSERT INTO Test VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
1920const WIDE_SCHEMA =
2021 'CREATE TABLE Test (id INT PRIMARY KEY, v1 TEXT, v2 TEXT, v3 TEXT, v4 TEXT, v5 TEXT, v6 INT, v7 INT, v8 INT, v9 INT, v10 INT, v11 REAL, v12 REAL, v13 REAL, v14 REAL) STRICT'
22+ const FRACTIONAL_SCHEMA =
23+ 'CREATE TABLE Bench (id INTEGER PRIMARY KEY, price REAL NOT NULL, ratio REAL NOT NULL, latitude REAL NOT NULL, longitude REAL NOT NULL) STRICT'
24+ const FRACTIONAL_INSERT = 'INSERT INTO Bench VALUES (?, ?, ?, ?, ?)'
25+
26+ type NarrowRow = { id : number ; name : string ; value : number }
27+ type WideRow = {
28+ id : number
29+ v1 : string
30+ v2 : string
31+ v3 : string
32+ v4 : string
33+ v5 : string
34+ v6 : number
35+ v7 : number
36+ v8 : number
37+ v9 : number
38+ v10 : number
39+ v11 : number
40+ v12 : number
41+ v13 : number
42+ v14 : number
43+ }
44+ type FractionalRow = {
45+ id : number
46+ price : number
47+ ratio : number
48+ latitude : number
49+ longitude : number
50+ }
2151
2252const lookupIds = Array . from (
2353 { length : POINT_READS } ,
@@ -38,13 +68,29 @@ export const readCases: BenchmarkCase[] = [
3868 {
3969 id : 'read-narrow' ,
4070 label : '10k narrow rows · full read + access' ,
41- setup : setupNarrowRead ,
71+ setup : ( ) => setupNarrowRead ( false ) ,
4272 } ,
4373 {
4474 id : 'read-wide' ,
4575 label : '300k wide rows · full read + access' ,
4676 measuredRuns : 3 ,
47- setup : setupWideRead ,
77+ setup : ( ) => setupWideRead ( false ) ,
78+ } ,
79+ {
80+ id : 'read-narrow-async' ,
81+ label : '10k narrow rows · async read + access' ,
82+ setup : ( ) => setupNarrowRead ( true ) ,
83+ } ,
84+ {
85+ id : 'read-wide-async' ,
86+ label : '300k wide rows · async read + access' ,
87+ measuredRuns : 3 ,
88+ setup : ( ) => setupWideRead ( true ) ,
89+ } ,
90+ {
91+ id : 'read-fractional' ,
92+ label : '100k fractional REAL rows · full read + access' ,
93+ setup : setupFractionalRead ,
4894 } ,
4995]
5096
@@ -98,25 +144,33 @@ async function setupPointReads(prepared: boolean): Promise<BenchmarkFixture> {
98144 }
99145}
100146
101- async function setupNarrowRead ( ) : Promise < BenchmarkFixture > {
102- const db = createBenchmarkDatabase ( 'benchmark_narrow' , NARROW_SCHEMA )
147+ async function setupNarrowRead (
148+ asynchronous : boolean ,
149+ ) : Promise < BenchmarkFixture > {
150+ const db = createBenchmarkDatabase (
151+ asynchronous ? 'benchmark_narrow_async' : 'benchmark_narrow' ,
152+ NARROW_SCHEMA ,
153+ )
103154 try {
104155 await seedNarrowRows ( db )
105156 let count = 0
106157 let checksum = 0
158+ const access = ( rows : NarrowRow [ ] ) => {
159+ count = rows . length
160+ checksum = 0
161+ for ( const row of rows ) checksum += row . id + row . value + row . name . length
162+ }
107163
108164 return {
109165 database : db ,
110- run : ( ) => {
111- const rows = db . execute < {
112- id : number
113- name : string
114- value : number
115- } > ( 'SELECT * FROM Bench' ) . rows . _array
116- count = rows . length
117- checksum = 0
118- for ( const row of rows ) checksum += row . id + row . value + row . name . length
119- } ,
166+ run : asynchronous
167+ ? async ( ) =>
168+ access (
169+ ( await db . executeAsync < NarrowRow > ( 'SELECT * FROM Bench' ) ) . rows
170+ . _array ,
171+ )
172+ : ( ) =>
173+ access ( db . execute < NarrowRow > ( 'SELECT * FROM Bench' ) . rows . _array ) ,
120174 verify : ( ) => {
121175 assertBenchmark ( count === NARROW_ROWS , 'Wrong narrow read row count' )
122176 assertBenchmark (
@@ -132,8 +186,10 @@ async function setupNarrowRead(): Promise<BenchmarkFixture> {
132186 }
133187}
134188
135- async function setupWideRead ( ) : Promise < BenchmarkFixture > {
136- const db = open ( { name : 'benchmark_wide' } )
189+ async function setupWideRead ( asynchronous : boolean ) : Promise < BenchmarkFixture > {
190+ const db = open ( {
191+ name : asynchronous ? 'benchmark_wide_async' : 'benchmark_wide' ,
192+ } )
137193 try {
138194 resetLargeDbSchema ( db )
139195 let expected = 0
@@ -147,52 +203,91 @@ async function setupWideRead(): Promise<BenchmarkFixture> {
147203 await db . executeBatchAsync ( [ { query : WIDE_INSERT , params } ] )
148204 }
149205
206+ let count = 0
207+ let checksum = 0
208+ const access = ( rows : WideRow [ ] ) => {
209+ count = rows . length
210+ checksum = 0
211+ for ( const row of rows ) {
212+ checksum +=
213+ row . id +
214+ row . v1 . length +
215+ row . v2 . length +
216+ row . v3 . length +
217+ row . v4 . length +
218+ row . v5 . length +
219+ row . v6 +
220+ row . v7 +
221+ row . v8 +
222+ row . v9 +
223+ row . v10 +
224+ row . v11 +
225+ row . v12 +
226+ row . v13 +
227+ row . v14
228+ }
229+ }
230+ return {
231+ database : db ,
232+ run : asynchronous
233+ ? async ( ) =>
234+ access (
235+ ( await db . executeAsync < WideRow > ( 'SELECT * FROM Test' ) ) . rows
236+ . _array ,
237+ )
238+ : ( ) => access ( db . execute < WideRow > ( 'SELECT * FROM Test' ) . rows . _array ) ,
239+ verify : ( ) => {
240+ assertBenchmark ( count === WIDE_ROWS , 'Wrong wide read row count' )
241+ assertBenchmark ( checksum === expected , 'Wrong wide read checksum' )
242+ } ,
243+ dispose : ( ) => disposeBenchmarkDatabase ( db ) ,
244+ }
245+ } catch ( error ) {
246+ disposeBenchmarkDatabase ( db )
247+ throw error
248+ }
249+ }
250+
251+ // The wide table stores whole numbers in its REAL columns. These rows store
252+ // fractional values, which take a slower formatting path in native code.
253+ async function setupFractionalRead ( ) : Promise < BenchmarkFixture > {
254+ const db = createBenchmarkDatabase ( 'benchmark_fractional' , FRACTIONAL_SCHEMA )
255+ try {
256+ let expected = 0
257+ for ( let start = 0 ; start < FRACTIONAL_ROWS ; start += CHUNK_SIZE ) {
258+ const params : SQLiteQueryParams [ ] = [ ]
259+ for (
260+ let i = start ;
261+ i < Math . min ( start + CHUNK_SIZE , FRACTIONAL_ROWS ) ;
262+ i ++
263+ ) {
264+ const [ price , ratio , latitude , longitude ] = fractionalRow ( i )
265+ params . push ( [ i , price , ratio , latitude , longitude ] )
266+ expected += price + ratio + latitude + longitude
267+ }
268+ await db . executeBatchAsync ( [ { query : FRACTIONAL_INSERT , params } ] )
269+ }
270+
150271 let count = 0
151272 let checksum = 0
152273 return {
153274 database : db ,
154275 run : ( ) => {
155- const rows = db . execute < {
156- id : number
157- v1 : string
158- v2 : string
159- v3 : string
160- v4 : string
161- v5 : string
162- v6 : number
163- v7 : number
164- v8 : number
165- v9 : number
166- v10 : number
167- v11 : number
168- v12 : number
169- v13 : number
170- v14 : number
171- } > ( 'SELECT * FROM Test' ) . rows . _array
276+ const rows = db . execute < FractionalRow > (
277+ 'SELECT * FROM Bench ORDER BY id' ,
278+ ) . rows . _array
172279 count = rows . length
173280 checksum = 0
174281 for ( const row of rows ) {
175- checksum +=
176- row . id +
177- row . v1 . length +
178- row . v2 . length +
179- row . v3 . length +
180- row . v4 . length +
181- row . v5 . length +
182- row . v6 +
183- row . v7 +
184- row . v8 +
185- row . v9 +
186- row . v10 +
187- row . v11 +
188- row . v12 +
189- row . v13 +
190- row . v14
282+ checksum += row . price + row . ratio + row . latitude + row . longitude
191283 }
192284 } ,
193285 verify : ( ) => {
194- assertBenchmark ( count === WIDE_ROWS , 'Wrong wide read row count' )
195- assertBenchmark ( checksum === expected , 'Wrong wide read checksum' )
286+ assertBenchmark (
287+ count === FRACTIONAL_ROWS ,
288+ 'Wrong fractional read row count' ,
289+ )
290+ assertBenchmark ( checksum === expected , 'Wrong fractional read checksum' )
196291 } ,
197292 dispose : ( ) => disposeBenchmarkDatabase ( db ) ,
198293 }
@@ -240,3 +335,12 @@ function wideRow(i: number): SQLiteQueryParams {
240335function wideRowChecksum ( i : number ) : number {
241336 return 10 * i + 36 + 5 * `n${ i % 1000 } ` . length
242337}
338+
339+ function fractionalRow ( i : number ) : [ number , number , number , number ] {
340+ return [
341+ ( i % 100000 ) / 100 + 0.005 ,
342+ i / 7 + 0.5 ,
343+ 48.2 + i * 1e-6 ,
344+ 16.37 + i * 1e-6 ,
345+ ]
346+ }
0 commit comments