Skip to content

Commit ccfc9ff

Browse files
Adding heavier tests
1 parent b4de637 commit ccfc9ff

File tree

6 files changed

+169
-138
lines changed

6 files changed

+169
-138
lines changed

src/drivers/connection-tls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
102102
this.buffer = Buffer.alloc(0)
103103
this.startedOn = new Date()
104104
this.processCallback = callback
105+
this.executingCommands = commands
105106

106107
// compose commands following SCPC protocol
107108
const formattedCommands = formatCommand(commands)
@@ -134,6 +135,7 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
134135
// buffer to accumulate incoming data until an whole command is received and can be parsed
135136
private buffer: Buffer = Buffer.alloc(0)
136137
private startedOn: Date = new Date()
138+
private executingCommands?: string
137139

138140
// callback to be called when a command is finished processing
139141
private processCallback?: ResultsCallback

test/benchmark.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* benchmark.test.ts - test low level communication protocol with tls sockets and raw commands
3+
*/
4+
5+
import { SQLiteCloudConnection } from '../src/drivers/connection'
6+
import { EXTRA_LONG_TIMEOUT, getChinookTlsConnection, getTestingDatabaseName, sendCommandsAsync } from './shared'
7+
8+
const fs = require('fs')
9+
const path = require('path')
10+
11+
async function createDatabaseAsync(): Promise<{ connection: SQLiteCloudConnection; database: string }> {
12+
const connection = getChinookTlsConnection()
13+
const database = getTestingDatabaseName('benchmark')
14+
const createSql = `UNUSE DATABASE; CREATE DATABASE ${database}; USE DATABASE ${database};`
15+
const createResults = await sendCommandsAsync(connection, createSql)
16+
expect(createResults).toBe('OK')
17+
return { database, connection }
18+
}
19+
20+
async function destroyDatabaseAsync(connection: SQLiteCloudConnection, database: string) {
21+
const cleanupResults = await sendCommandsAsync(connection, `UNUSE DATABASE; REMOVE DATABASE ${database}`)
22+
expect(cleanupResults).toBe('OK')
23+
connection.close()
24+
}
25+
26+
async function runScript(connection: SQLiteCloudConnection, database: string, relativePathname: string) {
27+
// convert to absolute path, read contents of .sql script
28+
const absolutePathname = path.resolve(__dirname, 'benchmark/test', relativePathname)
29+
const sql = fs.readFileSync(absolutePathname, 'utf8')
30+
const results = await sendCommandsAsync(connection, sql)
31+
if (results !== 'OK') {
32+
const error = `runScript - database: ${database}, script: ${relativePathname}, returned: ${results?.toString()}`
33+
debugger
34+
throw new Error(error)
35+
}
36+
37+
if (results !== 'OK') {
38+
console.error(`runScript - ${relativePathname} returned: ${results}`)
39+
}
40+
expect(results).toBe('OK')
41+
}
42+
43+
describe('benchmark', () => {
44+
describe('benchmark tests', () => {
45+
it('should run setup', async () => {
46+
const { connection, database } = await createDatabaseAsync()
47+
await destroyDatabaseAsync(connection, database)
48+
})
49+
50+
it(
51+
'should run script 120.sql',
52+
async () => {
53+
const { connection, database } = await createDatabaseAsync()
54+
await runScript(connection, database, '120.sql')
55+
await destroyDatabaseAsync(connection, database)
56+
},
57+
EXTRA_LONG_TIMEOUT
58+
)
59+
60+
it(
61+
'should run scripts with new connection for each script',
62+
async () => {
63+
// setup database used for testing
64+
const { connection, database } = await createDatabaseAsync()
65+
66+
// list contents of test folder
67+
const testFolder = path.resolve(__dirname, './benchmark/test')
68+
const files = fs.readdirSync(testFolder).sort()
69+
70+
for (const testFile of files) {
71+
const scriptConnection = getChinookTlsConnection()
72+
console.debug(`Benchmark tests, run ${testFile}`)
73+
await runScript(scriptConnection, database, testFile)
74+
}
75+
76+
await destroyDatabaseAsync(connection, database)
77+
},
78+
EXTRA_LONG_TIMEOUT
79+
)
80+
81+
it(
82+
'should run benchmark scripts with same connection',
83+
async () => {
84+
// setup database used for testing
85+
const { connection, database } = await createDatabaseAsync()
86+
// list contents of test folder
87+
const testFolder = path.resolve(__dirname, './benchmark/test')
88+
const files = fs.readdirSync(testFolder).sort()
89+
90+
for (const testFile of files) {
91+
console.debug(`Benchmark tests, run ${testFile}`)
92+
await runScript(connection, database, testFile)
93+
}
94+
95+
await destroyDatabaseAsync(connection, database)
96+
},
97+
EXTRA_LONG_TIMEOUT
98+
)
99+
})
100+
})

