Skip to content

Instantly share code, notes, and snippets.

@ibsusu
Created April 9, 2020 05:45
Show Gist options
  • Save ibsusu/0ffd8a3effcaf701e070b17d3ded9c6a to your computer and use it in GitHub Desktop.
Save ibsusu/0ffd8a3effcaf701e070b17d3ded9c6a to your computer and use it in GitHub Desktop.
Wrk Hello world benchmark of nim websockets and uWebSockets
#include "App.h"
int main() {
/* Overly simple hello world app */
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;
}
# ~/work/modules/wrk$ ./wrk -t12 -c400 -d15s http://127.0.0.1:3000
Running 15s test @ http://127.0.0.1:3000
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.83ms 313.61us 9.66ms 88.51%
Req/Sec 17.92k 1.84k 50.97k 92.29%
3215251 requests in 15.09s, 217.71MB read
Requests/sec: 213036.02
Transfer/sec: 14.42MB
EXAMPLE_FILES := HelloWorld EchoServer BroadcastingEchoServer
THREADED_EXAMPLE_FILES := HelloWorldThreaded EchoServerThreaded
override CXXFLAGS += -lpthread -Wconversion -std=c++17 -Isrc -IuSockets/src
override LDFLAGS += uSockets/*.o -lz
# WITH_OPENSSL=1 enables OpenSSL 1.1+ support
ifeq ($(WITH_OPENSSL),1)
# With problems on macOS, make sure to pass needed LDFLAGS required to find these
override LDFLAGS += -lssl -lcrypto
else
# WITH_WOLFSSL=1 enables WolfSSL 4.2.0 support (mutually exclusive with OpenSSL)
ifeq ($(WITH_WOLFSSL),1)
override LDFLAGS += -L/usr/local/lib -lwolfssl
endif
endif
# WITH_LIBUV=1 builds with libuv as event-loop
ifeq ($(WITH_LIBUV),1)
override LDFLAGS += -luv
endif
# WITH_ASAN builds with sanitizers
ifeq ($(WITH_ASAN),1)
override CXXFLAGS += -fsanitize=address
override LDFLAGS += -lasan
endif
.PHONY: examples
examples:
cd uSockets && make
$(foreach FILE,$(EXAMPLE_FILES),$(CXX) -flto -O3 $(CXXFLAGS) examples/$(FILE).cpp -o $(FILE) $(LDFLAGS);)
$(foreach FILE,$(THREADED_EXAMPLE_FILES),$(CXX) -pthread -flto -O3 $(CXXFLAGS) examples/$(FILE).cpp -o $(FILE) $(LDFLAGS);)
all:
make examples
make -C fuzzing
make -C benchmarks
clean:
rm -rf $(EXAMPLE_FILES) $(THREADED_EXAMPLE_FILES)
rm -rf fuzzing/*.o benchmarks/*.o
import websocket, asynchttpserver, asyncnet, asyncdispatch
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
discard req.respond(Http200, "Hello World")
waitfor server.serve(Port(8080), cb)
# Compiled with: `nim c -d:danger --gc:none nimsocket.nim`
# ~/work/modules/wrk$ ./wrk -t12 -c400 -d30s http://127.0.0.1:8080
Could only run 15 seconds before nearly out of memory
Running 30s test @ http://127.0.0.1:8080
12 threads and 400 connections
^C Thread Stats Avg Stdev Max +/- Stdev
Latency 5.46ms 6.36ms 249.96ms 99.55%
Req/Sec 6.34k 0.87k 20.68k 93.39%
1146352 requests in 15.22s, 54.66MB read
Requests/sec: 75327.55
Transfer/sec: 3.59MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment