Giter Site home page Giter Site logo

orhun / zig-http-benchmarks Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 3.0 642 KB

Benchmarking Zig HTTP client against Rust, Go, Python, C++ and curl

Home Page: https://blog.orhun.dev/zig-bits-04/

License: MIT License

Shell 8.78% Zig 61.84% Go 2.94% Python 0.70% Rust 12.63% C++ 13.12%
benchmarking curl http http-benchmarking http-client http-requests http-server python rust zig

zig-http-benchmarks's Introduction

zig-http-benchmarks

Read the blog post: Zig Bits 0x4

This repository contains a HTTP server/client implementation using Zig's standard library and benchmarks for comparing the client's performance with implementations in other programming languages such as Rust and Go.

Prerequisites

  • Zig (>=0.11), Rust/Cargo, Go, Python
  • Hyperfine (for benchmarking)

Benchmarking

To run the benchmarks:

chmod +x bench.sh
./bench.sh

The result will be saved to benchmarks.md and benchmarks.json.

  rust-hyper ran
    1.01 ± 0.02 times faster than rust-ureq
    1.01 ± 0.02 times faster than rust-reqwest
    1.24 ± 0.06 times faster than go-http-client
    1.46 ± 0.05 times faster than rust-attohttpc
    2.03 ± 0.05 times faster than zig-http-client
    4.26 ± 0.12 times faster than curl
    8.57 ± 0.12 times faster than python-http-client
   19.93 ± 0.25 times faster than cpp-asio-httpclient
Command Mean [ms] Min [ms] Max [ms] Relative
curl 457.9 ± 11.2 442.4 522.2 4.26 ± 0.12
zig-http-client 218.5 ± 4.8 210.3 240.3 2.03 ± 0.05
rust-attohttpc 157.2 ± 5.3 151.8 190.4 1.46 ± 0.05
rust-hyper 107.6 ± 1.3 104.4 114.8 1.00
rust-reqwest 108.7 ± 2.2 105.4 123.7 1.01 ± 0.02
rust-ureq 108.4 ± 2.3 105.7 123.1 1.01 ± 0.02
go-http-client 133.1 ± 6.2 127.6 159.2 1.24 ± 0.06
python-http-client 921.9 ± 5.9 911.4 947.1 8.57 ± 0.12
cpp-asio-httpclient 2144.5 ± 4.5 2133.0 2168.2 19.93 ± 0.25

Plotting

Use the JSON data along with the scripts from the hyperfine examples to plot data using matplotlib. For example:

git clone --depth 1 https://github.com/sharkdp/hyperfine
python hyperfine/scripts/plot_whisker.py benchmarks.json

plot_whisker

Environment

The results are coming from a GitHub runner (ubuntu-latest) and automated with this workflow.

To see the output for the latest run, check out the output branch in this repository.

License

Licensed under The MIT License.

zig-http-benchmarks's People

Contributors

dnaka91 avatar kassane avatar mo8it avatar orhun avatar xwiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zig-http-benchmarks's Issues

Improve the whisker plot

Currently we use this script: https://github.com/sharkdp/hyperfine/blob/master/scripts/plot_whisker.py

To generate a plot:

git clone https://github.com/orhun/zig-http-benchmarks
git checkout output
wget https://raw.githubusercontent.com/sharkdp/hyperfine/master/scripts/plot_whisker.py
python -m pip install -U matplotlib
python plot_whisker.py benchmarks.json

# save it to a file
# python plot_whisker.py benchmarks.json -o benchmarks.png

Results in:

plot_whisker

By changing plot_whisker.py, we should make the boxes bigger and colors more apparent. In the current state of the plot, it is not really possible to read the colors.

Additional context:

be more up front about TLS issue

currently the Zig TLS only support 1.3, which is essentially makes Zig HTTP client unusable in many situations, as TLS 1.2 still sees much wider use, and many public TLS 1.2 servers don't support TLS 1.3. this is mentioned in the blog post, but I think it would be helpful to have it in the Readme as well, for any who might be considering Zig.

https://github.com/Kikobeats/top-sites/blob/master/top-sites.json

