|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System.IO; |
| 20 | +using System.Net; |
| 21 | +using System.Net.Http; |
| 22 | +using System.Net.Http.Headers; |
| 23 | +using System.Threading.Tasks; |
| 24 | +using BenchmarkDotNet.Attributes; |
| 25 | +using Greet; |
| 26 | +using Grpc.AspNetCore.Microbenchmarks.Internal; |
| 27 | +using Grpc.Net.Client; |
| 28 | +using Grpc.Tests.Shared; |
| 29 | + |
| 30 | +namespace Grpc.AspNetCore.Microbenchmarks.Client |
| 31 | +{ |
| 32 | + public class UnaryClientBenchmark |
| 33 | + { |
| 34 | + private Greeter.GreeterClient? _client; |
| 35 | + |
| 36 | + [GlobalSetup] |
| 37 | + public void GlobalSetup() |
| 38 | + { |
| 39 | + var ms = new MemoryStream(); |
| 40 | + MessageHelpers.WriteMessage(ms, new HelloReply()); |
| 41 | + var requestMessage = ms.ToArray(); |
| 42 | + |
| 43 | + var handler = TestHttpMessageHandler.Create(r => |
| 44 | + { |
| 45 | + var content = new ByteArrayContent(requestMessage); |
| 46 | + content.Headers.ContentType = new MediaTypeHeaderValue("application/grpc"); |
| 47 | + |
| 48 | + return Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.OK, content)); |
| 49 | + }); |
| 50 | + |
| 51 | + var httpClient = new HttpClient(handler); |
| 52 | + |
| 53 | + var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions |
| 54 | + { |
| 55 | + HttpClient = httpClient |
| 56 | + }); |
| 57 | + |
| 58 | + _client = new Greeter.GreeterClient(channel); |
| 59 | + } |
| 60 | + |
| 61 | + [Benchmark] |
| 62 | + public Task HandleCallAsync() |
| 63 | + { |
| 64 | + return _client!.SayHelloAsync(new HelloRequest()).ResponseAsync; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments