// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Grpc.Net.Client; using Grpc.Core; using GrpcTest.Gen; namespace GrpcTest { public class Program { private static void TestFunctional(string host, int port) { GrpcChannel ch = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst = new Test.TestClient(ch); int a = 123; int b = 456; int c = tst.Add(new AddRequest { A = a, B = b }).C; Console.WriteLine(c); string s = "ABC"; string s2 = tst.Dup(new DupRequest { S = s }).S2; Console.WriteLine(s2); Data d = new Data { Iv = 123, Sv = "ABC" }; Data d2 = tst.Process(d); Console.WriteLine("{0} {1}", d2.Iv, d2.Sv); ch.ShutdownAsync().Wait(); } private static void TestInstantiation(string host, int port) { GrpcChannel ch1 = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst1 = new Test.TestClient(ch1); for(int i = 0; i < 2; i++) { int n = tst1.GetCounter(new Empty()).Counter; Console.WriteLine(n); } ch1.ShutdownAsync().Wait(); GrpcChannel ch2 = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst2 = new Test.TestClient(ch2); for(int i = 0; i < 2; i++) { int n = tst2.GetCounter(new Empty()).Counter; Console.WriteLine(n); } ch2.ShutdownAsync().Wait(); } private const int REP = 100000; private static void TestPerformance(string host, int port) { GrpcChannel ch = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst = new Test.TestClient(ch); DateTime dt1 = DateTime.Now; Enumerable.Range(0, REP).ToList().ForEach(i => { tst.Noop(new Empty()); }); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} requests per second", (int)(REP / (dt2 - dt1).TotalSeconds)); ch.ShutdownAsync().Wait(); } private const int REP2 = 10; private static void TestStream(string host, int port) { GrpcChannel ch = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst = new Test.TestClient(ch); using(AsyncDuplexStreamingCall stm = tst.StreamProcess()) { CountdownEvent cde = new CountdownEvent(REP2); Task resp = Task.Run(async () => { while(await stm.ResponseStream.MoveNext()) { Data d2 = stm.ResponseStream.Current; Console.WriteLine("{0} {1}", d2.Iv, d2.Sv); cde.Signal(); } }); for(int i = 0; i < REP2; i++) { Data d = new Data { Iv = i, Sv = new String((char)(65 + i), 1) }; stm.RequestStream.WriteAsync(d).Wait(); } stm.RequestStream.CompleteAsync().Wait(); cde.Wait(); cde.Dispose(); } ch.ShutdownAsync().Wait(); } private static void TestStreamPerformance(string host, int port) { GrpcChannel ch = GrpcChannel.ForAddress("http://" + host + ":" + port); Test.TestClient tst = new Test.TestClient(ch); using(AsyncDuplexStreamingCall stm = tst.StreamNoop()) { DateTime dt1 = DateTime.Now; CountdownEvent cde = new CountdownEvent(REP); Task resp = Task.Run(async () => { while(await stm.ResponseStream.MoveNext()) { cde.Signal(); } }); for(int i = 0; i < REP; i++) { stm.RequestStream.WriteAsync(new Empty()).Wait(); } stm.RequestStream.CompleteAsync().Wait(); cde.Wait(); cde.Dispose(); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} requests per second", (int)(REP / (dt2 - dt1).TotalSeconds)); } ch.ShutdownAsync().Wait(); } private static void Test(string lbl, string host, int port) { Console.WriteLine(lbl + ":"); TestFunctional(host, port); TestInstantiation(host, port); TestPerformance(host, port); TestStream(host, port); TestStreamPerformance(host, port); } private const string JAVA_HOST = "localhost"; private const int JAVA_PORT = 12345; private const string DOTNET_HOST = "localhost"; private const int DOTNET_PORT = 12346; private const string DOTNET2_HOST = "localhost"; private const int DOTNET2_PORT = 12347; private const string PYTHON_HOST = "localhost"; private const int PYTHON_PORT = 12348; private const string CPP_HOST = "localhost"; private const int CPP_PORT = 12349; public static void Main(string[] args) { Test("Java", JAVA_HOST, JAVA_PORT); Test(".NET", DOTNET_HOST, DOTNET_PORT); Test(".NET (MS)", DOTNET2_HOST, DOTNET2_PORT); Test("Python", PYTHON_HOST, PYTHON_PORT); Test("C++", CPP_HOST, CPP_PORT); } } }