11/**
22 * 1brc.test.ts - insert lots of data, syntesize, extract, benchmark
33 * https://github.com/gunnarmorling/1brc/tree/main
4+ *
5+ * To run:
6+ * npm test 1brc.test.ts
47 */
58
69import { SQLiteCloudRowset } from '../src'
@@ -12,22 +15,7 @@ const fs = require('fs')
1215const path = require ( 'path' )
1316
1417const BRC_UNIQUE_STATIONS = 41343
15- const BRC_INSERT_CHUNKS = 200_000 // we insert this many rows per request
16-
17- async function createDatabaseAsync ( numberOfRows : number ) : Promise < { connection : SQLiteCloudConnection ; database : string } > {
18- const connection = getChinookTlsConnection ( )
19- const database = getTestingDatabaseName ( `1brc-${ numberOfRows } ` )
20- const createSql = `UNUSE DATABASE; CREATE DATABASE ${ database } ; USE DATABASE ${ database } ;`
21- const createResults = await sendCommandsAsync ( connection , createSql )
22- expect ( createResults ) . toBe ( 'OK' )
23- return { database, connection }
24- }
25-
26- async function destroyDatabaseAsync ( connection : SQLiteCloudConnection , database : string ) {
27- const cleanupResults = await sendCommandsAsync ( connection , `UNUSE DATABASE; REMOVE DATABASE ${ database } ` )
28- expect ( cleanupResults ) . toBe ( 'OK' )
29- connection . close ( )
30- }
18+ const BRC_INSERT_CHUNKS = 300_000 // insert this many rows per request
3119
3220const BRC_TIMEOUT = 12 * 60 * 60 * 1000 // 12 hours
3321jest . setTimeout ( BRC_TIMEOUT ) // Set global timeout
@@ -66,9 +54,24 @@ describe('1 billion row challenge', () => {
6654} )
6755
6856//
69- // generate data on the fly
57+ // utility methods
7058//
7159
60+ async function createDatabaseAsync ( numberOfRows : number ) : Promise < { connection : SQLiteCloudConnection ; database : string } > {
61+ const connection = getChinookTlsConnection ( )
62+ const database = getTestingDatabaseName ( `1brc-${ numberOfRows } ` )
63+ const createSql = `UNUSE DATABASE; CREATE DATABASE ${ database } ; USE DATABASE ${ database } ;`
64+ const createResults = await sendCommandsAsync ( connection , createSql )
65+ expect ( createResults ) . toBe ( 'OK' )
66+ return { database, connection }
67+ }
68+
69+ async function destroyDatabaseAsync ( connection : SQLiteCloudConnection , database : string ) {
70+ const cleanupResults = await sendCommandsAsync ( connection , `UNUSE DATABASE; REMOVE DATABASE ${ database } ` )
71+ expect ( cleanupResults ) . toBe ( 'OK' )
72+ connection . close ( )
73+ }
74+
7275class WeatherStation {
7376 constructor ( public id : string , public meanTemperature : number ) { }
7477
@@ -85,6 +88,7 @@ class WeatherStation {
8588 }
8689}
8790
91+ /** Create csv file with random measurements starting from list of stations and base temperature */
8892async function createMeasurements ( numberOfRows : number = 1000000 ) {
8993 let startedOn = Date . now ( )
9094
@@ -126,6 +130,7 @@ async function createMeasurements(numberOfRows: number = 1000000) {
126130 console . log ( `Wrote 1brc_${ numberOfRows } _rows.csv in ${ Date . now ( ) - startedOn } ms` )
127131}
128132
133+ /** Read csv with measurements, insert in chunks, summarize and write out results to csv */
129134async function testChallenge ( numberOfRows : number , insertChunks = BRC_INSERT_CHUNKS ) {
130135 const startedOn = Date . now ( )
131136
@@ -163,7 +168,7 @@ async function testChallenge(numberOfRows: number, insertChunks = BRC_INSERT_CHU
163168 const insertResult = ( await sendCommandsAsync ( connection , insertSql ) ) as Array < number >
164169 expect ( Array . isArray ( insertResult ) ) . toBeTruthy ( )
165170 expect ( insertResult [ 3 ] as number ) . toBe ( dataChunk . length ) // totalChanges
166- console . debug ( `Inserted ${ dataChunk . length } rows in ${ Date . now ( ) - insertOn } ms` )
171+ console . debug ( `Inserted ${ dataChunk . length } rows ( ${ Math . floor ( insertSql . length / 1024 ) } KB) in ${ Date . now ( ) - insertOn } ms` )
167172 }
168173
169174 // calculate averages, etc
@@ -181,7 +186,7 @@ async function testChallenge(numberOfRows: number, insertChunks = BRC_INSERT_CHU
181186 const selectCsv = selectResult . map ( row => `"${ row . city } ",${ row [ 'MIN(temp)' ] } ,${ ( row [ 'AVG(temp)' ] as number ) . toFixed ( 2 ) } ,${ row [ 'MAX(temp)' ] } ` ) . join ( '\n' )
182187 fs . writeFileSync ( selectCsvPathname , selectCsv )
183188 } catch ( error ) {
184- console . error ( `An error occoured while running 1brc, error : ${ error } ` )
189+ console . error ( `Error : ${ error } ` )
185190 throw error
186191 } finally {
187192 // await destroyDatabaseAsync(connection, database)
0 commit comments