每秒處理32萬請求的高性能Web服務器:Lwan
Lwan 是一個高性能和可伸縮的 Web 服務器軟件,支持 glibc/Linux 平臺。
Lwan 開發了將近 3 年時間,目前還是個人研究的成果。主要是為了構建一個堅固、輕量級高性能的 Web 服務器。
-
占用內存低 (1萬個空閑連接只占用 ~500KiB)
-
最小化的內存分配和拷貝
-
最小化的系統調用
-
精確的 HTTP 請求解析
- Static file serving uses the most efficient way according to file size
- No copies between kernel and userland for files larger than 16KiB
- Smaller files are sent using vectored I/O
- Header overhead is considered before considering deflate compression
- Mostly wait-free multi-threaded design
- One thread accepts connections, one I/O thread per logical CPU handles them
- Hand-crafted coroutines makes asynchronous I/O a breeze in C
- Linux only, as it relies on epoll()
- Purpose-built I/O loop
- Efficient loading cache used for
- Directory listing
- File information (size, last modified date, MIME type, etc)
- Compressed files
-
核心只有 7200 行左右C語言代碼
性能:
在一臺使用 i7 處理器的筆記本上可以達到每秒 32 萬無磁盤訪問的 HTTP 請求。當訪問磁盤,測試文件在 16Kb 時可達到每秒 29 萬個請求;而更大的文件可到每秒 18.5 萬請求。以上測試使用 keep-alive 連接,如果不使用 keep-alive 這個數字可能降低 6 倍左右。
下圖是不同并發連接數測試的每秒請求數:
其中綠線是使用如下示例代碼的 Hello world 擴展應用,棕色線是 100 字節的文件。
此外 Lwan 也提供 API 擴展,示例代碼:
#include "lwan.h"static lwan_http_status_thello_world(lwan_request_t *request, lwan_response_t *response, void *data){ static const char message[] = "Hello, World!"; response->mime_type = "text/plain"; strbuf_set_static(response->buffer, message, sizeof(message) - 1); return HTTP_OK;}intmain(void){ const lwan_url_map_t default_map[] = { { .prefix = "/", .callback = hello_world }, { .prefix = NULL } }; lwan_t l; lwan_init(&l); lwan_set_url_map(&l, default_map); lwan_main_loop(&l); lwan_shutdown(&l); return 0;}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!