前言html
性能是咱們平常生活中常常接觸到的一個詞語,更好的性能意味着能給咱們帶來更好的用戶體檢。好比咱們在購買手機、顯卡、CPU等的時候,可能會更加的關注於這樣指標,因此本篇就來作一個性能評測。java
性能也一直是咱們開發人員一直追求的一個目標,咱們在作語言選擇,平臺選擇,架構選擇的過程當中都須要在性能之間作衡量。node
一樣性能對 .NET Core 團隊來講也是相當重要的,一項新技術的誕生,除了對生產力的提升,還有技術團隊對性能的追求。python
今天,咱們就來作一個對比測試,來看看微軟的這樣新技術性能到底怎麼樣,俗話說的好:「是騾子是馬,拉出來溜溜」。git
下面讓我開始吧。github
在測試以前,咱們必需要明確咱們本次測試想達到的一個目標。本次測試主要是測試應用程序的一個吞吐量。其中QPS,併發數,響應時間是咱們衡量吞吐量的幾個重要指標。web
如下是本次對比測試的任務目標:express
編號 | 對比方 | 系統環境 | 宿主環境 | 測試目標 |
---|---|---|---|---|
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 |
相同平臺不一樣語言不一樣宿主不一樣框架間性能差距 |
工欲善其事,必先利其器。django
首先咱們須要一個壓力測試工具,本次咱們使用 wrk,有關於wrk的介紹和使用,請查看個人 這篇博客。windows
而後咱們須要一個性能監控工具,由於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個核心給虛擬機使用,因此開的是雙線程。使用這個參數是我通過屢次測試,獲得的一個最佳的模擬效果。
ASP.NET Core
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
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的性能監視器,兩個的曲線圖差很少我就放一個了:
ASP.NET Core
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
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% 的使用率。
ASP.NET Core
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
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倍的性能,是有很大差距的。
注意,如下咱們開始使用到虛擬機器2了,咱們要在Windows性能監控器裏面查看CPU使用率,還須要再添加2個計數器。
物理處理器 \Hyper-V Hypervisor Logical Processor(*) %Total Run Time
虛擬處理器 \Hyper-V Hypervisor Virtual Processor(*) %Guest Run Time
ASP.NET Core
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
服務端宿主運行命令:
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 在作針對於作大請求併發狀況下,仍是弱了一點。
C# 和 JAVA 一直是兩大陣營的開發人員喜歡討論的話題,爲了不有陣營偏見,JAVA的源代碼是我委託咱們一個JAVA同事編寫的,而且委託由他部署的,而且已經交代了他避免使用jsp,由Servlet直接輸出。
ASP.NET Core
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
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。不說太多了,怕被噴...
ASP.NET Core
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
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。
一、添加node 多核cpu cluster 模式
二、添加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%的性能。