|
| 1 | +import type { ICreateTableRequestDto } from '@teable/v2-contract-http'; |
| 2 | +import { Bench } from 'tinybench'; |
| 3 | + |
| 4 | +import { createBunBenchContext } from './bench-context'; |
| 5 | + |
| 6 | +const benchOptions = { |
| 7 | + iterations: 0, |
| 8 | + warmupIterations: 0, |
| 9 | + time: 5000, |
| 10 | + warmupTime: 1000, |
| 11 | + throws: true, |
| 12 | +}; |
| 13 | + |
| 14 | +const createTableName = (scenario: string): string => { |
| 15 | + const random = Math.random().toString(36).slice(2, 8); |
| 16 | + return `Bench_Bun_${scenario}_${Date.now()}_${random}`; |
| 17 | +}; |
| 18 | + |
| 19 | +const createSimpleFields = (): ICreateTableRequestDto['fields'] => [ |
| 20 | + { type: 'singleLineText', name: 'Name' }, |
| 21 | + { type: 'number', name: 'Amount', options: { defaultValue: 1 } }, |
| 22 | + { type: 'checkbox', name: 'Done', options: { defaultValue: false } }, |
| 23 | +]; |
| 24 | + |
| 25 | +const createAllBaseFields = (): ICreateTableRequestDto['fields'] => [ |
| 26 | + { type: 'singleLineText', name: 'Name' }, |
| 27 | + { type: 'longText', name: 'Description', options: { defaultValue: 'Notes' } }, |
| 28 | + { type: 'number', name: 'Amount', options: { defaultValue: 10 } }, |
| 29 | + { type: 'rating', name: 'Priority', max: 5, options: { icon: 'star', color: 'yellowBright' } }, |
| 30 | + { type: 'singleSelect', name: 'Status', options: ['Todo', 'Done'] }, |
| 31 | + { type: 'multipleSelect', name: 'Tags', options: ['Frontend', 'Backend'] }, |
| 32 | + { type: 'checkbox', name: 'Done', options: { defaultValue: true } }, |
| 33 | + { type: 'attachment', name: 'Files' }, |
| 34 | + { type: 'date', name: 'Due Date' }, |
| 35 | + { type: 'user', name: 'Owner', options: { isMultiple: false } }, |
| 36 | + { type: 'button', name: 'Action', options: { label: 'Run' } }, |
| 37 | +]; |
| 38 | + |
| 39 | +const createTextColumns = (count: number): ICreateTableRequestDto['fields'] => |
| 40 | + Array.from({ length: count }, (_, index) => ({ |
| 41 | + type: 'singleLineText', |
| 42 | + name: `Column ${index + 1}`, |
| 43 | + })); |
| 44 | + |
| 45 | +export const runCreateTableBench = async (): Promise<void> => { |
| 46 | + const context = await createBunBenchContext(); |
| 47 | + |
| 48 | + try { |
| 49 | + const bench = new Bench(benchOptions); |
| 50 | + const simpleFields = createSimpleFields(); |
| 51 | + const baseFields = createAllBaseFields(); |
| 52 | + const fields200 = createTextColumns(200); |
| 53 | + const fields1000 = createTextColumns(1000); |
| 54 | + |
| 55 | + const runCreateTable = async (scenario: string, fields: ICreateTableRequestDto['fields']) => { |
| 56 | + const input = { |
| 57 | + baseId: context.baseId, |
| 58 | + name: createTableName(scenario), |
| 59 | + fields, |
| 60 | + }; |
| 61 | + |
| 62 | + const response = await context.client.tables.create(input); |
| 63 | + if (!response.ok) { |
| 64 | + throw new Error('Create table failed'); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + bench.add('bun: create table: 3 columns', async () => { |
| 69 | + await runCreateTable('simple', simpleFields); |
| 70 | + }); |
| 71 | + |
| 72 | + bench.add('bun: create table: all base fields', async () => { |
| 73 | + await runCreateTable('base', baseFields); |
| 74 | + }); |
| 75 | + |
| 76 | + bench.add('bun: create table: 200 columns', async () => { |
| 77 | + await runCreateTable('200', fields200); |
| 78 | + }); |
| 79 | + |
| 80 | + bench.add('bun: create table: 1000 columns', async () => { |
| 81 | + await runCreateTable('1000', fields1000); |
| 82 | + }); |
| 83 | + |
| 84 | + console.log('[bun-bench] running create table benchmarks'); |
| 85 | + await bench.run(); |
| 86 | + |
| 87 | + console.log('CreateTable benchmarks (bun)'); |
| 88 | + console.table(bench.table()); |
| 89 | + } finally { |
| 90 | + await context.dispose(); |
| 91 | + } |
| 92 | +}; |
0 commit comments