前言
性能是我們日常生活中經常接觸到的一個詞語,更好的性能意味著能給我們帶來更好的用戶體檢。比如我們在購買手機、顯卡、CPU等的時候,可能會更加的關注于這樣指標,所以本篇就來做一個性能評測。
性能也一直是我們開發人員一直追求的一個目標,我們在做語言選擇,平臺選擇,架構選擇的過程中都需要在性能之間做衡量。
同樣性能對 .NET Core 團隊來說也是至關重要的,一項新技術的誕生,除了對生產力的提高,還有技術團隊對性能的追求。
今天,我們就來做一個對比測試,來看看微軟的這樣新技術性能到底怎么樣,俗話說的好:“是騾子是馬,拉出來溜溜”。
下面讓我開始吧。
目錄
- 測試目標
- 測試工具
- 環境準備
- 開始測試
ASP.NET Core Kestrel vs ASP.NET Core IIS
ASP.NET Core IIS vs ASP.NET IIS
ASP.NET Core Kestrel vs ASP.NET IIS
ASP.NET Core vs Python Django
ASP.NET Core vs Java Servlet
ASP.NET Core vs NodeJS
- 總結
測試目標
在測試之前,我們必須要明確我們本次測試想達到的一個目標。本次測試主要是測試應用程序的一個吞吐量。其中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-1 | ASP.NET Core vs NodeJS Express |
Linux | Kestrel vs self host |
相同平臺不同語言不同宿主不同框架間性能差距 |
6-2 | ASP.NET Core vs NodeJS Koa |
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 |
虛擬機器3 | Nano Server | 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:5000
Running 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.43
Transfer/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:5001
Running 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.97
Transfer/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:5001
Running 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.97
Transfer/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:10280
Running 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.50
Transfer/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:5000
Running 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.43
Transfer/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:10280
Running 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.50
Transfer/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.83
Transfer/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:8000
Running 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 0
Requests/sec: 1572.64
Transfer/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.83
Transfer/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/hello
Running 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.73
Transfer/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.83
Transfer/sec: 3.37MB
NodeJS
- 環境:虛擬機器2
- OS:Linux
- Host:self host
wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:1337
Running 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.89
Transfer/sec: 3.05MB
***************更新1:NodeJS 添加Web框架*******
Express框架,cluster模式
wrk -t 2 -c 30 -d 20 --latency http://192.168.2.48:1337
Running 20s test @ http://192.168.2.48:1337
2 threads and 30 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.97ms 1.45ms 32.23ms 83.97%
Req/Sec 7.83k 0.90k 8.82k 91.50%
Latency Distribution
50% 2.00ms
75% 2.50ms
90% 3.50ms
99% 6.00ms
311896 requests in 20.01s, 66.03MB read
Requests/sec: 15583.99
Transfer/sec: 3.30MB
Koa框架,cluster模式
wrk -t 2 -c 30 -d 20 --latency http://192.168.2.48:1337
Running 20s test @ http://192.168.2.48:1337
2 threads and 30 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.74ms 0.86ms 13.59ms 86.65%
Req/Sec 8.79k 804.39 9.98k 87.75%
Latency Distribution
50% 1.99ms
75% 2.00ms
90% 2.96ms
99% 4.83ms
349856 requests in 20.02s, 53.38MB read
Requests/sec: 17478.73
Transfer/sec: 2.67MB
從測試結果可以看出,Koa框架性能略高于Express框架。
**************End***********
總結
QPS(ASP.NET Core + Kestrel):26730.83
QPS(NodeJS):20522.89 (非cluster模式)
QPS(NodeJS Express):15583.99 (cluster模式)
QPS(NodeJS Koa):17478.73 (cluster模式)
這個結果著實讓我吃了一驚,NodeJS性能竟然如此驚人,比JAVA要快10%。作為一個解釋性語言這個性能可以說達到了極致,雖然在測試之前知道NodeJS采用的是異步IO,但還是被測試結果震驚了。
===========更新1=========
NodeJS 在加入了Web框架之后,性能仍然不弱。
不知道是不是因為NodeJS沒有經過什么Web框架,直接輸出的結果。所以我需要再加測一個ASP.NET Core 通過中間件直接輸入結果的性能,這次我要使用微軟的測試項目benchmarks。
wrk -t 2 -c 50 -d 20 --latency http://192.168.2.48:5000/plaintext
Running 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.57
Transfer/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-1 | ASP.NET Core vs NodeJS Express |
Linux | Kestrel vs self host |
26.7k vs 15.6k |
6-2 | ASP.NET Core vs NodeJS Koa |
Linux | Kestrel vs self host |
26.7k vs 17.5k |
作為微軟的下一代 ASP.NET 框架,ASP.NET Core沒有讓我們失望,通過本次測試,我們大概對ASP.NET Core的性能心里有底了。一個圈子的良好發展需要社區的共同參與,也希望大家共同為.NET Core社區貢獻自己的力量,同時也希望看到本篇文章的CTOs們以后在平臺和框架選擇的過程中考慮一下ASP.NET Core,因為她真的很優秀。
如果你覺得本篇博客對您有幫助的話,感謝您的【推薦】,如果你對.NET Core感興趣可以關注我,我會定期在博客分享關于.NET Core的學習心得。
========更新1 :2016-10-17 感謝園友“幻天芒” 關于NodeJS的貢獻======
有園友反應NodeJS項目沒有使用web mvc框架,所以特更新,同時感謝 "幻天芒" 在github向nodeJS項目提交的PR。
1、添加node 多核cpu cluster 模式
2、添加node koa
框架和express
框架測試
更新測試結果。
========更新2 :2016-10-19 添加ASP.NET Core 在Windows Nano Server的測試結果======
環境:虛擬機器3,和Linux硬件一樣
wrk -t 2 -c 30 -d 20 --latency http://192.168.2.52:8000
Running 20s test @ http://192.168.2.52:8000
2 threads and 30 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.08ms 709.98us 31.25ms 77.30%
Req/Sec 13.98k 1.38k 15.80k 87.75%
Latency Distribution
50% 1.00ms
75% 1.03ms
90% 2.00ms
99% 3.45ms
556354 requests in 20.03s, 70.04MB read
Requests/sec: 27780.50
Transfer/sec: 3.50MB
這個測試結果和微軟的測試結果是一致的,Nano Server大概比在Linux上高出5-10%的性能。
========更新3 :2016-12-30 添加 WebListener 測試 ======
WebListener 是基于 Http.sys 實現的非跨平臺只能運行于 Windows 的一個 web 服務器,其目的我覺得是為了替代iis的性能不足問題。
引用自QQ群 Lyrics:我的理解是這樣的,Kestrel是跨平臺的,定義了一套通用的 feature,然而目前在windows平臺上,Kestrel所具備的feature 并沒有 http.sys 提供的那么強大,為了使老系統能順利遷移到core上面,微軟不得已搞了一個能支持所有http.sys 的web server,就是weblistener, weblistener 能完整的利用 http.sys 的特性,在windows上功能完整。
測試結果:
Windows ASP.NET Core Kestrel :35.5k
Windows ASP.NET Core WebListener:27.9k
Kestrl 大概比 WebListener 高出 5-10%的性能。
本文地址:http://www.cnblogs.com/savorboard/p/dotnet-benchmarks.html
作者博客:Savorboard
歡迎轉載,請在明顯位置給出出處及鏈接
文章列表
![]() |
不含病毒。www.avast.com |