FAIL Get "https://4shared.com": tls: server selected unsupported protocol version 303 4shared.com 366
FAIL Get "https://admin.ch": tls: server selected unsupported protocol version 303 admin.ch 35
FAIL Get "https://airbnb.com": tls: server selected unsupported protocol version 303 airbnb.com 94
FAIL Get "https://alexa.com": tls: server selected unsupported protocol version 303
FAIL Get "https://alibaba.com": remote error: tls: protocol version not supported alibaba.com 299
FAIL Get "https://aliexpress.com": remote error: tls: protocol version not supported aliexpress.com 380
FAIL Get "https://amazon.ca": tls: server selected unsupported protocol version 303
FAIL Get "https://amazon.co.jp": tls: server selected unsupported protocol version 303 amazon.co.jp 385
FAIL Get "https://amazon.co.uk": tls: server selected unsupported protocol version 303 amazon.co.uk 370
FAIL Get "https://amazon.com": tls: server selected unsupported protocol version 303 amazon.com 468
FAIL Get "https://amazon.de": tls: server selected unsupported protocol version 303 amazon.de 384
FAIL Get "https://amazon.es": tls: server selected unsupported protocol version 303
FAIL Get "https://amazon.fr": tls: server selected unsupported protocol version 303
FAIL Get "https://amazon.in": tls: server selected unsupported protocol version 303
FAIL Get "https://archive.org": remote error: tls: protocol version not supported archive.org 334
FAIL Get "https://arxiv.org": tls: server selected unsupported protocol version 303
FAIL Get "https://assine.abril.com.br/?redirect=abrilcom": tls: server selected unsupported protocol version 303 abril.com.br 395
FAIL Get "https://biglobe.ne.jp": tls: server selected unsupported protocol version 303 biglobe.ne.jp 313
FAIL Get "https://bing.com": tls: server selected unsupported protocol version 303 bing.com 359
FAIL Get "https://britannica.com": tls: server selected unsupported protocol version 303 britannica.com 236
FAIL Get "https://businessinsider.com": remote error: tls: protocol version not supported businessinsider.com 355
FAIL Get "https://ca.gov": tls: server selected unsupported protocol version 303 ca.gov 192
FAIL Get "https://cambridge.org": remote error: tls: protocol version not supported
FAIL Get "https://canada.ca": remote error: tls: protocol version not supported canada.ca 306
FAIL Get "https://cbslocal.com": remote error: tls: protocol version not supported cbslocal.com 87
FAIL Get "https://cbsnews.com": remote error: tls: protocol version not supported cbsnews.com 369
FAIL Get "https://cdc.gov": remote error: tls: handshake failure cdc.gov 331
FAIL Get "https://change.org": remote error: tls: protocol version not supported change.org 377
FAIL Get "https://channel4.com": tls: server selected unsupported protocol version 303 channel4.com 5
FAIL Get "https://chicagotribune.com": tls: server selected unsupported protocol version 303 chicagotribune.com 32
FAIL Get "https://cointernet.com.co": tls: server selected unsupported protocol version 303 cointernet.com.co 296
FAIL Get "https://cornell.edu": remote error: tls: handshake failure cornell.edu 137
FAIL Get "https://corriere.it": tls: server selected unsupported protocol version 303 corriere.it 97
FAIL Get "https://dailystar.co.uk": tls: server selected unsupported protocol version 303 dailystar.co.uk 1
FAIL Get "https://deloitte.com": remote error: tls: handshake failure deloitte.com 50
FAIL Get "https://detik.com": remote error: tls: protocol version not supported detik.com 178
FAIL Get "https://dictionary.com": remote error: tls: protocol version not supported dictionary.com 4
FAIL Get "https://dropbox.com": remote error: tls: protocol version not supported dropbox.com 447
FAIL Get "https://enable-javascript.com": tls: server selected unsupported protocol version 303 enable-javascript.com 458
FAIL Get "https://esa.int": tls: server selected unsupported protocol version 303 esa.int 6
FAIL Get "https://etsy.com": remote error: tls: protocol version not supported etsy.com 12
FAIL Get "https://excite.co.jp": tls: server selected unsupported protocol version 303 excite.co.jp 90
FAIL Get "https://fastcompany.com": remote error: tls: protocol version not supported fastcompany.com 33
FAIL Get "https://forbes.com": remote error: tls: protocol version not supported forbes.com 457
FAIL Get "https://ft.com": remote error: tls: protocol version not supported ft.com 132
FAIL Get "https://giphy.com": remote error: tls: protocol version not supported giphy.com 61
FAIL Get "https://gizmodo.com": remote error: tls: protocol version not supported gizmodo.com 145
FAIL Get "https://godaddy.com": tls: server selected unsupported protocol version 303 godaddy.com 379
FAIL Get "https://goo.ne.jp": remote error: tls: handshake failure goo.ne.jp 55
FAIL Get "https://goodreads.com": tls: server selected unsupported protocol version 303 goodreads.com 188
FAIL Get "https://harvard.edu": remote error: tls: protocol version not supported harvard.edu 134
FAIL Get "https://hatena.ne.jp": tls: server selected unsupported protocol version 303 hatena.ne.jp 417
FAIL Get "https://hindustantimes.com": remote error: tls: protocol version not supported hindustantimes.com 183
FAIL Get "https://hp.com": remote error: tls: handshake failure hp.com 251
FAIL Get "https://huawei.com": tls: server selected unsupported protocol version 303 huawei.com 172
FAIL Get "https://imageshack.us": tls: server selected unsupported protocol version 303 imageshack.us 305
FAIL Get "https://imdb.com": tls: server selected unsupported protocol version 303 imdb.com 431
FAIL Get "https://inc.com": remote error: tls: protocol version not supported inc.com 16
FAIL Get "https://independent.co.uk": remote error: tls: protocol version not supported independent.co.uk 401
FAIL Get "https://insider.com": remote error: tls: protocol version not supported insider.com 202
FAIL Get "https://instructables.com": remote error: tls: protocol version not supported instructables.com 154
FAIL Get "https://issuu.com": remote error: tls: protocol version not supported issuu.com 351
FAIL Get "https://jhu.edu": tls: server selected unsupported protocol version 303 jhu.edu 93
FAIL Get "https://jimdofree.com": remote error: tls: protocol version not supported jimdofree.com 448
FAIL Get "https://justjared.com": tls: server selected unsupported protocol version 303 justjared.com 72
FAIL Get "https://kakao.com": remote error: tls: protocol version not supported kakao.com 100
FAIL Get "https://latimes.com": tls: server selected unsupported protocol version 303 latimes.com 160
FAIL Get "https://leparisien.fr": tls: server selected unsupported protocol version 303 leparisien.fr 186
FAIL Get "https://lexpress.fr": tls: server selected unsupported protocol version 303 lexpress.fr 19
FAIL Get "https://linkedin.com": tls: server selected unsupported protocol version 303 linkedin.com 497
FAIL Get "https://linktr.ee": remote error: tls: protocol version not supported linktr.ee 106
FAIL Get "https://list-manage.com": tls: server selected unsupported protocol version 303 list-manage.com 338
FAIL Get "https://live.com": tls: server selected unsupported protocol version 303 live.com 442
FAIL Get "https://liveinternet.ru": tls: server selected unsupported protocol version 303 liveinternet.ru 250
FAIL Get "https://loc.gov": remote error: tls: protocol version not supported loc.gov 320
FAIL Get "https://mail.ru": tls: server selected unsupported protocol version 303 mail.ru 419
FAIL Get "https://main.jp": remote error: tls: protocol version not supported main.jp 7
FAIL Get "https://mediafire.com": remote error: tls: protocol version not supported mediafire.com 357
FAIL Get "https://mirror.co.uk": tls: server selected unsupported protocol version 303 mirror.co.uk 350
FAIL Get "https://mit.edu": remote error: tls: protocol version not supported mit.edu 326
FAIL Get "https://msn.com": tls: server selected unsupported protocol version 303 msn.com 465
FAIL Get "https://mysql.com": tls: server selected unsupported protocol version 303
FAIL Get "https://namecheap.com": remote error: tls: protocol version not supported
FAIL Get "https://narod.ru": tls: server selected unsupported protocol version 303 narod.ru 67
FAIL Get "https://nasa.gov": remote error: tls: protocol version not supported nasa.gov 416
FAIL Get "https://nationalgeographic.com": tls: server selected unsupported protocol version 303 nationalgeographic.com 170
FAIL Get "https://nbcnews.com": tls: server selected unsupported protocol version 303 nbcnews.com 255
FAIL Get "https://networkadvertising.org": tls: server selected unsupported protocol version 303 networkadvertising.org 364
FAIL Get "https://newsweek.com": tls: server selected unsupported protocol version 303 newsweek.com 194
FAIL Get "https://newyorker.com": remote error: tls: protocol version not supported
FAIL Get "https://nginx.org": remote error: tls: protocol version not supported nginx.org 256
FAIL Get "https://nicovideo.jp": tls: server selected unsupported protocol version 303 nicovideo.jp 56
FAIL Get "https://nih.gov": remote error: tls: handshake failure nih.gov 424
FAIL Get "https://nikkei.com": tls: server selected unsupported protocol version 303 nikkei.com 156
FAIL Get "https://noaa.gov": tls: server selected unsupported protocol version 303 noaa.gov 83
FAIL Get "https://nydailynews.com": tls: server selected unsupported protocol version 303 nydailynews.com 216
FAIL Get "https://nytimes.com": remote error: tls: protocol version not supported nytimes.com 439
FAIL Get "https://oecd.org": remote error: tls: handshake failure oecd.org 81
FAIL Get "https://office.com": tls: server selected unsupported protocol version 303 office.com 383
FAIL Get "https://offset.com": tls: server selected unsupported protocol version 303 offset.com 291
FAIL Get "https://oracle.com": tls: server selected unsupported protocol version 303 oracle.com 179
FAIL Get "https://oup.com": tls: server selected unsupported protocol version 303 oup.com 274
FAIL Get "https://outlook.com": tls: server selected unsupported protocol version 303 outlook.com 294
FAIL Get "https://pbs.org": remote error: tls: protocol version not supported pbs.org 282
FAIL Get "https://php.net": remote error: tls: protocol version not supported php.net 323
FAIL Get "https://planalto.gov.br": remote error: tls: handshake failure planalto.gov.br 402
FAIL Get "https://playstation.com": tls: server selected unsupported protocol version 303 playstation.com 110
FAIL Get "https://prezi.com": tls: server selected unsupported protocol version 303
FAIL Get "https://prnewswire.com": remote error: tls: protocol version not supported prnewswire.com 77
FAIL Get "https://proof.ovh.net": remote error: tls: protocol version not supported
FAIL Get "https://psu.edu": tls: server selected unsupported protocol version 303 psu.edu 45
FAIL Get "https://psychologytoday.com": tls: server selected unsupported protocol version 303 psychologytoday.com 213
FAIL Get "https://qq.com": remote error: tls: protocol version not supported qq.com 167
FAIL Get "https://quora.com": tls: server selected unsupported protocol version 303
FAIL Get "https://rakuten.co.jp": remote error: tls: handshake failure rakuten.co.jp 210
FAIL Get "https://reuters.com": remote error: tls: handshake failure reuters.com 396
FAIL Get "https://reverbnation.com": tls: server selected unsupported protocol version 303 reverbnation.com 85
FAIL Get "https://sakura.ne.jp": remote error: tls: protocol version not supported sakura.ne.jp 266
FAIL Get "https://samsung.com": remote error: tls: handshake failure samsung.com 400
FAIL Get "https://sapo.pt": tls: server selected unsupported protocol version 303 sapo.pt 120
FAIL Get "https://secureserver.net": tls: server selected unsupported protocol version 303 secureserver.net 249
FAIL Get "https://shutterstock.com": tls: server selected unsupported protocol version 303 shutterstock.com 404
FAIL Get "https://sky.com": tls: server selected unsupported protocol version 303 sky.com 215
FAIL Get "https://stanford.edu": remote error: tls: handshake failure stanford.edu 263
FAIL Get "https://statista.com": tls: server selected unsupported protocol version 303 statista.com 238
FAIL Get "https://target.com": remote error: tls: protocol version not supported target.com 184
FAIL Get "https://ted.com": tls: server selected unsupported protocol version 303 ted.com 103
FAIL Get "https://theatlantic.com": remote error: tls: protocol version not supported theatlantic.com 191
FAIL Get "https://thefreedictionary.com": tls: server selected unsupported protocol version 303 thefreedictionary.com 70
FAIL Get "https://thetimes.co.uk": tls: server selected unsupported protocol version 303 thetimes.co.uk 227
FAIL Get "https://umich.edu": remote error: tls: handshake failure umich.edu 29
FAIL Get "https://un.org": tls: server selected unsupported protocol version 303 un.org 378
FAIL Get "https://unam.mx": remote error: tls: protocol version not supported unam.mx 2
FAIL Get "https://unesco.org": remote error: tls: handshake failure unesco.org 226
FAIL Get "https://unsplash.com": remote error: tls: protocol version not supported unsplash.com 150
FAIL Get "https://uol.com.br": tls: server selected unsupported protocol version 303 uol.com.br 474
FAIL Get "https://us.ovhcloud.com/": tls: server selected unsupported protocol version 303 ovhcloud.com 229
FAIL Get "https://usda.gov": remote error: tls: handshake failure usda.gov 23
FAIL Get "https://vice.com": tls: server selected unsupported protocol version 303 vice.com 139
FAIL Get "https://washington.edu": tls: server selected unsupported protocol version 303 washington.edu 41
FAIL Get "https://webmd.com": remote error: tls: handshake failure webmd.com 354
FAIL Get "https://weibo.com": tls: server selected unsupported protocol version 303
FAIL Get "https://wiley.com": tls: server selected unsupported protocol version 303
FAIL Get "https://wired.com": remote error: tls: protocol version not supported wired.com 325
FAIL Get "https://www.elmundo.es/": remote error: tls: protocol version not supported elmundo.es 122
FAIL Get "https://www.gov.br": remote error: tls: handshake failure www.gov.br 199
FAIL Get "https://www.indiatimes.com/": remote error: tls: protocol version not supported indiatimes.com 398
FAIL Get "https://www.lavanguardia.com/": remote error: tls: protocol version not supported lavanguardia.com 113
FAIL Get "https://www.lemonde.fr/": remote error: tls: protocol version not supported lemonde.fr 314
FAIL Get "https://www.marca.com/": remote error: tls: protocol version not supported marca.com 124
FAIL Get "https://www.naver.com/": remote error: tls: protocol version not supported naver.com 268
FAIL Get "https://www.opera.com/": tls: server selected unsupported protocol version 303 opera.com 441
FAIL Get "https://www.redbull.com/": remote error: tls: protocol version not supported redbull.com 123
FAIL Get "https://www.scientificamerican.com/": remote error: tls: protocol version not supported scientificamerican.com 44
FAIL Get "https://www.skype.com/": tls: server selected unsupported protocol version 303 skype.com 279
FAIL Get "https://www.smh.com.au/": remote error: tls: protocol version not supported smh.com.au 258
FAIL Get "https://www.weebly.com": tls: server selected unsupported protocol version 303 www.weebly.com 428
FAIL Get "https://www.zendesk.com/": remote error: tls: protocol version not supported zendesk.com 151
FAIL Get "https://yadi.sk": remote error: tls: protocol version not supported yadi.sk 111
FAIL Get "https://zdf.de": remote error: tls: protocol version not supported zdf.de 309
FAIL Get "https://zippyshare.com": tls: server selected unsupported protocol version 303 zippyshare.com 117

