ASP.NET Core文件壓縮常見使用誤區(qū)(最佳實(shí)踐)
前言
在微軟官方文檔中,未明確指出文件壓縮功能的使用誤區(qū)。
本文將對 ASP.NET Core 文件響應(yīng)壓縮的常見使用誤區(qū)做出說明。
誤區(qū)1:未使用Brotil 壓縮
幾乎不需要任何額外的代價(jià),Brotil 壓縮算法可以幫助你的網(wǎng)站提升約 20% 靜態(tài)資源加載性能。
同時(shí)啟用 Gzip / Brotil 壓縮
Gzip 有更好的 user-agent 兼容性,而 Brotli 有更好的性能。
所以我們通常需要在 ASP.NET Core 網(wǎng)站中同時(shí)啟用這兩種壓縮。
如何區(qū)分 Gzip 壓縮和 Brotli 壓縮
網(wǎng)站啟用 Brotli 壓縮時(shí),服務(wù)器請求返回頭 Content-Encoding 中會包含 br 字樣,否則是 gzip。
誤區(qū)2:使用 Fastest 級別的 Brotli 壓縮
如果你閱讀并參考了微軟官方文檔或者其他中文資源,比如:
ASP.NET Core 中的響應(yīng)壓縮 - MS Doc
在ASP.NET Core中使用brotli壓縮- Cnblogs
那么你可能會在代碼中像下面這樣使用壓縮功能:
寫法1:使用默認(rèn)的壓縮行為(框架將隱式添加 Brotli 和 Gzip 功能)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseResponseCompression();
}
}
寫法2:顯式添加壓縮功能
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.Providers.Add<CustomCompressionProvider>();
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "image/svg+xml" });
});
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
}
寫法3:自定義 BrotliCompressionProvider
public class BrotliCompressionProvider : ICompressionProvider
{
public string EncodingName => "br";
public bool SupportsFlush => true
public Stream CreateStream(Stream outputStream)
{
return new BrotliStream(outputStream,CompressionLevel.Fastest);
}
}
不幸的是,以上三種寫法都沒有發(fā)揮出 Brotil 壓縮算法的優(yōu)勢。
它們的共同點(diǎn)是均使用了 CompressionLevel.Fastest壓縮級別。
而在 CompressionLevel.Fastest 級別時(shí),Brotil 與 Gzip 壓縮性能幾乎無異。
參考:Introducing Support for Brotli Compression

圖 2-1 Fastest 模式下,三種算法的壓縮率等同
誤區(qū)3:使用 Optimal 級別的 Brotli 壓縮
CompressionLevel 只有三個(gè)枚舉值:Fastest / NoCompression / Optimal。
既然 Fastest 級別沒有用,那我們只能換成 Optimal 了。

圖 3-1 壓縮級別枚舉
非常不幸,Brotil 的 Optimal 壓縮級別存在嚴(yán)重的性能問題,在實(shí)際網(wǎng)站應(yīng)用中幾乎沒有適用的場景。

圖 3-2 Optimal 壓縮耗時(shí)對比
最佳實(shí)踐:使用 4 或 5 級別的 Brotli 壓縮
在Introducing Support for Brotli Compression這篇文章中,作者對不同級別 Brotil 的壓縮耗時(shí)做了評測,也就是下面這幅圖。

圖 4-1 不同壓縮級別下 Brotli 的壓縮耗時(shí)
觀察這副圖,Brotil 的壓縮質(zhì)量其實(shí)有 1~11 個(gè)級別。
那我們?nèi)绾巫远x Brotli 的壓縮級別呢,答案是直接將級別對應(yīng)的整數(shù)轉(zhuǎn)成 CompressionLevel 枚舉。
參考:Setting a specific Brotli compression level when using response compression in ASP.NET Core

圖 4-2 指定 Brotli 的壓縮級別
盡管這種寫法看起來十分古怪,但通過考察 .NET 源碼,可以確鑿這種寫法是可行的。
參考:System.IO.Compression.BrotliUtils.cs

圖 4-3 CompressionLevel 枚舉強(qiáng)制轉(zhuǎn)換整數(shù)
現(xiàn)在我們可以自定義壓縮級別了,但注意上方源碼,只能指定 3 以上的級別,3 包括 3 以下的值有其他對應(yīng)的轉(zhuǎn)換邏輯。
回到本節(jié)第一副圖 4-1,通過實(shí)測,發(fā)現(xiàn)在 4 / 5 級別下,Brotli 確實(shí)可以獲得最佳的壓縮率和較低的壓縮損耗。
總結(jié)
在 ASP.NET Core 中,我們應(yīng)該同時(shí)啟用 Gzip 和 Brotil 壓縮功能,其中 Brotil 的壓縮級別應(yīng)該自定義到 4 或 5。
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "image/svg+xml" });
});
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = (CompressionLevel)4; // 4 or 5 is OK
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
}
以上就是ASP.NET Core文件壓縮最佳實(shí)踐的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core文件壓縮的資料請關(guān)注腳本之家其它相關(guān)文章!
- 在asp.net core中使用類似Application的服務(wù)的實(shí)現(xiàn)
- 理解ASP.NET Core 依賴注入(Dependency Injection)
- asp.net core3.1cookie和jwt混合認(rèn)證授權(quán)實(shí)現(xiàn)多種身份驗(yàn)證方案
- 理解ASP.NET Core 啟動類(Startup)
- 理解ASP.NET Core 中間件(Middleware)
- asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證
- ASP.NET Core 集成 React SPA應(yīng)用的步驟
- ASP.NET Core快速入門之實(shí)戰(zhàn)篇
- ASP.NET Core如何注入多個(gè)服務(wù)實(shí)現(xiàn)類
相關(guān)文章
WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate
本文詳細(xì)講解了WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼
asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼2010-02-02
ASP.NET Core3.1 Ocelot認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core3.1 Ocelot認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
常用的在數(shù)據(jù)庫中建立無限級樹形菜單的asp.net代碼
經(jīng)常在項(xiàng)目中遇到建立無限級樹形菜單展示的效果,這里簡單地做了一個(gè),基本后臺代碼如下2008-09-09
ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán)功能
這篇文章主要介紹了ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán),本文將分別介紹?Authentication(認(rèn)證)?和?Authorization(授權(quán)),通過實(shí)例代碼分別介紹了這兩個(gè)功能,需要的朋友可以參考下2022-04-04
asp.net計(jì)算一串?dāng)?shù)字中每個(gè)數(shù)字出現(xiàn)的次數(shù)
計(jì)算一串?dāng)?shù)字中每個(gè)數(shù)字出現(xiàn)的次數(shù),可以這樣子,先判斷輸入的字符串是不是數(shù)字組成,還是否包含有其它字符2012-05-05
運(yùn)行page頁面時(shí)的事件執(zhí)行順序及頁面的回發(fā)與否深度了解
page頁面時(shí)的事件執(zhí)行順序的了解對于一些.net開發(fā)者起到者尤關(guān)重要的作用;頁面的回發(fā)與否會涉及到某些事件執(zhí)行與不執(zhí)行,在本文中會詳細(xì)介紹,感興趣的朋友可以了解下2013-01-01