test/benchmark.testOFF.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

test/benchmark/test/090.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
-- 8 INSERTs into table with no index
22
BEGIN;
3-
CREATE TABLE z99(a INTEGER , b INTEGER , c VARCHAR(200) );
4-
INSERT INTO z99 VALUES(32768,1,'thirty two thousand seven hundred sixty eight');
5-
INSERT INTO z99 VALUES(16384,2,'sixteen thousand three hundred eighty four');
6-
INSERT INTO z99 VALUES(49152,3,'forty nine thousand one hundred fifty two');
7-
INSERT INTO z99 VALUES(8192,4,'eight thousand one hundred ninety two');
8-
INSERT INTO z99 VALUES(40960,5,'forty thousand nine hundred sixty');
9-
INSERT INTO z99 VALUES(24576,6,'twenty four thousand five hundred seventy six');
10-
INSERT INTO z99 VALUES(57344,7,'fifty seven thousand three hundred forty four');
11-
INSERT INTO z99 VALUES(4096,8,'four thousand ninety six');
3+
CREATE TABLE zPollo(a INTEGER , b INTEGER , c VARCHAR(200) );
4+
INSERT INTO zPollo VALUES(32768,1,'thirty two thousand seven hundred sixty eight');
5+
INSERT INTO zPollo VALUES(16384,2,'sixteen thousand three hundred eighty four');
6+
INSERT INTO zPollo VALUES(49152,3,'forty nine thousand one hundred fifty two');
7+
INSERT INTO zPollo VALUES(8192,4,'eight thousand one hundred ninety two');
8+
INSERT INTO zPollo VALUES(40960,5,'forty thousand nine hundred sixty');
9+
INSERT INTO zPollo VALUES(24576,6,'twenty four thousand five hundred seventy six');
10+
INSERT INTO zPollo VALUES(57344,7,'fifty seven thousand three hundred forty four');
11+
INSERT INTO zPollo VALUES(4096,8,'four thousand ninety six');
1212
COMMIT;

test/connection-tls.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* connection-tls.test.ts - test low level communication protocol with tls sockets and raw commands
33
*/
44

