private static void Hello10000(ILog log, HelloClient helloClient) { log.DebugFormat("Hello10000, start ..."); var watch = Stopwatch.StartNew(); for (var i = 0; i < 10000; i++) { var request = new ActorMessageEnvelope <Hello10000Request>() { Message = new Hello10000Request() { Text = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff") }, }; helloClient.SayHello10000(request); } watch.Stop(); log.DebugFormat("Hello10000, end with cost {0} ms.", watch.ElapsedMilliseconds); }
private static void Hello10000MultiThreading(ILog log, HelloClient helloClient, int totalCalls, int threadCount) { log.DebugFormat("Hello10000MultiThreading, TotalCalls[{0}], ThreadCount[{1}], start ...", totalCalls, threadCount); var taskList = new Task[threadCount]; var watch = Stopwatch.StartNew(); for (int i = 0; i < threadCount; i++) { var task = Task.Factory.StartNew(() => { for (var j = 0; j < totalCalls / threadCount; j++) { var request = new ActorMessageEnvelope <Hello10000Request>() { Message = new Hello10000Request() { Text = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff") }, }; helloClient.SayHello10000(request); } }, TaskCreationOptions.PreferFairness); taskList[i] = task; } Task.WaitAll(taskList); watch.Stop(); log.DebugFormat("Hello10000MultiThreading, TotalCalls[{0}], ThreadCount[{1}] end with cost [{2}] ms." + "{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}", totalCalls, threadCount, watch.ElapsedMilliseconds, Environment.NewLine, string.Format(" Concurrency level: {0} threads", threadCount), Environment.NewLine, string.Format(" Complete requests: {0}", totalCalls), Environment.NewLine, string.Format("Time taken for tests: {0} seconds", (decimal)watch.ElapsedMilliseconds / 1000m), Environment.NewLine, string.Format(" Time per request: {0:#####0.000} ms (avg)", (decimal)watch.ElapsedMilliseconds / (decimal)totalCalls), Environment.NewLine, string.Format(" Requests per second: {0} [#/sec] (avg)", (int)((decimal)totalCalls / ((decimal)watch.ElapsedMilliseconds / 1000m))) ); }