Curl Performance

Just curious, what version of curl did you use?

And also how about including a version for gzip enabled curl

curl -H "Accept-Encoding: gzip" -X GET curl http://127.0.0.1:8000/get?[1-1000]

Add caching for GitHub Actions

Right now the servers are being built for each workflow run which takes time. It would be nice to e.g. cache target folder for Rust and zig-cache for the Zig server.

Generate whisker plot in CI

It would be nice to generate the whisker plot in the CI pipeline and upload it as a downloadable GitHub asset.

Non-fair comparison of HTTP clients

I think this is a nice attempt at comparing different HTTP clients in different languages, but suffers from unfairness in regard to how those are invoked.

The problem is, that hyperfine repeatedly runs the binary from scratch, which puts Zig in a better light due to additional overhead in other languages. So most of the timings are not really timing the HTTP library, but instead the surrounding things:

  • Python: It invokes the interpreter every time, meaning the time to parse and interpret the script is measured as well, not just the HTTP library call.
  • Rust:
    • In general, most of these crates have extensive features, including certificate loading, internal caching and what not. I don't know how much they do up-front and do on demand, but I'd expect there is quite some overhead in creating the client instances. Overall, they are likely to perform better on subsequent HTTP calls.
    • Especially the async clients have an additional overhead by spinning up the Tokio runtime. You use the current thread version, which is more lightweight, but definitely has some setup overhead as well.
  • Go: Similar to Rust, there is definitely some overhead for the runtime in the background, GC, and internal mechanics of the client.
  • Curl: Probably same as with the Rust version, loading several things on startup that might not be needed for such a lightweight HTTP/1 call without certs.

