這篇文章主要介紹.net中關(guān)于異步性能測試的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)基于成都重慶香港及美國等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報價,主機(jī)托管價格性價比高,為金融證券行業(yè)西云機(jī)房,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
首先,建一個 ASP.NET MVC WebAPI項(xiàng)目,在默認(rèn)的控制器 values里面,增加兩個方法:
// GET api/values?sleepTime=10 [HttpGet] public async Task<string> ExecuteAIO(int sleepTime) { await Task.Delay(sleepTime); return "Hello world,"+ sleepTime; } [HttpGet] // GET api/values?sleepTime2=10 public string ExecuteBIO(int sleepTime2) { System.Threading.Thread.Sleep(sleepTime2); return "Hello world," + sleepTime2; }
然后,建立一個控制臺程序,來測試這個web API:
class Program { static void Main(string[] args) { Console.WriteLine("按任意鍵開始測試 WebAPI:http://localhost:62219/api/values?sleepTime={int}"); Console.Write("請輸入線程數(shù):"); int threadNum = 100; int.TryParse(Console.ReadLine(), out threadNum); while (Test(threadNum)) ; Console.ReadLine(); Console.ReadLine(); } private static bool Test(int TaskNumber) { Console.Write("請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:"); string input = Console.ReadLine(); int SleepTime = 50; if (!int.TryParse(input, out SleepTime)) return false; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:62219/"); var result = client.GetStringAsync("api/values?sleepTime=" + input).Result; Console.WriteLine("Result:{0}", result); //int TaskNumber = 1000; Console.WriteLine("{0}次 BIO(同步)測試(睡眠{1} 毫秒):", TaskNumber, SleepTime); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Task[] taskArr = new Task[TaskNumber]; for (int i = 0; i < TaskNumber; i++) { Task task = client.GetStringAsync("api/values?sleepTime2=" + SleepTime); taskArr[i] = task; } Task.WaitAll(taskArr); sw.Stop(); double useTime1 = sw.Elapsed.TotalSeconds; Console.WriteLine("耗時(秒):{0},QPS:{1,10:f2}", useTime1, TaskNumber/useTime1); sw.Reset(); Console.WriteLine("{0}次 AIO(異步)測試(睡眠{1} 毫秒):", TaskNumber, SleepTime); sw.Start(); for (int i = 0; i < TaskNumber; i++) { Task task = client.GetStringAsync("api/values?sleepTime=" + SleepTime); taskArr[i] = task; } Task.WaitAll(taskArr); sw.Stop(); double useTime2 = sw.Elapsed.TotalSeconds; Console.WriteLine("耗時(秒):{0},QPS:{1,10:f2}", useTime2, TaskNumber / useTime2); return true; } }
View Code
其實(shí)主要是下面幾行代碼:
HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:62219/");var result = client.GetStringAsync("api/values?sleepTime=" + input).Result;
注意,你可能需要使用Nuget添加下面這個包:
Microsoft.AspNet.WebApi.Client
最后,運(yùn)行這個測試,結(jié)果如下:
按任意鍵開始測試 WebAPI:http://localhost:62219/api/values?sleepTime={int} 請輸入線程數(shù):1000 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:10 Result:"Hello world,10" 1000次 BIO(同步)測試(睡眠10 毫秒): 耗時(秒):1.2860545,QPS: 777.57 1000次 AIO(異步)測試(睡眠10 毫秒): 耗時(秒):0.4895946,QPS: 2042.51 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:100 Result:"Hello world,100" 1000次 BIO(同步)測試(睡眠100 毫秒): 耗時(秒):8.2769307,QPS: 120.82 1000次 AIO(異步)測試(睡眠100 毫秒): 耗時(秒):0.5435111,QPS: 1839.89
本來想嘗試測試10000個線程,但報錯了。
上面的測試結(jié)果,QPS并不高,但由于使用的是IISExpress,不同的Web服務(wù)器軟件性能不相同,所以還得對比下進(jìn)程內(nèi)QPS結(jié)果,于是新建一個控制臺程序,代碼如下:
class Program { static void Main(string[] args) { Console.WriteLine("按任意鍵開始測試 "); Console.Write("請輸入線程數(shù):"); int threadNum = 100; int.TryParse(Console.ReadLine(), out threadNum); while (Test(threadNum)) ; Console.ReadLine(); Console.ReadLine(); } private static bool Test(int TaskNumber) { Console.Write("請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:"); string input = Console.ReadLine(); int SleepTime = 50; if (!int.TryParse(input, out SleepTime)) return false; var result = ExecuteAIO(SleepTime).Result; Console.WriteLine("Result:{0}", result); //int TaskNumber = 1000; Console.WriteLine("{0}次 BIO(同步)測試(睡眠{1} 毫秒):", TaskNumber, SleepTime); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); Task[] taskArr = new Task[TaskNumber]; for (int i = 0; i < TaskNumber; i++) { Task task = Task.Run<string>(()=> ExecuteBIO(SleepTime)); taskArr[i] = task; } Task.WaitAll(taskArr); sw.Stop(); double useTime1 = sw.Elapsed.TotalSeconds; Console.WriteLine("耗時(秒):{0},QPS:{1,10:f2}", useTime1, TaskNumber / useTime1); sw.Reset(); Console.WriteLine("{0}次 AIO(異步)測試(睡眠{1} 毫秒):", TaskNumber, SleepTime); sw.Start(); for (int i = 0; i < TaskNumber; i++) { Task task = ExecuteAIO(SleepTime); taskArr[i] = task; } Task.WaitAll(taskArr); sw.Stop(); double useTime2 = sw.Elapsed.TotalSeconds; Console.WriteLine("耗時(秒):{0},QPS:{1,10:f2}", useTime2, TaskNumber / useTime2); return true; } public static async Task<string> ExecuteAIO(int sleepTime) { await Task.Delay(sleepTime); return "Hello world," + sleepTime; } public static string ExecuteBIO(int sleepTime2) { System.Threading.Thread.Sleep(sleepTime2); //不能在非異步方法里面使用 Task.Delay,否則可能死鎖 //Task.Delay(sleepTime2).Wait(); return "Hello world," + sleepTime2; } }
View Code
注意,關(guān)鍵代碼只有下面兩個方法:
public static async Task<string> ExecuteAIO(int sleepTime) { await Task.Delay(sleepTime); return "Hello world," + sleepTime; } public static string ExecuteBIO(int sleepTime2) { System.Threading.Thread.Sleep(sleepTime2); //不能在非異步方法里面使用 Task.Delay,否則可能死鎖 //Task.Delay(sleepTime2).Wait(); return "Hello world," + sleepTime2; }
這兩個方法跟WebAPI的測試方法代碼是一樣的,但是調(diào)用代碼稍微不同:
同步調(diào)用:
Task[] taskArr = new Task[TaskNumber]; for (int i = 0; i < TaskNumber; i++) { Task task = Task.Run<string>(()=> ExecuteBIO(SleepTime)); taskArr[i] = task; } Task.WaitAll(taskArr);
異步調(diào)用:
for (int i = 0; i < TaskNumber; i++) { Task task = ExecuteAIO(SleepTime); taskArr[i] = task; } Task.WaitAll(taskArr);
可見,這里測試的時候,同步和異步調(diào)用,客戶端代碼都是使用的多線程,主要的區(qū)別就是異步方法使用了 async/await 語句。
下面是非Web的進(jìn)程內(nèi)異步多線程和同步多線程的結(jié)果:
請輸入線程數(shù):1000 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:10 Result:Hello world,10 1000次 BIO(同步)測試(睡眠10 毫秒): 耗時(秒):1.3031966,QPS: 767.34 1000次 AIO(異步)測試(睡眠10 毫秒): 耗時(秒):0.026441,QPS: 37820.05 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:100 Result:Hello world,100 1000次 BIO(同步)測試(睡眠100 毫秒): 耗時(秒):9.8502858,QPS: 101.52 1000次 AIO(異步)測試(睡眠100 毫秒): 耗時(秒):0.1149469,QPS: 8699.67 請輸入線程數(shù):10000 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:10 Result:Hello world,10 10000次 BIO(同步)測試(睡眠10 毫秒): 耗時(秒):7.7966125,QPS: 1282.61 10000次 AIO(異步)測試(睡眠10 毫秒): 耗時(秒):0.083922,QPS: 119158.27 請輸入此API方法的睡眠時間(毫秒),輸入非數(shù)字內(nèi)容退出:100 Result:Hello world,100 10000次 BIO(同步)測試(睡眠100 毫秒): 耗時(秒):34.3646036,QPS: 291.00 10000次 AIO(異步)測試(睡眠100 毫秒): 耗時(秒):0.1721833,QPS: 58077.64
結(jié)果表示,.NET程序開啟10000個任務(wù)(不是10000個原生線程,需要考慮線程池線程),異步方法的QPS超過了10萬,而同步方法只有1000多點(diǎn),性能差距還是很大的。
注:以上測試結(jié)果的測試環(huán)境是
Intel i7-4790K CPU,4核8線程,內(nèi)存 16GB,Win10 企業(yè)版
以上是“.net中關(guān)于異步性能測試的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:.net中關(guān)于異步性能測試的示例分析
瀏覽地址:http://chinadenli.net/article20/gehhco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、商城網(wǎng)站、營銷型網(wǎng)站建設(shè)、建站公司、App設(shè)計、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)