Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var measurements = new Dictionary <string, List <long> >
            {
                ["gRPC (native)"]        = new List <long>(),
                ["gRPC (REST)"]          = new List <long>(),
                ["Controller (JSON)"]    = new List <long>(),
                ["Controller (MSGPACK)"] = new List <long>()
            };

            var request = new HelloRequest {
                Name = "GreeterClient"
            };

            var sw = Stopwatch.StartNew();

            // Using gRPC client:
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new Greeter.GreeterClient(channel);

            sw.Restart();

            for (var i = 0; i < 1000; i++)
            {
                var reply = await client.SayHelloAsync(request);

                var measurement = sw.ElapsedMilliseconds;
                if (i > 1)
                {
                    measurements["gRPC (native)"].Add(measurement);
                }
                Console.WriteLine($"gRPC (native) {i}: {reply.Message} ({measurement}ms)");
                sw.Restart();
            }

            // Using HttpClient(s):
            using var httpClient = new HttpClient();
            sw.Restart();

            // Using gRPC endpoint over REST
            for (var i = 1; i <= 1000; i++)
            {
                using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://localhost:5001/v1/greeter/{request.Name}")
                      {
                          Content = JsonContent.Create(request),
                          Version = HttpVersion.Version20
                      };

                using var response = await httpClient
                                     .SendAsync(httpRequestMessage)
                                     .ConfigureAwait(false);

                var reply = await response.Content
                            .ReadFromJsonAsync <HelloReply>()
                            .ConfigureAwait(false);

                var measurement = sw.ElapsedMilliseconds;
                if (i > 1)
                {
                    measurements["gRPC (REST)"].Add(measurement);
                }
                Console.WriteLine($"gRPC (REST) {i}: {reply.Message} ({measurement}ms)");
                sw.Restart();
            }

            // Using API controller (POST, JSON)
            for (var i = 1; i <= 1000; i++)
            {
                using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/api/Greeter/SayHello")
                      {
                          Content = JsonContent.Create(request),
                          Version = HttpVersion.Version20
                      };

                var response = await httpClient
                               .SendAsync(httpRequestMessage)
                               .ConfigureAwait(false);

                var reply = await response.Content
                            .ReadFromJsonAsync <HelloReply>()
                            .ConfigureAwait(false);

                var measurement = sw.ElapsedMilliseconds;
                if (i > 1)
                {
                    measurements["Controller (JSON)"].Add(measurement);
                }
                Console.WriteLine($"Controller (JSON) {i}: {reply.Message} ({measurement})ms");
                sw.Restart();
            }

            for (var i = 1; i <= 1000; i++)
            {
                using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/api/Greeter/SayHello")
                      {
                          Content = JsonContent.Create(request),
                          Version = HttpVersion.Version20,
                      };

                httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-msgpack"));

                var response = await httpClient
                               .SendAsync(httpRequestMessage)
                               .ConfigureAwait(false);

                var stream = await response.Content
                             .ReadAsStreamAsync()
                             .ConfigureAwait(false);

                var reply = await MessagePackSerializer
                            .DeserializeAsync <HelloReply>(stream, CustomResolver.Options)
                            .ConfigureAwait(false);

                var measurement = sw.ElapsedMilliseconds;
                if (i > 1)
                {
                    measurements["Controller (MSGPACK)"].Add(measurement);
                }
                Console.WriteLine($"Controller (MSGPACK) {i}: {reply.Message} ({measurement})ms");
                sw.Restart();
            }

            Console.WriteLine($"Summary: \n{string.Join('\n', measurements.Select(x => $"{x.Key}: {x.Value.Average()}ms (avg)"))}");

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static async Task Main(string[] args)
        {
            var service = "authentication";

            switch (service)
            {
            case "authentication": {
                var credentials = new LoginRequest {
                    Username = "******", Password = "******"
                };
                var channel       = GrpcChannel.ForAddress("https://localhost:5001");
                var client        = new Authentication.AuthenticationClient(channel);
                var authorization = client.Login(credentials);

                var jwt = authorization.AccessToken == null ? "null" : authorization.AccessToken;
                Console.WriteLine($"JWT: {jwt}");

                credentials = new LoginRequest {
                    Username = "******", Password = "******"
                };
                authorization = client.Login(credentials);

                jwt = authorization.AccessToken == null ? "null" : authorization.AccessToken;
                Console.WriteLine($"JWT: {jwt}");

                break;
            }

            case "greeter":
            {
                var input = new HelloRequest {
                    Name = "Jim Bob"
                };

                var channel = GrpcChannel.ForAddress("https://localhost:5001");
                var client  = new Greeter.GreeterClient(channel);

                var reply = await client.SayHelloAsync(input);

                Console.WriteLine(reply.Message);

                break;
            }

            case "meter": {
                var channel     = GrpcChannel.ForAddress("https://localhost:5001");
                var meterClient = new RemoteMeter.RemoteMeterClient(channel);
                var meterInput  = new MeterLookupModel {
                    MeterId = 3
                };
                var meterReply = await meterClient.GetMeterInfoAsync(meterInput);

                Console.WriteLine($"{meterReply.MeterNumber}");

                break;
            }
            }



            Console.ReadLine();
        }