Improving the setup

To mitigate some of the pitfalls in the other languages and making the comparison more fair, there are two attempts that immediately come to mind:

  • Let each program print out its own timing.
  • Run the HTTP request in a loop.

The first attempt actually came to mind after I implemented the second 😅.

I guess for most programs except curl it's pretty trivial. Get the timestamp before the request is sent, diff with the current timestamp after the request body was printed out, then log the duration.

But this would still not be fair, as it wouldn't take any optimizations for subsequent calls into account. Something like internal buffers that might be re-used and don't need another allocation on the next request.

So I went ahead with the second attempt (as said before actually my first idea and went with that, then realized we might log the timing by the program itself, while I was writing this).

Pretty easy attempt actually, just let each program repeatedly send the request in a loop several times, so we can mitigate some of the overhead from spinning things up. Would not be absolutely zero, but if run often enough, it could water down the overhead to get more comparable results that show the actual HTTP library performance.


My modifications

Overall, I did 3 modifications, 2 of them being Rust-specific.

  1. I added the attohttpc crate into the pool, as it's a somewhat popular alternative to ureq. Have used it in the past several times, and it very much resembles the reqwest API, so I was curious how it performs.
  2. Many Rust programs use too many features, most of them unused. Probably most of them would be optimized away anyway, but it can't hurt to reduce the feature set. At least build times go down a bit.
    2.1. Apply default-features = false to most dependencies, and reduce tokio and hyper features from full to the absolute minimum.
    2.2. Do the async runtime setup manually, which is probably not having any performance impact, but again can reduce the dependency count for this simple setup.
  3. The most important change of adjusting each client to do repeated HTTP requests in a loop.
    3.1. Set all loops to do 100 requests. I'd have loved to set it higher, but the Zig HTTP client was surprisingly slow when run in a loop.
    3.2. For curl it was a bit tricky as a shell loop wouldn't be fair. But I found a trick online.

