using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; namespace TestCSharp2115 { public static class Program { private static IMongoDatabase __adminDatabase; private static IMongoClient __client; private static int __count; public static void Main(string[] args) { __client = new MongoClient("mongodb://localhost/?waitqueuesize=10000"); __adminDatabase = __client.GetDatabase("admin"); //TestSync(); //TestAsync().GetAwaiter().GetResult(); TestFakeAsync().GetAwaiter().GetResult(); Console.ReadLine(); } public static void TestSync() { var startTime = DateTime.Now; var numberOfThreads = 1000; var threads = new List(); for (var i = 0; i < numberOfThreads; i++) { var thread = new Thread(Ping); threads.Add(thread); } Console.WriteLine($"{DateTime.Now} starting threads"); foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } var duration = (int)(DateTime.Now - startTime).TotalSeconds; Console.WriteLine($"{DateTime.Now} {__count} threads finished in {duration} seconds"); } public static async Task TestAsync() { var startTime = DateTime.Now; Console.WriteLine($"{DateTime.Now} starting tasks"); var numberOfTasks = 1000; var tasks = new List(); for (var i = 0; i < numberOfTasks; i++) { var task = PingAsync(); tasks.Add(task); } await Task.WhenAll(tasks.ToArray()); var duration = (int)(DateTime.Now - startTime).TotalSeconds; Console.WriteLine($"{DateTime.Now} {__count} tasks finished in {duration} seconds"); } public static async Task TestFakeAsync() { var startTime = DateTime.Now; Console.WriteLine($"{DateTime.Now} starting tasks"); var numberOfTasks = 100; var tasks = new List(); for (var i = 0; i < numberOfTasks; i++) { var task = Task.Run(() => Ping()); tasks.Add(task); } await Task.WhenAll(tasks.ToArray()); var duration = (int)(DateTime.Now - startTime).TotalSeconds; Console.WriteLine($"{DateTime.Now} {__count} tasks finished in {duration} seconds"); } public static void Ping() { var result = __adminDatabase.RunCommand("{ ping : 1 }"); var count = Interlocked.Increment(ref __count); Console.WriteLine($"{DateTime.Now} ping {count} succeeded"); } public static async Task PingAsync() { var result = await __adminDatabase.RunCommandAsync("{ ping : 1 }"); var count = Interlocked.Increment(ref __count); Console.WriteLine($"{DateTime.Now} ping {count} succeeded"); } } }