【问题标题】:uWebSockets http serves too slowuWebSockets http 服务太慢
【发布时间】:2020-10-23 07:01:54
【问题描述】:

我已经克隆了uWebSockets,写了以下文件。

#include <App.h>
#include <iostream>

int main() {
    uWS::App()
            .get("/*", [](auto *res, auto *req) {
                res->end("Hello world!");
            })
            .listen(3000, [](auto *token) {
                if (token) {
                    std::cout << "Listening on port " << 3000 << std::endl;
                }
            })
            .run();

    std::cout << "Failed to listen on port 3000" << std::endl;
    return 0;
}

用它构建

make -C uWebSockets/uSockets
g++ -flto -O3 -Wconversion -std=c++17 -IuWebSockets/src -IuWebSockets/uSockets/src main.cpp -o main uWebSockets/uSockets/*.o -lz -lssl -lcrypto -luv

当我做一个基准测试时,这就是我得到的。

Concurrency Level:      100
Time taken for tests:   116.928 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      68000 bytes
HTML transferred:       12000 bytes
Requests per second:    8.55 [#/sec] (mean)
Time per request:       11692.844 [ms] (mean)
Time per request:       116.928 [ms] (mean, across all concurrent requests)
Transfer rate:          0.57 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   2.6      2      10
Processing:  8923 11690 921.2  11995   12012
Waiting:        0    1   1.3      0      10
Total:       8923 11693 921.7  11999   12012

Percentage of the requests served within a certain time (ms)
  50%  11999
  66%  12001
  75%  12002
  80%  12003
  90%  12006
  95%  12008
  98%  12010
  99%  12011
 100%  12012 (longest request)

如果我将并发设置为 1,就会发生这种情况

Concurrency Level:      1
Time taken for tests:   61.655 seconds
Complete requests:      5
Failed requests:        0
Total transferred:      408 bytes
HTML transferred:       72 bytes
Requests per second:    0.08 [#/sec] (mean)
Time per request:       12330.911 [ms] (mean)
Time per request:       12330.911 [ms] (mean, across all concurrent requests)
Transfer rate:          0.01 [Kbytes/sec] received

我做错了吗?我错过了什么?不是应该更快吗?

PS。我在单线程上运行它。

【问题讨论】:

  • 您的基准测试客户端似乎保持打开 100 个并发连接。由于只有一个线程来服务这些连接,因此每秒只会服务几个;其余的只是等待。
  • 说“Hello world!”真的要花那么多时间吗?甚至 expressjs 在 node.js 中也可以做得比这更好。与异步有关吗?
  • 请在下结论前先用单个并发连接进行基准测试。
  • 添加了单个并发连接统计信息。更糟。
  • 请注意,SO etiquette 更喜欢您创建一个自我回答,而不是编辑带有“已解决”的问题。

标签: c++ c++17 uwebsockets


【解决方案1】:

我使用的是 apache 基准测试工具 (ab),它很慢,而不是程序。使用轰炸机并得到了这个结果

Bombarding http://127.0.0.1:3000/ with 1000000 request(s) using 5000 connection(s)
 1000000 / 1000000 [===============================================================================================================================================================================================] 100.00% 316582/s 3s
Done!
Statistics        Avg      Stdev        Max
  Reqs/sec    361230.80   54733.74  520938.19
  Latency       14.06ms     8.81ms   358.62ms
  HTTP codes:
    1xx - 0, 2xx - 1000000, 3xx - 0, 4xx - 0, 5xx - 0
    others - 0
  Throughput:    40.48MB/s

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多