// 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 Thrift.Protocol; using Thrift.Transport; using ThriftTest.Gen; namespace ThriftTest.Client { public class Program { private static Test.Client GetClient(string host, int port) { TTransport transport = new TSocket(host, port); transport.Open(); return new Test.Client(new TBinaryProtocol(transport)); } private static void TestFunctional(string host, int port) { Test.Client tst = GetClient(host, port); int a = 123; int b = 456; int c = tst.add(a, b); Console.WriteLine(c); string s = "ABC"; string s2 = tst.dup(s); Console.WriteLine(s2); Data d = new Data(123, "ABC"); Data d2 = tst.process(d); Console.WriteLine("{0} {1}", d2.Iv, d2.Sv); tst.OutputProtocol.Transport.Close(); } private static void TestInstantiation(string host, int port) { Test.Client tst1 = GetClient(host, port); for(int i = 0; i < 2; i++) { int n = tst1.getCounter(); Console.WriteLine(n); } tst1.OutputProtocol.Transport.Close(); Test.Client tst2 = GetClient(host, port); for(int i = 0; i < 2; i++) { int n = tst2.getCounter(); Console.WriteLine(n); } tst2.OutputProtocol.Transport.Close(); } private const int REP = 100000; private static void TestPerformance(string host, int port) { Test.Client tst = GetClient(host, port); DateTime dt1 = DateTime.Now; Enumerable.Range(0, REP).ToList().ForEach(i => { tst.noop(); }); // not parallel as Thrift clients ar enot thread safe DateTime dt2 = DateTime.Now; Console.WriteLine("{0} requests per second", (int)(REP / (dt2 - dt1).TotalSeconds)); tst.OutputProtocol.Transport.Close(); } private static void Test(string lbl, string host, int port) { Console.WriteLine(lbl + ":"); TestFunctional(host, port); TestInstantiation(host, port); TestPerformance(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 PYTHON_HOST = "localhost"; private const int PYTHON_PORT = 12347; public static void Main(string[] args) { Test("Java", JAVA_HOST, JAVA_PORT); Test(".NET", DOTNET_HOST, DOTNET_PORT); Test("Python", PYTHON_HOST, PYTHON_PORT); } } }