5-
import { SQLiteCloudError } from '../src/index'
5+
import { SQLiteCloudError, SQLiteCloudRowset } from '../src/index'
66
import { SQLiteCloudConnection } from '../src/drivers/connection'
77
import { SQLiteCloudTlsConnection } from '../src/drivers/connection-tls'
88
import { anonimizeCommand } from '../src/drivers/utilities'
@@ -13,6 +13,7 @@ import {
1313
getTestingConfig,
1414
getChinookConfig,
1515
getChinookTlsConnection,
16+
sendCommandsAsync,
1617
// clearTestingDatabasesAsync,
1718
WARN_SPEED_MS,
1819
EXPECT_SPEED_MS,
@@ -453,6 +454,36 @@ describe('connection-tls', () => {
453454
})
454455

455456
describe('send select commands', () => {
457+
it(
458+
'should send xxl query',
459+
async () => {
460+
const XXL_QUERY = 1 * 1_000_000
461+
let longSql = ''
462+
463+
const connection = getConnection()
464+
while (longSql.length < XXL_QUERY) {
465+
for (let i = 0; i < 5_000; i++) longSql += `SELECT ${longSql.length} 'HowLargeIsTooMuch'; `
466+
467+
try {
468+
const longResults = await sendCommandsAsync(connection, longSql)
469+
expect(longResults).toBeInstanceOf(SQLiteCloudRowset)
470+
if (longResults instanceof SQLiteCloudRowset) {
471+
expect(longResults.numberOfColumns).toBe(1)
472+
expect(longResults.numberOfRows).toBe(1)
473+
expect(longResults[0]['HowLargeIsTooMuch']).toBeGreaterThanOrEqual(longSql.length - 50)
474+
}
475+
} catch (error) {
476+
console.error(`An error occoured while sending an xxl query of ${longSql.length} bytes, error: ${error}`)
477+
debugger
478+
throw error
479+
}
480+
}
481+
482+
connection.close()
483+
},
484+
EXTRA_LONG_TIMEOUT
485+
)
486+
456487
it('should LIST METADATA', done => {
457488
const chinook = getConnection()
458489
chinook.sendCommands('LIST METADATA;', (error, results) => {

test/shared.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,7 @@ export function getChinookDatabase(callback?: ResultsCallback, extraConfig?: Par
127127
/** SQL used to setup testing database with some data */
128128
export const TESTING_SQL = readFileSync(join(__dirname, 'assets/testing.sql'), 'utf8')
129129

130-
export function getTestingConfig(url = CHINOOK_DATABASE_URL): SQLiteCloudConfig {
131-
const testingConfig = parseConnectionString(url)
132-
133-
if (testingConfig.host === 'localhost' && testingConfig.tlsOptions === undefined) {
134-
testingConfig.tlsOptions = {
135-
ca: SELF_SIGNED_CERTIFICATE
136-
}
137-
}
138-
130+
export function getTestingDatabaseName(prefix: string) {
139131
// create a unique id for this test run based on current time with
140132
// enough precision to avoid duplicate ids and be human readable
141133
function generateRandomId(length: number): string {
@@ -155,13 +147,25 @@ export function getTestingConfig(url = CHINOOK_DATABASE_URL): SQLiteCloudConfig
155147
'-' +
156148
generateRandomId(4)
157149

150+
return `${prefix}-${id}.sqlite`
151+
}
152+
153+
export function getTestingConfig(url = CHINOOK_DATABASE_URL): SQLiteCloudConfig {
154+
const testingConfig = parseConnectionString(url)
155+
156+
if (testingConfig.host === 'localhost' && testingConfig.tlsOptions === undefined) {
157+
testingConfig.tlsOptions = {
158+
ca: SELF_SIGNED_CERTIFICATE
159+
}
160+
}
161+
158162
testingConfig.createDatabase = true
159163

160164
if (!(testingConfig.database === 'chinook.sqlite')) {
161165
throw Error('testingConfig.database is not equal to chinook.sqlite')
162166
}
163167

164-
testingConfig.database = testingConfig.database?.replace('chinook.sqlite', `testing-${id}.db`)
168+
testingConfig.database = testingConfig.database?.replace('chinook.sqlite', getTestingDatabaseName('testing'))
165169
return testingConfig
166170
}
167171

@@ -260,3 +264,15 @@ export async function removeDatabaseAsync(database?: Database) {
260264
database.close()
261265
}
262266
}
267+
268+
export async function sendCommandsAsync<T>(connection: SQLiteCloudConnection, sql: string): Promise<T> {
269+
return new Promise<T>((resolve, reject) => {
270+
connection.sendCommands(sql, (error, results) => {
271+
if (error) {
272+
console.error(`sendCommandsAsync - error running ${sql}, error: ${error}`, sql, error)
273+
reject(error)
274+
}
275+
resolve(results)
276+
})
277+
})
278+
}

0 commit comments

Comments
 (0)