• webapi和GRPC性能对比


    平台:dotnet 3.0.100-preview6-012264

    IDE:VS2019

    硬件

    新建WEBAPI项目

     

    API项目创建完成,下一步创建GRPC项目

     添加控制台测试项目

     

    为控制台项目添加nuget依赖:Google.Protobuf  Grpc.Core Grpc.Tools

    添加文件夹Protos  把GRPC项目的greet.proto复制到文件夹下面

     

    右键单击项目并选择“编辑 GrpcGreeterClient.csproj”

    <ItemGroup>

      <Protobuf Include="Protosgreet.proto" GrpcServices="Client" />

    </ItemGroup>

    生成控制台项目

    在控制台程序中写两个测试方法

    static async Task Main(string[] args)

            {

                Console.WriteLine("测试开始");

                var a=await ApiTest();

               var b=await GRpcTest();

               Console.WriteLine($"WEBAPI耗时:{a}毫秒");

               Console.WriteLine($"GRPC耗时:{b}毫秒");

                Console.WriteLine("测试完成");

                Console.ReadKey();

            }

            static async Task<long> ApiTest()

            {

                Stopwatch s=new Stopwatch();

                s.Start();

                HttpClient client=new HttpClient();

                for (int i = 0; i < 1000; i++)

                {

                    Console.WriteLine(await client.GetStringAsync("https://localhost:5001/api/values"));

                }

                return s.ElapsedMilliseconds;

               

            }

            static async Task<long> GRpcTest()

            {

                Stopwatch s = new Stopwatch();

                s.Start();

                var channel = new Channel("localhost:50051",

                    ChannelCredentials.Insecure);

          

                var client = new Greeter.GreeterClient(channel);

                for (int i = 0; i < 1000; i++)

                {

                    var reply = await client.SayHelloAsync(

                        new HelloRequest { Name = "GreeterClient" });

                    Console.WriteLine("Greeting: " + reply.Message);

                }

                return s.ElapsedMilliseconds;

            }

    运行程序开始测试

    WEBAPI耗时:48701毫秒

    GRPC耗时:5485毫秒

  • 相关阅读:
    Loj #6307. 「雅礼国庆 2017 Day1」Clique
    bzoj 4457: 游戏任务
    Codeforces 375 D Tree and Queries
    Codeforces 837 E Vasya's Function
    [POI2008]BLO
    Codeforces 451 E Devu and Flowers
    洛谷 P4318 完全平方数
    [JSOI2016]反质数序列
    bzoj 4320: ShangHai2006 Homework
    bzoj4454 C Language Practice
  • 原文地址:https://www.cnblogs.com/lidezhen/p/11042836.html
Copyright © 2020-2023  润新知