uWebSockets http服务太慢

问题描述

我已经克隆了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。我在单线程上运行它。

解决方法

我使用的是Apache基准测试工具(ab),它运行缓慢,而不是程序。使用了bomberdier并获得了结果

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