Open ConsoleC137 opened 5 months ago
Yeah, ServeFile
is not particularily optimized. However, note that tokio::fs::read_to_string
buffers the entire file contents in memory, which can be problematic for large files. Additionally, I would expect larger files to show less of a difference in performance.
In any case, optimizing that service would be nice. If I recall correctly, it's currently a wrapper around ServeDir
, so it will even do filetype detection and potentially other things once per request, rather than once on construction.
Bug Report
Version
tower-http v0.5.2
Platform
Linux [..] 6.7.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Sun, 03 Mar 2024 00:30:36 +0000 x86_64 GNU/Linux
Crates
Description
The tests were performed with the
wrk
utility. My friend also ran tests withrewrk
utility, the situation is similar. I expected to see this happen:ServeFile
and read from file will show identical readings. Instead, this happened:ServeFile
proved to be slower. I tried this code:api.rs:
"/1" - file is read into memory in advance "/2" - read data from the file, send it "/3" - ServeFile
Test results: In this test, the file weighs 10kb.
In this test, the file weighs 100kb.
I am attaching an archive with my example so that you can try the tests yourself. There are 2 HTML files in the static/html folder: index.html and index-100.html. The first one weighs 10kb, the second one weighs a little more than 100kb. benchmark.zip