Ejemplo n.º 1
0
        public static void Execute(string host, string port, long length)
        {
            Channel channel = new Channel(string.Format("{0}:{1}", host, port), ChannelCredentials.Insecure);
            var     client  = new RPCDemoService.RPCDemoServiceClient(channel);

            var stopwatch = Stopwatch.StartNew();

            for (var i = 0; i < length; i++)
            {
                var reply = client.GetById(new DemoId()
                {
                    Id = i
                });
                Console.WriteLine("receive" + JsonConvert.SerializeObject(reply));
            }
            stopwatch.Stop();

            Console.WriteLine(string.Format("call GetById repeat={0}, time={1} Milliseconds, time/repeat={2}", length, stopwatch.ElapsedMilliseconds, stopwatch.ElapsedMilliseconds / (float)length));
            Console.ReadKey();

            channel.ShutdownAsync().Wait();
        }
Ejemplo n.º 2
0
        public static void TestInstance(string host, long length)
        {
            string  strGet = "", strAdd = "";
            Channel channel = new Channel(host, ChannelCredentials.Insecure);

            TaskFactory factory    = new TaskFactory();
            TaskFactory factoryAdd = new TaskFactory();

            List <Task> listTaskGet = new List <Task>();
            List <Task> listTaskAdd = new List <Task>();

            var stopwatch    = Stopwatch.StartNew();
            var stopwatchAdd = Stopwatch.StartNew();

            for (var i = 0; i < length; i++)
            {
                var task = new Task(
                    () =>
                {
                    var client = new RPCDemoService.RPCDemoServiceClient(channel);
                    Console.WriteLine("receive" + JsonConvert.SerializeObject(client.GetById(new DemoId()
                    {
                        Id = i
                    })));
                }
                    );
                listTaskGet.Add(task);

                var taskAdd = new Task(

                    () =>
                {
                    var client = new RPCDemoService.RPCDemoServiceClient(channel);
                    Console.WriteLine("receive" + JsonConvert.SerializeObject(client.Add(new DemoRequest()
                    {
                        Id = DateTime.Now.Ticks.ToString(), CommentId = i
                    })));
                }
                    );

                listTaskAdd.Add(taskAdd);
            }

            factory.ContinueWhenAll(listTaskGet.ToArray(),
                                    p =>
            {
                strGet = string.Format("instance: call GetById method length={0},time={1} Milliseconds, time/repeat={2}"
                                       , length
                                       , stopwatch.ElapsedMilliseconds
                                       , stopwatch.ElapsedMilliseconds / (float)length);
                Console.WriteLine(strGet);
                Console.WriteLine(strAdd);
            }
                                    );

            factoryAdd.ContinueWhenAll(listTaskAdd.ToArray(),
                                       p =>
            {
                strAdd = string.Format("instance: call Add method length={0},time={1} Milliseconds, time/repeat={2}"
                                       , length
                                       , stopwatchAdd.ElapsedMilliseconds
                                       , stopwatchAdd.ElapsedMilliseconds / (float)length);

                Console.WriteLine(strAdd);
                Console.WriteLine(strGet);
                channel.ShutdownAsync().Wait();
            }
                                       );

            listTaskGet.ForEach(p => p.Start());
            listTaskAdd.ForEach(p => p.Start());
        }