Performance of async functions of HTTP server #128
-
Hi, I tested your great library and got amazing results with WebSockets. But in case of HTTP + Redis (some cache service) I got unexpected results: OS: Native HTTP + Redisconst http = require('http');
const { promisify } = require('util');
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => {
console.log('Redis error', err);
});
const getAsync = promisify(client.get).bind(client);
const server = http.createServer(async (req, res) => {
const data = await getAsync('a');
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(data);
});
server.listen(3000, () => {
console.log('Listening to port 3000');
}); Result:$ wrk -c 50 -d 30s -t 50 http://127.0.0.1:3000/
Running 30s test @ http://127.0.0.1:3000/
50 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.06ms 556.61us 22.01ms 93.74%
Req/Sec 489.99 64.84 1.17k 91.70%
732858 requests in 30.11s, 102.74MB read
Requests/sec: 24343.35
Transfer/sec: 3.41MB uWebSockets.js HTTP + Redisconst { promisify } = require('util');
const uws = require('uWebSockets.js');
const redis = require('redis');
const app = uws.App();
const client = redis.createClient();
client.on('error', (err) => {
console.log('Redis error', err);
});
const getAsync = promisify(client.get).bind(client);
app.get('/', async (res, req) => {
res.onAborted(() => {
res.aborted = true;
});
const data = await getAsync('a');
if (!res.aborted) {
res.writeHeader('Content-Type', 'application/json');
res.end(data);
}
});
app.listen(3000, () => {
console.log('Listening to port 3000');
}); Result:$ wrk -c 50 -d 30s -t 50 http://127.0.0.1:3000/
Running 30s test @ http://127.0.0.1:3000/
50 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.65ms 358.20us 12.52ms 89.49%
Req/Sec 275.07 15.13 616.00 91.73%
411773 requests in 30.07s, 41.63MB read
Requests/sec: 13693.56
Transfer/sec: 1.38MB Total:
Is it a bug or can you suggest some fix for it? |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 1 reply
-
Hi, This is a good test. It hits an issue not yet fixed https://github.com/uNetworking/uWebSockets/issues/816 I get 16k with Node.js, 11k with uws.js and 34k with the fix. Basically in v16 there will be some kind of res.begin() function you have to call before writing anything (as an opening matching pair to res.end). It's not yet added, so performance is lacking in this case. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer! I very want to use it as HTTP / HTTPS server and I guess your fix will help :) Do you have some ETA of v16? ;) |
Beta Was this translation helpful? Give feedback.
-
Could you try npm install uNetworking/uWebSockets.js#binaries and replace:
with
|
Beta Was this translation helpful? Give feedback.
-
I get with and without experimental_cork:
|
Beta Was this translation helpful? Give feedback.
-
This method fixed performance! Thank you for so quick experimental changes ;) Native HTTP + Redis
uWebSockets.js HTTP + Redis
Total:
Thank you one more time! You are a performance monster! :) |
Beta Was this translation helpful? Give feedback.
-
Even if you're getting worse performance right now, you probably want to first make it work and then just update what you have with the corking fix when it's done? |
Beta Was this translation helpful? Give feedback.
-
Yes, it will be easy to change after the release of this fix :) |
Beta Was this translation helpful? Give feedback.
-
Now you just need a faster Redis client 😉 |
Beta Was this translation helpful? Give feedback.
-
Hello, Anyone knows if it's still relevant to use |
Beta Was this translation helpful? Give feedback.
-
Yes you still need it, maybe it's time to rename it to "cork" soon? |
Beta Was this translation helpful? Give feedback.
-
Does this work with |
Beta Was this translation helpful? Give feedback.
-
Yes you call those write functions while inside experimental_cork |
Beta Was this translation helpful? Give feedback.
-
Yep I think its time to rename it to cork and add it to the typings too, good job |
Beta Was this translation helpful? Give feedback.
-
There is now docs for res.cork: https://unetworking.github.io/uWebSockets.js/generated/interfaces/httpresponse.html#cork |
Beta Was this translation helpful? Give feedback.
There is now docs for res.cork:
https://unetworking.github.io/uWebSockets.js/generated/interfaces/httpresponse.html#cork