[Node.js] Chrome Zstd Go Rust nginx 初体验
Facebook Zstd
Zstd
注意1.51版本性能更好.
lz4也是不错的可惜Chrome不支持.
zstd > brolti > gzip
cpu占用率解压缩压缩都好
Chrome
Caniuse里看到了zstd
chrome >= 118 打开可用,默认不可用
地址栏输入
chrome://flags
enable-zstd-content-encoding //enabled
nginx的支持
zstd-nginx-module(第三方)
# centos readhot fedora rocky linux
yum install libzstd-devel
# debian ubuntu
apt install zstd
# alpine
apk add --no-cache zstd-dev
istio k8s的支持
isto filter支持
Golang Server Zstd
查看评测数据 github.com/klauspost/compress/zstd 数据好一些,
比必须ssl才能用的br好,好于gzip.
package main
import (
"fmt"
"github.com/CAFxX/httpcompression"
"github.com/klauspost/compress/gzip"
"io"
"net/http"
"os"
)
func main() {
data, err := os.ReadFile("./data.json")
if err != nil {
fmt.Println(err)
return
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
str := string(data)
io.WriteString(w, str)
})
zstdComp, err := zstd.New()
compression := httpcompression.ZstandardCompressor(zstdComp)
compress, _ := httpcompression.DefaultAdapter(compression)
http.Handle("/", compress(handler))
http.ListenAndServe("0.0.0.0:8080", nil)
}
Rust Salvo Zstd
Cargo.toml
[package]
name = "zstd_salvo"
version = "0.1.0"
edition = "2021"
[dependencies]
salvo = { version="0.63.1", features = ["compression", "serve-static"] }
tokio = { version = "1", features = ["macros"] }
tracing = "0.1.27"
tracing-subscriber = "0.3"
main.rs
use salvo::compression::Compression;
use salvo::prelude::*;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let base_dir = std::env::current_exe()
.unwrap()
.join("../../../static")
.canonicalize()
.unwrap();
println!("Base Dir: {:?}", base_dir);
let router = Router::new()
.push(
Router::with_hoop(Compression::new().enable_zstd(CompressionLevel::Minsize))
.path("")
.get(StaticFile::new(base_dir.join("data.json"))),
);
//没起效果还是 gzip encoding 在response headers里,是个bug需要fork
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
Java
等待Spring Boot 实现
NodeJS
等待Nest.js @fastify/compress 实现
效果
打开Chrome控制台,随意请求
rust
go
查看请求 Request Headers是否为
Accept-Encoding: gzip, deflate, br, zstd
Response Headers
Content-Encoding: zstd
5.7Mb的文件最终为485Kb