|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { Bench } from 'tinybench'; |
| 3 | +import { DataSource, QueryRunner } from 'typeorm'; |
| 4 | +import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions'; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { getDbConfig } from '../../../e2e-common/test-config'; |
| 8 | + |
| 9 | +const BATCH_SIZE = 10; |
| 10 | +const expectedValues = Array.from({ length: BATCH_SIZE }, (_, index) => index); |
| 11 | + |
| 12 | +describe.skipIf(process.env.DB !== 'postgres')('PostgreSQL query pipelining benchmark', () => { |
| 13 | + let sequentialDataSource: DataSource; |
| 14 | + let pipelinedDataSource: DataSource; |
| 15 | + let sequentialQueryRunner: QueryRunner; |
| 16 | + let pipelinedQueryRunner: QueryRunner; |
| 17 | + |
| 18 | + beforeAll(async () => { |
| 19 | + const connectionOptions = getDbConfig(); |
| 20 | + if (connectionOptions.type !== 'postgres') { |
| 21 | + throw new Error('This benchmark requires DB=postgres'); |
| 22 | + } |
| 23 | + |
| 24 | + sequentialDataSource = await createDataSource(connectionOptions, false); |
| 25 | + pipelinedDataSource = await createDataSource(connectionOptions, true); |
| 26 | + sequentialQueryRunner = sequentialDataSource.createQueryRunner(); |
| 27 | + pipelinedQueryRunner = pipelinedDataSource.createQueryRunner(); |
| 28 | + await Promise.all([sequentialQueryRunner.connect(), pipelinedQueryRunner.connect()]); |
| 29 | + }); |
| 30 | + |
| 31 | + afterAll(async () => { |
| 32 | + await Promise.all([sequentialQueryRunner?.release(), pipelinedQueryRunner?.release()]); |
| 33 | + await Promise.all([sequentialDataSource?.destroy(), pipelinedDataSource?.destroy()]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('compares batches of concurrent queries on one TypeORM QueryRunner', async () => { |
| 37 | + const sequentialResult = await runBatch(sequentialQueryRunner); |
| 38 | + const pipelinedResult = await runBatch(pipelinedQueryRunner); |
| 39 | + expect(getValues(sequentialResult)).toEqual(expectedValues); |
| 40 | + expect(getValues(pipelinedResult)).toEqual(expectedValues); |
| 41 | + |
| 42 | + const bench = new Bench({ |
| 43 | + warmupTime: 500, |
| 44 | + time: 2000, |
| 45 | + }); |
| 46 | + bench |
| 47 | + .add('pipeline off', () => runBatch(sequentialQueryRunner)) |
| 48 | + .add('pipeline on', () => runBatch(pipelinedQueryRunner)); |
| 49 | + |
| 50 | + const tasks = await bench.run(); |
| 51 | + const sequentialQps = getQueriesPerSecond(tasks[0].result?.hz); |
| 52 | + const pipelinedQps = getQueriesPerSecond(tasks[1].result?.hz); |
| 53 | + const speedup = pipelinedQps / sequentialQps; |
| 54 | + |
| 55 | + console.table([ |
| 56 | + { mode: 'pipeline off', queriesPerSecond: Math.round(sequentialQps) }, |
| 57 | + { mode: 'pipeline on', queriesPerSecond: Math.round(pipelinedQps) }, |
| 58 | + { mode: 'ratio', queriesPerSecond: `${speedup.toFixed(2)}x` }, |
| 59 | + ]); |
| 60 | + |
| 61 | + expect(sequentialQps).toBeGreaterThan(0); |
| 62 | + expect(pipelinedQps).toBeGreaterThan(0); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +async function createDataSource( |
| 67 | + connectionOptions: PostgresConnectionOptions, |
| 68 | + pipeline: boolean, |
| 69 | +): Promise<DataSource> { |
| 70 | + const dataSource = new DataSource({ |
| 71 | + ...connectionOptions, |
| 72 | + database: 'postgres', |
| 73 | + entities: [], |
| 74 | + synchronize: false, |
| 75 | + extra: { |
| 76 | + ...connectionOptions.extra, |
| 77 | + pipeline, |
| 78 | + }, |
| 79 | + }); |
| 80 | + await dataSource.initialize(); |
| 81 | + return dataSource; |
| 82 | +} |
| 83 | + |
| 84 | +function runBatch(queryRunner: QueryRunner): Promise<any[]> { |
| 85 | + return Promise.all( |
| 86 | + expectedValues.map(value => queryRunner.query('SELECT $1::integer AS value', [value])), |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function getValues(results: any[]): number[] { |
| 91 | + return results.map(rows => rows[0].value); |
| 92 | +} |
| 93 | + |
| 94 | +function getQueriesPerSecond(iterationsPerSecond: number | undefined): number { |
| 95 | + if (!iterationsPerSecond) { |
| 96 | + throw new Error('Benchmark did not produce a throughput result'); |
| 97 | + } |
| 98 | + return iterationsPerSecond * BATCH_SIZE; |
| 99 | +} |
0 commit comments