Mercurial
comparison benchmark/bun-http-framework-benchmark/__tests__/correction.spec.ts @ 183:a8976a008a9d
[BenchMark] Added bun bench mark to test seoboe vs other popular benchmarks.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Fri, 23 Jan 2026 21:19:08 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 179:8d17f6e6e290 | 183:a8976a008a9d |
|---|---|
| 1 import { describe, it, expect, beforeEach, beforeAll, afterAll } from 'bun:test' | |
| 2 import { readdirSync } from 'fs' | |
| 3 import { resolve } from 'path' | |
| 4 | |
| 5 import { spawn, type Subprocess } from 'bun' | |
| 6 | |
| 7 const startup = (ms = 5000) => new Promise((resolve) => setTimeout(resolve, ms)) | |
| 8 | |
| 9 const blacklists = ['bunrest', 'colston', 'fastify'] | |
| 10 | |
| 11 const frameworks = readdirSync(resolve('src')) | |
| 12 .filter((a) => a.endsWith('.ts') || !a.includes('.')) | |
| 13 .map((a) => (a.includes('.') ? a.replace('.ts', '') : `${a}/index`)) | |
| 14 .filter((a) => !blacklists.includes(a)) | |
| 15 | |
| 16 // ? Not usable atm (bun 0.2.2) | |
| 17 // ! Blocking on: https://github.com/oven-sh/bun/issues/1462 | |
| 18 describe('correctness', async () => { | |
| 19 for (const framework of ['kingworld']) { | |
| 20 const server = Bun.spawn({ | |
| 21 cmd: ['bun', `src/${framework}.ts`], | |
| 22 env: { | |
| 23 ENV: 'production' | |
| 24 } | |
| 25 }) | |
| 26 | |
| 27 await startup(100) | |
| 28 | |
| 29 let ops: Promise<void>[] = [] | |
| 30 | |
| 31 const wrap = ( | |
| 32 executor: ( | |
| 33 resolve: (value: void) => void | Promise<void> | |
| 34 ) => Promise<void> | |
| 35 ): void => { | |
| 36 ops.push(new Promise(executor)) | |
| 37 } | |
| 38 | |
| 39 it('[GET /]: return hi in plain/text', () => { | |
| 40 wrap(async (resolve) => { | |
| 41 const res = await fetch('http://localhost:3000/') | |
| 42 expect(await res.text()).toBe('Hi') | |
| 43 | |
| 44 resolve() | |
| 45 }) | |
| 46 }) | |
| 47 | |
| 48 it('[GET /id/:id]: set header and return params and query', async () => { | |
| 49 const res = await fetch('http://localhost:3000/id/1?name=bun') | |
| 50 | |
| 51 expect(res.headers.get('x-powered-by')).toBe('benchmark') | |
| 52 expect(await res.text()).toBe('1 bun') | |
| 53 }) | |
| 54 | |
| 55 it('[POST /json]: mirror json result', () => | |
| 56 wrap(async () => { | |
| 57 const body = JSON.stringify({ | |
| 58 hello: 'world' | |
| 59 }) | |
| 60 | |
| 61 const res = await fetch('http://localhost:3000/json', { | |
| 62 method: 'POST', | |
| 63 body, | |
| 64 headers: { | |
| 65 'content-type': 'application/json', | |
| 66 'content-length': body.length.toString() | |
| 67 } | |
| 68 }) | |
| 69 | |
| 70 expect(res.headers.get('content-type')).toBe('application/json') | |
| 71 expect(await res.text()).toBe(body) | |
| 72 })) | |
| 73 } | |
| 74 }) |