static void RunOrleans() { var config = ClientConfiguration.LocalhostSilo(26224); try { GrainClient.Initialize(config); Console.WriteLine("Client successfully connect to silo host"); } catch (Exception ex) { Console.WriteLine($"Orleans client initialization failed failed due to {ex}"); Console.ReadLine(); return; } var friend = GrainClient.GrainFactory.GetGrain <IHello>(0); var name = "world"; CodeTimerAdvance.TimeByConsole("orleans", _count, (i) => { var response = friend.SayHello(new RpcTest_OrleansServer.Interfaces.HelloRequest { Name = name }).Result; }, _threadcount); Console.WriteLine("Press Enter to terminate..."); Console.ReadLine(); }
static void RunThriftBio() { //TTransport transport = new TSocket("localhost", 7911); //TProtocol protocol = new TBinaryProtocol(transport); //Helloword.Client client = new Helloword.Client(protocol); //transport.Open(); //String user = "******"; //CodeTimerAdvance.TimeByConsole("thrift bio", _count, a => //{ // var reply = client.SayHello(new HelloRequest { Name = user }); //}, _threadcount); //transport.Close(); //多线程不能复用TSocket故需每次实例化。 String user = "******"; CodeTimerAdvance.TimeByConsole("thrift bio", _count, a => { TTransport transport = new TSocket("localhost", 7911); transport.Open(); TProtocol protocol = new TBinaryProtocol(transport); using (Helloword.Client client = new Helloword.Client(protocol)) { var reply = client.SayHello(new HelloRequest { Name = user }); } transport.Close(); }, _threadcount); }
/// <summary> /// 快速计时并且返回当前计时实例 /// </summary> /// <param name="times"></param> /// <param name="action"></param> /// <param name="threadCount"></param> /// <returns></returns> public static CodeTimerAdvance Time(Int32 times, Action <Int32> action, int threadCount = 1) { CodeTimerAdvance timer = new CodeTimerAdvance { Times = times, Action = action }; timer.TimeOne(); //不要预热的话就注释 timer.Time(threadCount); return(timer); }
static void RunHttpClient() { var httpClient = new HttpClient(); CodeTimerAdvance.TimeByConsole("httpclient", _count, a => { var response = httpClient.PostAsync("http://localhost:23455/api/Values/SayHello", new FormUrlEncodedContent(new Dictionary <string, string> { { "Name", "world" }, })); var res = response.Result.Content.ReadAsStringAsync().Result; }, _threadcount); }
static void RunGrpc() { Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); String user = "******"; CodeTimerAdvance.TimeByConsole("gRpc", _count, a => { var client = new Greeter.GreeterClient(channel); var reply = client.SayHello(new Helloworld.HelloRequest { Name = user + a }); }, _threadcount); channel.ShutdownAsync().Wait(); }
static void RunWcfTcp() { string address = "net.tcp://localhost:8002/"; ChannelFactory <RpcTest_WCFServer.IHelloword> channel = new ChannelFactory <RpcTest_WCFServer.IHelloword>( new NetTcpBinding(), new EndpointAddress( new Uri(address))); RpcTest_WCFServer.IHelloword proxy = channel.CreateChannel(); String user = "******"; CodeTimerAdvance.TimeByConsole("wcf tcp", _count, a => { var reply = proxy.SayHello(new RpcTest_WCFServer.HelloRequest { Name = user }).Result; }, _threadcount); }
static void RunHttpWebRequest() { string url = "http://localhost:23455/api/Values/SayHello"; CodeTimerAdvance.TimeByConsole("HttpWebRequest", _count, a => { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.Method = "post"; webRequest.ContentType = "application/x-www-form-urlencoded"; string postData = "Name=world"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); webRequest.ContentLength = byteArray.Length; System.IO.Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); string res = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); }, _threadcount); }
/// <summary> /// 计时,并用控制台输出行 /// </summary> /// <param name="title"></param> /// <param name="times"></param> /// <param name="action"></param> /// <param name="threadCount"></param> public static void TimeByConsole(String title, Int32 times, Action <Int32> action, int threadCount = 1) { ConsoleColor currentForeColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.Write("{0,16}:\r\n", title); CodeTimerAdvance timer = new CodeTimerAdvance { Times = times, Action = action, ShowProgress = true }; Console.ForegroundColor = ConsoleColor.Yellow; //timer.TimeOne(); //不要预热的话就注释 timer.Time(threadCount); Console.WriteLine(timer.ToString()); Console.ForegroundColor = currentForeColor; }