uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.09k stars 578 forks source link

Benchmark performance #861

Closed dorinesinenco closed 1 year ago

dorinesinenco commented 1 year ago

Hi! I decided to try out uWebSockets.js

I'm running a simple app, this is the main code


import HyperExpress from 'hyper-express'
import { MongoClient } from 'mongodb'

const webserver = new HyperExpress.Server()
const mongoClient = new MongoClient("mongodb://mongo:mongo@bench_he_db")
mongoClient.connect()
const db = await mongoClient.db('bench')

// Create GET route to serve 'Hello World'
webserver.get('/', async (request, response) => {
    response.send('Hello World');
})
webserver.get('/bench', async (request, response) => {
    const articles = await db.collection('articles')
    let ids = []
    let q = 50
    while (q--) {
        ids.push(parseInt(Math.random() * 100000))
    }
    const data = await articles.find({ n: { $in: ids } }).toArray()
    // console.log(data)
    return response.send(JSON.stringify(data))
    // return response.send(JSON.stringify({a:1}))
})

webserver.listen(8000)
    .then((socket) => {

        console.log('Webserver started on port 8000')
    })
    .catch((error) => console.log('Failed to start webserver on port 8000', error));

basically retrieving 50 random docs from a mongodb database where we have already 100k documents, when i try to benchmark this with ab it hangs, then i tried wrk (using: wrk -t1 -c500 -d10s http://localhost:8000/bench) and it gives me about 350 reqs/s using 100% (12 cores on ubuntu), while the same exact test, on the same machine, using only 15% of the same CPU - gives me about 5600reqs/s on fastify.js

NOTE1: i tried a simple {hello: "World"} without accessing the db and on 25% CPU load uWebSockets gives me about 200000 reqs/s NOTE2: both apps run in docker containers

Can't get if the issue is on the mongo driver side (although i used the same driver for fastify) or on the framework itself, or maybe i did something wrong )?

IF you have any idea, please let me know. Thank you!

uNetworkingAB commented 1 year ago

import HyperExpress from 'hyper-express' import { MongoClient } from 'mongodb'

None of this is uWS. I don't know why it is reported here. Report issues in respective projects, not here.

What I can add, however, is that ab (ApacheBench) is a tool written in the 1990s for HTTP1.0 which is a deprecated protocol nobody should be using, and not supported by uWS so it's not supposed to work. uWS implements the latest RFC for HTTP1.1.

If you want some more competent usage of uWS, you can look at Bun and its DB connectors - I cannot be responsible for whatever MongoClient or HyperExpress does wrong.