My test results

So long story short, I ran the tests again after the mentioned adjustments and at least for me all the Rust programs were the fastest, closely followed by Go and cURL.

Most surprising was the slowdown in Zig. I'm not sure what's exactly the issue there. I'm not a Zig dev, and simply searched for how to do simple loops online. Probably I did something wrong there... or maybe it's really this slow and might improve a lot until the full 0.11 release 🤔

I'll open a PR with my modifications shortly, so maybe you can check that I don't do anything wrong, that might slow it down so much.

Command Mean [s] Min [s] Max [s] Relative
zig-http-client 4.065 ± 0.003 4.062 4.069 619.47 ± 942.33
curl 0.014 ± 0.003 0.010 0.021 2.12 ± 3.26
rust-attohttpc 0.007 ± 0.010 0.004 0.082 1.01 ± 2.19
rust-hyper 0.007 ± 0.011 0.004 0.092 1.05 ± 2.28
rust-reqwest 0.007 ± 0.008 0.005 0.081 1.09 ± 2.02
rust-ureq 0.007 ± 0.010 0.004 0.100 1.00
go-http-client 0.010 ± 0.005 0.007 0.094 1.58 ± 2.53
python-http-client 0.092 ± 0.002 0.090 0.099 13.98 ± 21.26

zig-server to go-client: error.ConnectionResetByPeer

