|
| 1 | +/** |
| 2 | + * Benchmark: readSqlQuery / toSql on 10k-row result sets |
| 3 | + */ |
| 4 | +import { DataFrame, readSqlQuery, toSql } from "../../src/index.js"; |
| 5 | +import type { SqlConnection, SqlResult, SqlRow, SqlValue, IfExistsStrategy } from "../../src/index.js"; |
| 6 | + |
| 7 | +const ROWS = 10_000; |
| 8 | +const WARMUP = 3; |
| 9 | +const ITERATIONS = 20; |
| 10 | + |
| 11 | +// ── Shared result set ───────────────────────────────────────────────────────── |
| 12 | +const columns: string[] = ["id", "value", "label"]; |
| 13 | +const rows: SqlRow[] = Array.from({ length: ROWS }, (_, i) => ({ |
| 14 | + id: i, |
| 15 | + value: Math.sin(i * 0.01) * 1000, |
| 16 | + label: `item_${i % 100}`, |
| 17 | +})); |
| 18 | + |
| 19 | +// ── Mock adapter for reads ──────────────────────────────────────────────────── |
| 20 | +class ReadAdapter implements SqlConnection { |
| 21 | + query(_sql: string, _params?: readonly SqlValue[]): SqlResult { |
| 22 | + return { columns, rows }; |
| 23 | + } |
| 24 | + listTables(): readonly string[] { |
| 25 | + return ["mock_table"]; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const readConn = new ReadAdapter(); |
| 30 | + |
| 31 | +// ── Warm-up reads ───────────────────────────────────────────────────────────── |
| 32 | +for (let i = 0; i < WARMUP; i++) { |
| 33 | + readSqlQuery("SELECT * FROM mock_table", readConn); |
| 34 | +} |
| 35 | + |
| 36 | +// ── readSqlQuery benchmark ──────────────────────────────────────────────────── |
| 37 | +const startRead = performance.now(); |
| 38 | +for (let i = 0; i < ITERATIONS; i++) { |
| 39 | + readSqlQuery("SELECT * FROM mock_table", readConn); |
| 40 | +} |
| 41 | +const totalRead = performance.now() - startRead; |
| 42 | + |
| 43 | +// ── Mock adapter for writes ─────────────────────────────────────────────────── |
| 44 | +class WriteAdapter implements SqlConnection { |
| 45 | + query(_sql: string, _params?: readonly SqlValue[]): SqlResult { |
| 46 | + return { columns: [], rows: [] }; |
| 47 | + } |
| 48 | + listTables(): readonly string[] { |
| 49 | + return []; |
| 50 | + } |
| 51 | + insert( |
| 52 | + _tableName: string, |
| 53 | + _rows: readonly SqlRow[], |
| 54 | + _columns: readonly string[], |
| 55 | + _ifExists: IfExistsStrategy, |
| 56 | + ): number { |
| 57 | + return _rows.length; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const writeConn = new WriteAdapter(); |
| 62 | +const df = readSqlQuery("SELECT * FROM mock_table", readConn); |
| 63 | + |
| 64 | +// ── Warm-up writes ──────────────────────────────────────────────────────────── |
| 65 | +for (let i = 0; i < WARMUP; i++) { |
| 66 | + toSql(df, "bench_table", writeConn, { ifExists: "replace" }); |
| 67 | +} |
| 68 | + |
| 69 | +// ── toSql benchmark ─────────────────────────────────────────────────────────── |
| 70 | +const startWrite = performance.now(); |
| 71 | +for (let i = 0; i < ITERATIONS; i++) { |
| 72 | + toSql(df, "bench_table", writeConn, { ifExists: "replace" }); |
| 73 | +} |
| 74 | +const totalWrite = performance.now() - startWrite; |
| 75 | + |
| 76 | +console.log( |
| 77 | + JSON.stringify({ |
| 78 | + function: "sql", |
| 79 | + mean_ms: (totalRead + totalWrite) / (2 * ITERATIONS), |
| 80 | + iterations: ITERATIONS, |
| 81 | + total_ms: totalRead + totalWrite, |
| 82 | + read_mean_ms: totalRead / ITERATIONS, |
| 83 | + write_mean_ms: totalWrite / ITERATIONS, |
| 84 | + }), |
| 85 | +); |
0 commit comments