oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.16k stars 2.77k forks source link

BUN 182x SLOWER than NodeJS with MSSQL consecutive Pool Requests #13093

Open gjovanov opened 3 months ago

gjovanov commented 3 months ago

What version of Bun is running?

1.1.22

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

package.json

{
  "name": "bun",
  "module": "index.js",
  "type": "module",
  "devDependencies": {
    "@types/bun": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.4.5"
  },
  "dependencies": {
    "cross-env": "^7.0.3",
    "dotenv": "^16.4.5",
    "mssql": "^11.0.0"
  }
}

index.js

import sql from 'mssql'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import dotenv from 'dotenv'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

dotenv.config({ path: join(__dirname, '.env') })

const runtime = process.versions.bun ? 'Bun' : 'Node'
const version = process.versions.bun || process.versions.node
console.log(`Hello via ${runtime} - ${version} with MSSQL on ${process.env.MSSQL_SERVER}`)

const config = {
    user: process.env.MSSQL_USER,
    password: process.env.MSSQL_PASSWORD,
    database: process.env.MSSQL_DB,
    server: process.env.MSSQL_SERVER,
    requestTimeout: 130000,
    idleTimeoutMillis: 130000,
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 300000,
    },
    options: {
        encrypt: false,
        requestTimeout: 130000,
        trustServerCertificate: true,
        trustedConnection: true,
        instanceName: process.env.MSSQL_INSTANCE,
    },
}

const t1 = performance.now()
const dbPool = new sql.ConnectionPool(config)
const t2 = performance.now()
console.log(t2 - t1)

const t3 = performance.now()
let pool = await dbPool.connect()
const t4 = performance.now()
console.log(t4 - t3)

const t5 = performance.now()
let result = await pool.request().query('SELECT top 10 * FROM Project')
const t6 = performance.now()
console.log(t6 - t5)

const t7 = performance.now()
pool = await dbPool.connect()
const t8 = performance.now()
console.log(t8 - t7)

const t9 = performance.now()
result = await pool.request().query('SELECT top 10 * FROM Project')
const t10 = performance.now()
console.log(t10 - t9)
// console.log(result)

Project.sql

CREATE TABLE [dbo].[Project](
    [PR_ID] [bigint] IDENTITY(1,1) NOT NULL,
    [PR_NUMMER] [varchar](15) NOT NULL,
 CONSTRAINT [PKey_PR_ID] PRIMARY KEY NONCLUSTERED 
(
    [PR_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

Data.sql

SET IDENTITY_INSERT Project ON;

with randowvalues
    as(
       select 1000000 PR_ID,  CONCAT('Project-', 1) PR_NUMMER
       --select 1 id, RAND(CHECKSUM(NEWID()))*100 randomnumber
        union  all
        select PR_ID + 1, CONCAT('Project-', (PR_ID + 1)) PR_NUMMER
        from randowvalues
        where 
          PR_ID < 1001200
      )

insert into Project (PR_ID, PR_NUMMER) 
select *
from randowvalues
OPTION(MAXRECURSION 0)

What is the expected behavior?

Expectation is to see better or even similar performance as in Node:

Node

PS C:\dev\bun\packages\bun> node .\index.js
Hello via Node - 20.8.1 with MSSQL on localhost
0.38989996910095215
35.54549998044968
23.2771999835968
0.06549996137619019
3.147499978542328

What do you see instead?

But instead we are getting 182x slower performance than Node for consecutive requests:

Bun

PS C:\dev\bun\packages\bun> bun .\index.js
Hello via Bun - 1.1.22 with MSSQL on localhost
0.5806999999999789 <-- Marginally slower than Node (could be statistical error)
39.66310000000004 <-- Marginally slower than Node (could be statistical error)
31.647400000000005 <-- Marginally slower than Node (could be statistical error)
0.05330000000003565 <-- Marginally faster than Node (could be statistical error)
575.0634 <------- 182x SLOWER than Node!!!

Additional information

No response

Jarred-Sumner commented 3 months ago

very likely an event loop bug

gjovanov commented 2 months ago

any update on this one?