@orhun, @truemedian

zig version: 0.12.0-dev.82+2ceeade99
go version: go1.21.0 linux/amd64

see: https://github.com/kassane/zig-http-benchmarks/actions/runs/5856493738

Benchmark 7: go-http-client
error(server): server error: error.ConnectionResetByPeer

ReleaseSafe:

2023-08-18T16:34:53.1301797Z Benchmark 7: go-http-client
2023-08-18T16:34:53.1313621Z info(server): GET HTTP/1.1 /get
2023-08-18T16:37:02.3434100Z error(server): server error: error.ConnectionResetByPeer
2023-08-18T16:37:02.3434685Z 
2023-08-18T16:37:02.3577603Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/os.zig:756:27: 0x241e4f in read (zig-http-server)
2023-08-18T16:37:02.3578401Z             .CONNRESET => return error.ConnectionResetByPeer,
2023-08-18T16:37:02.3578814Z                           ^
2023-08-18T16:37:02.3581412Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/http/Server.zig:493:36: 0x21bcf7 in runServer (zig-http-server)
2023-08-18T16:37:02.3581896Z             try res.connection.fill();
2023-08-18T16:37:02.3582502Z                                    ^
2023-08-18T16:37:02.3584288Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/http/Server.zig:493:36: 0x21bcff in runServer (zig-http-server)
2023-08-18T16:37:02.3584711Z             try res.connection.fill();
2023-08-18T16:37:02.3585033Z                                    ^
2023-08-18T16:37:02.3587487Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/http/Server.zig:493:36: 0x21bd20 in runServer (zig-http-server)
2023-08-18T16:37:02.3587895Z             try res.connection.fill();
2023-08-18T16:37:02.3588180Z                                    ^
2023-08-18T16:37:02.3590504Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/http/Server.zig:493:36: 0x21bd28 in runServer (zig-http-server)
2023-08-18T16:37:02.3590944Z             try res.connection.fill();
2023-08-18T16:37:02.3591253Z                                    ^
2023-08-18T16:37:02.3593606Z /opt/hostedtoolcache/zig/0.11.0/x64/lib/std/http/Server.zig:0:0: 0x21bd30 in runServer (zig-http-server)
2023-08-18T16:37:02.3597042Z /home/runner/work/zig-http-benchmarks/zig-http-benchmarks/zig-http-server/src/main.zig:0:0: 0x21d648 in runServer (zig-http-server)

