private static async void LaunchServer() { var host = new ProtocolHost(); host.LoadFromReference <Program>(); var dbOptions = new DbOptions(); dbOptions.SetCreateIfMissing(); var rocksDb = RocksDbSharp.RocksDb.Open(dbOptions, @"C:\rocks\primaryservicetest\"); host.Resolver.Register(rocksDb, false); var primaryRepository = new RocksPrimaryRepository(rocksDb, nameof(TestEntity)); primaryRepository.WriteOptions.SetSync(false); host.Resolver.Register(primaryRepository); var indexMaps = new IndexMapProvider <TestEntity>(); host.Resolver.Register(indexMaps); indexMaps.AddScoreIndex(e => e.Score, IndexLevel.Entity, OrderType.Asc | OrderType.Desc); host.RegisterHandler <PrimaryRepositoryService <TestEntity> >(true); var serverTask = host.StartServerAsync("127.0.0.1", 8080, CancellationToken.None); Console.WriteLine("Service Started"); }
public static async Task LaunchClient() { var host = new ProtocolHost(); host.LoadFromReference <Program>(); using (var session = await host.StartClientAsync("127.0.0.1", 8080, CancellationToken.None)) { Console.WriteLine("Client connected"); var repository = session.CreateChannel <IPrimaryRepositoryService <TestEntity> >(); var entityId = Enumerable.Range(0, 10000).Select(i => ByteArray.New(32)).ToArray(); List <Task> tasks = new List <Task>(); var rand = new Random(); var watch = Stopwatch.StartNew(); //for (int i = 0; i < entityId.Length; i++) //{ // var score = rand.Next(); // var task = repository.SaveAsync(new TestEntity() // { // Id = entityId[i], // Name= "card 1", // Value = 1, // Score = i, // }); // tasks.Add(task); // //var card = await repository.Get(entityId[i]); //} //await Task.WhenAll(tasks); for (int i = 0; i < 3; i++) { await repository.SaveAsync(new TestEntity() { Id = ByteArray.New(32), Name = "card 1", Value = 1, Score = i, }); } var firstTen = await repository.GetAsync(OrderType.Asc, false, 0, 10); watch.Stop(); // save async: 10k 0.82s Console.WriteLine($"Done in {watch.Elapsed}"); Console.WriteLine($"{watch.Elapsed.TotalMilliseconds} ms."); } }
public async Task HostAndChannelFactory() { var host = new ProtocolHost(); host.LoadFromReference <ProtocolHostTests>(); host.Resolver.Register(new CalculatorParameter() { First = 10 }); var cts = new CancellationTokenSource(); var serverTask = host.StartServerAsync("127.0.0.1", 91, cts.Token); var channelProvider = new ChannelProvider("127.0.0.1", 91); channelProvider.LoadFromReference <ProtocolHostTests>(); var calculator = await channelProvider.GetChannelAsync <ICalculator>(); var result = await calculator.AddAsync("9"); Assert.AreEqual(19, result.Result); cts.Cancel(); try { await serverTask; } catch { }; host = new ProtocolHost(); host.LoadFromReference <ProtocolHostTests>(); host.Resolver.Register(new CalculatorParameter() { First = 10 }); serverTask = host.StartServerAsync("127.0.0.1", 91, CancellationToken.None); var channelProviderCollection = new ChannelProviderCollection(channelProvider); calculator = await channelProviderCollection.GetChannelAsync <ICalculator>(); result = await calculator.AddAsync("9"); Assert.AreEqual(19, result.Result); }
public async Task HostAndCreateChannel() { // Create host var host = new ProtocolHost(); // Load controllers from reference. host.LoadFromReference <ProtocolHostTests>(); // Register object to dependency injection. Just an example. This test doesn't require this. host.Resolver.Register(new CalculatorParameter() { First = 10 }); // Start server. Should be skiped on client side. var serverTask = host.StartServerAsync("127.0.0.1", 90, CancellationToken.None); IResolverSession clientSession = null; // Handle client connections. host.ClientConnected += (h, s) => { // Session of client connected to server. clientSession = s; }; // Can be used in Program.Main to run server. Task ends when server socket stops listening. // await serverTask; // Connect client. Should be skiped on server side. var session = await host.StartClientAsync("127.0.0.1", 90); // Create channel of IAddMethod interface on client side and call method from client to server. var serverSideCalculator = session.CreateChannel <IAddMethod>(); // Create Stopwatch for performance measure. var watch = Stopwatch.StartNew(); var tasks = new List <Task <CalculatorResult> >(); // Run asynchronous 10k requests. for (int i = 0; i < 10000; i++) { // From client call method on server side. var task = serverSideCalculator.AddAsync(10, 9); tasks.Add(task); } // Wait when all 10k requests ends. var serverResults = await Task.WhenAll(tasks); // Stop Stopwatch. watch.Stop(); // Check result. foreach (var serverResult in serverResults) { Assert.AreEqual(19, serverResult.Result); } // Check if elapsed time is less than one second. Assert.IsTrue(watch.ElapsedMilliseconds < 1000, $"Elapsed time is more than one second. Elapsed time in milliseconds: {watch.ElapsedMilliseconds}"); // Since we created both server and client from same ProtocolHost, they have same controllers that we can call. // Create channel of IAddMethod interface on server side and call method from server to client. var clientSideCalculator = clientSession.CreateChannel <IAddMethod>(); var clientResult = await clientSideCalculator.AddAsync(10, 10); Assert.AreEqual(20, clientResult.Result); // Close client connection. session.Dispose(); }