前言
性能是我們日常生活中經常接觸到的一個詞語,更好的性能意味着能給我們帶來更好的用戶體檢。比如我們在購買手機、顯卡、CPU等的時候,可能會更加的關注於這樣指標,所以本篇就來做一個性能評測。
性能也一直是我們開發人員一直追求的一個目標,我們在做語言選擇,平臺選擇,架構選擇的過程中都需要在性能之間做衡量。
同樣性能對 .NET Core 團隊來說也是至關重要的,一項新技術的誕生,除了對生產力的提高,還有技術團隊對性能的追求。
今天,我們就來做一個對比測試,來看看微軟的這樣新技術性能到底怎麼樣,俗話說的好:“是騾子是馬,拉出來溜溜”。
下面讓我開始吧。
測試目標
在測試之前,我們必須要明確我們本次測試想達到的一個目標。本次測試主要是測試應用程序的一個吞吐量。其中QPS,併發數,響應時間是我們衡量吞吐量的幾個重要指標。
以下是本次對比測試的任務目標:
編號 | 對比方 | 系統環境 | 宿主環境 | 測試目標 |
---|---|---|---|---|
1 | ASP.NET Core vs ASP.NET Core |
Windows | Kestrel vs IIS |
相同平臺不同宿主間性能差距 |
2 | ASP.NET Core vs ASP.NET |
Windows | IIS vs IIS |
相同平臺相同宿主不同框架間性能差距 |
3 | ASP.NET Core vs ASP.NET |
Windows | Kestrel vs IIS |
相同平臺不同宿主不同框架間性能差距 |
4 | ASP.NET Core vs Python Django |
Linux | Kestrel vs uwsgi |
相同平臺不同語言不同宿主不同框架間性能差距 |
5 | ASP.NET Core vs Java Servlet |
Linux | Kestrel vs Tomcat |
相同平臺不同語言不同宿主不同框架間性能差距 |
6 | ASP.NET Core vs NodeJS |
Linux | Kestrel vs self host |
相同平臺不同語言不同宿主不同框架間性能差距 |
測試工具
工欲善其事,必先利其器。
首先我們需要一個壓力測試工具,本次我們使用 wrk,有關於wrk的介紹和使用,請查看我的 這篇博客。
然後我們需要一個性能監控工具,因爲wrk已經會給我們輸出吞吐量相關指標,所以我們只需要一個監控CPU,內存等的工具即可。本次我們使用 Windows 自帶的性能監視器。
Windows 性能監視器的打開方式:
開始-->運行-->perfmon
PS: 在下面的監視器圖中如果你發現cpu並沒有100%,那是因爲使用的虛擬機佔用了一部分cpu,所以計算方式應該是虛擬機的cpu使用量+物理機cpu使用量。
環境準備
既然做測試,首先肯定是具有相同的運行環境,以下是本次測試使用到的軟件和硬件環境。
軟硬件環境
名稱 | 操作系統 | 職責 | CPU | 核心數 | 內存 |
---|---|---|---|---|---|
物理機器1 | Windows 10 RS1 | Web Server && 負載生成 | Intel Core i5-4590 | 4 | 16G |
虛擬機器2 | Ubuntu Server 16.04 | Web Server | Intel Core i5-4590 | 2 | 1G |
其中 虛擬機器2
爲 “物理機器1” 使用 win 10 的 Hyper-v 技術搭建的一個虛擬機,所以有幾個指標對於本次測試至關重要。
虛擬機設置爲了2個虛擬核心,以便於在壓力測試的過程中利用到多核特性。其中的虛擬機保留百分比,需要設置爲100%,來分配兩個物理cpu所有資源給它。佔綜系統資源百分比設置爲50,也就是說虛擬機最多利用本地50%的CPU資源,虛擬機限制設置爲100。
源代碼
AspNet 在 GitHub 有一個開源的性能測試項目叫benchmarks,之前新聞中23倍的性能也是出自於本測試項目, 爲了客觀,本次測試並不使用該項目,所有項目均我們自己新建,並且使用當前流行的框架,爲了排除代碼因素的干擾,我們使用最簡單的 Hello World!。
如果你覺得本代碼不夠客觀公正,歡迎在GitHub上Fork本項目,修改後給我提交PR,我會重新進行測試,並且更新本博客。
GitHub: https://github.com/yuleyule66/AspNetCoreBenchmarksCompare
開始測試
wkr命令參數:
wrk -t 2 -c 50 -d 20 --latency http://xxx
因爲已經分配了2個核心給虛擬機使用,所以開的是雙線程。使用這個參數是我經過多次測試,得到的一個最佳的模擬效果。
1 - ASP.NET Core vs ASP.NET Core(Kestrel vs IIS)
ASP.NET Core
環境:物理機器1
OS:Windows 10 RS 1
Host:Kestrel
- wrk -t 2 -c 50 -d 20 --latency http://localhost:5000Running 20s test @ http://localhost:5000
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 5.49ms 21.72ms 358.18ms 98.99%
- Req/Sec 23.28k 1.98k 27.48k 92.13%
- Latency Distribution 50% 0.00us 75% 6.87ms 90% 12.76ms 99% 28.58ms 913567 requests in 20.02s, 115.00MB read
- Requests/sec: 45636.43Transfer/sec: 5.74MB
ASP.NET Core
環境:物理機器1
OS:Windows 10 RS 1
Host:IIS 10.0
- wrk -t 2 -c 50 -d 20 --latency http://localhost:5001Running 20s test @ http://localhost:5001
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 5.30ms 5.81ms 22.24ms 76.75%
- Req/Sec 7.61k 455.21 8.12k 90.00%
- Latency Distribution 50% 3.14ms 75% 9.02ms 90% 15.62ms 99% 17.17ms 302880 requests in 20.02s, 44.77MB read
- Requests/sec: 15130.97Transfer/sec: 2.24MB
總結:
QPS(Kestrel):45636.43
QPS(IIS):15130.97
這個結果難免令人詫異,程序部署在IIS上和使用Kestrel竟然差別如此之大,我們知道實際上即便部署在IIS上,實際上內部還是調用的Kestrel,但是測試結果告訴了我們答案。可能是由於IIS進一步的http封裝導致的吧,畢竟IIS提供了那麼多的其他功能。
以下是Windows的性能監視器,兩個的曲線圖差不多我就放一個了:
紅色:CPU使用率
藍色:內存使用率
2 - ASP.NET Core vs ASP.NET(IIS vs IIS)
ASP.NET Core
環境:物理機器1
OS:Windows 10 RS
Host:IIS
- wrk -t 2 -c 50 -d 20 --latency http://localhost:5001Running 20s test @ http://localhost:5001
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 5.30ms 5.81ms 22.24ms 76.75%
- Req/Sec 7.61k 455.21 8.12k 90.00%
- Latency Distribution 50% 3.14ms 75% 9.02ms 90% 15.62ms 99% 17.17ms 302880 requests in 20.02s, 44.77MB read
- Requests/sec: 15130.97Transfer/sec: 2.24MB
ASP.NET
環境:物理機器1
OS:Windows 10 RS
Host:IIS
.NET Framework 4.6 + MVC5
- wrk -t 2 -c 50 -d 20 --latency http://localhost:10280Running 20s test @ http://localhost:10280
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.94ms 5.58ms 22.82ms 80.90%
- Req/Sec 9.10k 444.04 9.42k 95.00%
- Latency Distribution 50% 3.00ms 75% 10.10ms 90% 13.57ms 99% 16.45ms 362177 requests in 20.00s, 89.80MB read
- Requests/sec: 18104.50Transfer/sec: 4.49MB
總結:
QPS(ASP.NET Core + IIS):15130.97
QPS(ASP.NET + IIS):18104.50
看到這個結果的時候,其實我還是有一點小驚訝的,不僅僅是因爲ASP.NET跑出了1.8K QPS這樣的成績,而是通過Stdev
可以看出,ASP.NET 在應對高請求高併發的時候,還是相當的穩定的。這個結果說明了,在同樣Windows+IIS環境中,ASP.NET是具有優勢和競爭力的,可以預見 ASP.NET 應該還不會淘汰的太快。
Windows性能圖我就不上了,基本上和上面一樣 CPU 100% 的使用率。
3 - ASP.NET Core vs ASP.NET(Kestrel vs IIS)
ASP.NET Core
環境:物理機器1
OS:Windows 10 RS 1
Host:Kestrel
- wrk -t 2 -c 50 -d 20 --latency http://localhost:5000Running 20s test @ http://localhost:5000
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 5.49ms 21.72ms 358.18ms 98.99%
- Req/Sec 23.28k 1.98k 27.48k 92.13%
- Latency Distribution 50% 0.00us 75% 6.87ms 90% 12.76ms 99% 28.58ms 913567 requests in 20.02s, 115.00MB read
- Requests/sec: 45636.43Transfer/sec: 5.74MB
ASP.NET
環境:物理機器1
OS:Windows 10 RS
Host:IIS
.NET Framework 4.6 + MVC5
- wrk -t 2 -c 50 -d 20 --latency http://localhost:10280Running 20s test @ http://localhost:10280
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.94ms 5.58ms 22.82ms 80.90%
- Req/Sec 9.10k 444.04 9.42k 95.00%
- Latency Distribution 50% 3.00ms 75% 10.10ms 90% 13.57ms 99% 16.45ms 362177 requests in 20.00s, 89.80MB read
- Requests/sec: 18104.50Transfer/sec: 4.49MB
總結
QPS(ASP.NET Core + Kestrel):45636.43
QPS(ASP.NET + IIS):18104.50
這個結果應該是在預料之中的,大概是3倍的性能差距吧。但是我覺得和之前微軟宣傳的23倍的性能,是有很大差距的。
4 - ASP.NET Core vs Python Django
注意,以下我們開始使用到虛擬機器2了,我們要在Windows性能監控器裏面查看CPU使用率,還需要再添加2個計數器。
物理處理器 \Hyper-V Hypervisor Logical Processor(*) \ %Total Run Time
虛擬處理器 \Hyper-V Hypervisor Virtual Processor(*) \ %Guest Run Time
ASP.NET Core
環境:虛擬機器2
OS:Linux
Host:Kestrel
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:5000/Running 20s test @ http://192.168.2.48:5000/
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.39ms 5.33ms 33.05ms 77.20%
- Req/Sec 13.43k 1.32k 17.95k 74.75%
- Latency Distribution 50% 2.00ms 75% 8.15ms 90% 13.75ms 99% 15.80ms 534787 requests in 20.01s, 67.32MB read
- Requests/sec: 26730.83Transfer/sec: 3.37MB
Python Django
環境:虛擬機器2
OS:Linux
Host:uwsgi
Python 2.7.12 + Django 1.10.2
服務端宿主運行命令:
sudo uwsgi --http :8000 --file HelloWorldWebApp/wsgi.py --processes=2 --threads==2 --daemonize=/var/log/django.log
結果:
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:8000Running 20s test @ http://192.168.2.48:8000
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 23.40ms 12.23ms 78.13ms 74.81%
- Req/Sec 792.64 143.13 1.25k 67.10%
- Latency Distribution 50% 21.16ms 75% 31.25ms 90% 38.26ms 99% 53.75ms 31591 requests in 20.09s, 3.01MB read
- Socket errors: connect 0, read 31591, write 0, timeout 0Requests/sec: 1572.64Transfer/sec: 153.67KB
總結
QPS(ASP.NET Core + Kestrel):26730.83
QPS(Python Django + Kestrel ):1572.64
不知道是我運行的方式不對還是怎麼,這個差距還是蠻大的,大概是17倍的差距。看來Python Web 在做針對於做大請求併發情況下,還是弱了一點。
5 - ASP.NET Core vs Java Servlet
C# 和 JAVA 一直是兩大陣營的開發人員喜歡討論的話題,爲了避免有陣營偏見,JAVA的源代碼是我委託我們一個JAVA同事編寫的,並且委託由他部署的,並且已經交代了他避免使用jsp,由Servlet直接輸出。
ASP.NET Core
環境:虛擬機器2
OS:Linux
Host:Kestrel
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:5000/Running 20s test @ http://192.168.2.48:5000/
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.39ms 5.33ms 33.05ms 77.20%
- Req/Sec 13.43k 1.32k 17.95k 74.75%
- Latency Distribution 50% 2.00ms 75% 8.15ms 90% 13.75ms 99% 15.80ms 534787 requests in 20.01s, 67.32MB read
- Requests/sec: 26730.83Transfer/sec: 3.37MB
Java Servlet
環境:虛擬機器2
OS:Linux
Host:Tomcat 7.0 + jdk 1.7
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:8080/j2eeWebApp/helloRunning 20s test @ http://192.168.2.48:8080/j2eeWebApp/hello
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.93ms 6.17ms 68.17ms 81.53%
- Req/Sec 9.22k 1.01k 14.06k 70.50%
- Latency Distribution 50% 1.75ms 75% 9.91ms 90% 14.39ms 99% 22.10ms 367733 requests in 20.05s, 93.70MB read
- Requests/sec: 18338.73Transfer/sec: 4.67MB
總結
QPS(ASP.NET Core + Kestrel):26730.83
QPS(Java Servlet + Tomcat):18338.73
通過這個結果我們可以看出,在性能上 ASP.NET Core 已經超越了Java。不說太多了,怕被噴...
6 - ASP.NET Core vs NodeJS
ASP.NET Core
環境:虛擬機器2
OS:Linux
Host:Kestrel
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:5000/Running 20s test @ http://192.168.2.48:5000/
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.39ms 5.33ms 33.05ms 77.20%
- Req/Sec 13.43k 1.32k 17.95k 74.75%
- Latency Distribution 50% 2.00ms 75% 8.15ms 90% 13.75ms 99% 15.80ms 534787 requests in 20.01s, 67.32MB read
- Requests/sec: 26730.83Transfer/sec: 3.37MB
NodeJS
環境:虛擬機器2
OS:Linux
Host:self host
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:1337Running 20s test @ http://192.168.2.48:1337
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 4.40ms 5.23ms 31.25ms 79.47%
- Req/Sec 10.32k 0.88k 11.37k 90.25%
- Latency Distribution 50% 2.08ms 75% 8.32ms 90% 13.19ms 99% 15.93ms 410902 requests in 20.02s, 61.13MB read
- Requests/sec: 20522.89Transfer/sec: 3.05MB
總結
QPS(ASP.NET Core + Kestrel):26730.83
QPS(NodeJS):20522.89
這個結果着實讓我吃了一驚,NodeJS性能竟然如此驚人,比JAVA要快10%。作爲一個解釋性語言這個性能可以說達到了極致,雖然在測試之前知道NodeJS採用的是異步IO,但還是被測試結果震驚了。
不知道是不是因爲NodeJS沒有經過什麼Web框架,直接輸出的結果。所以我需要再加測一個ASP.NET Core 通過中間件直接輸入結果的性能,這次我要使用微軟的測試項目benchmarks。
- wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:5000/plaintextRunning 20s test @ http://192.168.2.48:5000/plaintext
- 2 threads and 50 connections
- Thread Stats Avg Stdev Max +/- Stdev
- Latency 3.69ms 5.03ms 18.30ms 80.38%
- Req/Sec 25.06k 4.14k 29.19k 83.33%
- Latency Distribution 50% 806.00us 75% 6.82ms 90% 12.62ms 99% 15.63ms 1002476 requests in 20.10s, 126.20MB read
- Requests/sec: 49874.57Transfer/sec: 6.28MB
My God !!!
總結
以下是測試結果的彙總統計:
編號 | 對比方 | 系統環境 | 宿主環境 | 測試結果(QPS) |
---|---|---|---|---|
1 | ASP.NET Core vs ASP.NET Core |
Windows | Kestrel vs IIS |
45.6k vs 15.2k |
2 | ASP.NET Core vs ASP.NET |
Windows | IIS vs IIS |
15.2k vs 18.2k |
3 | ASP.NET Core vs ASP.NET |
Windows | Kestrel vs IIS |
45.6k vs 18.2k |
4 | ASP.NET Core vs Python Django |
Linux | Kestrel vs uwsgi |
26.7k vs 1.57k |
5 | ASP.NET Core vs Java Servlet |
Linux | Kestrel vs Tomcat |
26.7k vs 18.3k |
6 | ASP.NET Core vs NodeJS |
Linux | Kestrel vs self host |
26.7k vs 20.5k |
作爲微軟的下一代 ASP.NET 框架,ASP.NET Core沒有讓我們失望,通過本次測試,我們大概對ASP.NET Core的性能心裏有底了。一個圈子的良好發展需要社區的共同參與,也希望大家共同爲.NET Core社區貢獻自己的力量,同時也希望看到本篇文章的CTOs們以後在平臺和框架選擇的過程中考慮一下ASP.NET Core,因爲她真的很優秀。
原文鏈接:http://www.cnblogs.com/savorboard/p/dotnet-benchmarks.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注