Not fair to zig, need to use sendfile.

The reason for the performance difference is likely the use of sendfile in the Rust implementation by way of std::io::copy when you do

std::io::copy(&mut body.reader(), &mut stdout)?;

On Linux and Android that calls sys::kernel_copy::copy_spec which has sendfile optimizations. In the zig implementation you do,

const body = request.reader().readAllAlloc(allocator, 8192) catch unreachable;
defer allocator.free(body);
const stdout = std.io.getStdOut().writer();
try stdout.print("{} {s}\n", .{i, body});

When you should probably be doing something like std.os.sendfile or something like that. I don't write Zig but the patch for sendfile optimizations landed there. Zig has had these optimization since 2020

The lack of sendfile means you made an additional double-copy, moving all the data from kernel space to user space and back.

Good luck!


Follow up questions from me

Zig [master] changes in running benchmark

Please, try replace master to 0.11.0 in CI or fix zig code.

 Building cpp-asio-httpclient...
/home/runner/work/zig-http-benchmarks/zig-http-benchmarks/cpp-asio-httpclient/build.zig.zon:1:2: error: missing top-level 'paths' field
Building go-http-client...
Running the server...
Benchmark 1: curl
  Time (mean ± σ):       6.3 ms ±   0.1 ms    [User: 3.0 ms, System: 3.0 ms]
  Range (min … max):     6.1 ms …   6.6 ms    100 runs
 
Error: Failed to run command '/home/runner/work/zig-http-benchmarks/zig-http-benchmarks/zig-http-client/zig-out/bin/zig-http-client': No such file or directory (os error 2)
Benchmark 2: zig-http-client

cc: @orhun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.