很久沒有寫部落格了,今年做的產品公司這兩天剛開了發布會,稍微清閒下來,想想我們做的產品還有沒有性能優化空間,於是想到了.Net的異步可以優化性能,但到底能夠提升多大的比例呢?剛好有個朋友正在做各種語言的非同步效能測試(有關非同步和同步的問題,請參考客《AIO與BIO介面效能比較》),於是我今天寫了一個C#的測試程式。
首先,建立一個ASP.NET MVC WebAPI項目,在預設的控制器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("请输入线程数:"); 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方法的睡眠时间(毫秒),输入非数字内容退出:"); 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
#其實主要是下面幾行程式碼:
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
最後,執行這個測試,結果如下:
按任意键开始测试 WebAPI:http://localhost:62219/api/values?sleepTime={int} 请输入线程数:1000 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:10 Result:"Hello world,10" 1000次 BIO(同步)测试(睡眠10 毫秒): 耗时(秒):1.2860545,QPS: 777.57 1000次 AIO(异步)测试(睡眠10 毫秒): 耗时(秒):0.4895946,QPS: 2042.51 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:100 Result:"Hello world,100" 1000次 BIO(同步)测试(睡眠100 毫秒): 耗时(秒):8.2769307,QPS: 120.82 1000次 AIO(异步)测试(睡眠100 毫秒): 耗时(秒):0.5435111,QPS: 1839.89
本來想嘗試測試10000個線程,但報錯了。
上面的測試結果,QPS並不高,但由於使用的是IISExpress,不同的Web伺服器軟體效能不相同,所以還得對比下進程內QPS結果,於是新建一個控制台程序,程式碼如下:
class Program { static void Main(string[] args) { Console.WriteLine("按任意键开始测试 "); Console.Write("请输入线程数:"); 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方法的睡眠时间(毫秒),输入非数字内容退出:"); 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
請注意,關鍵程式碼只有下面兩個方法:
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的測試方法程式碼是一樣的,但是呼叫程式碼稍微不同:
同步呼叫:
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);
for (int i = 0; i < TaskNumber; i++) { Task task = ExecuteAIO(SleepTime); taskArr[i] = task; } Task.WaitAll(taskArr);
请输入线程数:1000 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:10 Result:Hello world,10 1000次 BIO(同步)测试(睡眠10 毫秒): 耗时(秒):1.3031966,QPS: 767.34 1000次 AIO(异步)测试(睡眠10 毫秒): 耗时(秒):0.026441,QPS: 37820.05 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:100 Result:Hello world,100 1000次 BIO(同步)测试(睡眠100 毫秒): 耗时(秒):9.8502858,QPS: 101.52 1000次 AIO(异步)测试(睡眠100 毫秒): 耗时(秒):0.1149469,QPS: 8699.67 请输入线程数:10000 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:10 Result:Hello world,10 10000次 BIO(同步)测试(睡眠10 毫秒): 耗时(秒):7.7966125,QPS: 1282.61 10000次 AIO(异步)测试(睡眠10 毫秒): 耗时(秒):0.083922,QPS: 119158.27 请输入此API方法的睡眠时间(毫秒),输入非数字内容退出:100 Result:Hello world,100 10000次 BIO(同步)测试(睡眠100 毫秒): 耗时(秒):34.3646036,QPS: 291.00 10000次 AIO(异步)测试(睡眠100 毫秒): 耗时(秒):0.1721833,QPS: 58077.64
以上是.net中關於非同步效能測試的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!